⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sht11.lst

📁 关于瑞士SHT11程序
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V8.08   SHT11                                                                 04/07/2009 22:03:19 PAGE 1   


C51 COMPILER V8.08, COMPILATION OF MODULE SHT11
OBJECT MODULE PLACED IN SHT11.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE SHT11.C BROWSE DEBUG OBJECTEXTEND

line level    source

   1          /******************************************************************/
   2          /*      temperature模and humidity measure                                                 */
   3          /*        display the data on the 1602;                                                           */
   4          /*        first line display temprrature                                                          */
   5          /*        two line display humidity                                                                       */
   6          /******************************************************************/      
   7          #include<reg52.h>
   8          #include"intrins.h"      /*keil library for _nop() operation*/
   9          #include"stdio.h"
  10          #include"math.h"
  11          #include"absacc.h"       /*keil library*/
  12          
  13          
  14          #define ACK 1
  15          #define noACK 0
  16          #define measure_temp 0x03 /*command to measure temperature*/
  17          #define measure_humi 0x05 /*command to measure humidity*/
  18          #define RESET        0x1e /*soft reset*/
  19          
  20          
  21          #define uchar   unsigned char
  22          #define uint    unsigned int
  23          #define ulong   unsigned long
  24          #define LCD_DATA        P0 /*LCD data import*/
  25          
  26          
  27          
  28          sbit RS =P2^0;
  29          sbit RW =P2^1;
  30          sbit E =P2^2;
  31          sbit DATA =P1^0;
  32          sbit SCK =P1^1;
  33          
  34          
  35          
  36          
  37          typedef union            //reserve measured value for temperature or humidity
  38          {
  39                  uint i;
  40                  float f;
  41          }Value;
  42          
  43          
  44          
  45          
  46          typedef struct  
  47          {
  48                  unsigned char   gewei;
  49                  unsigned char   shiwei;
  50                  unsigned char   DATAstrins1[6];
  51                  unsigned char   DATAstrins2[6];
  52          }Systemtime;
  53          
  54          
  55          
C51 COMPILER V8.08   SHT11                                                                 04/07/2009 22:03:19 PAGE 2   

  56          
  57          
  58          char Write_Byte(uchar value)   //write a byte on the sen_bus and checks the acknowledge
  59          {
  60   1              uchar i,error;
  61   1              for(i=0x80;i>0;i/=2)       // continuous right shift 8 bit
  62   1              {       
  63   2                      if(value&i)DATA =1;      //get the DATA 
  64   2                      else DATA =0;
  65   2                      SCK =1;
  66   2                      _nop_();
  67   2                      _nop_();
  68   2                      _nop_();  //delay 5 us
  69   2                      SCK =0;
  70   2                                                                         // drop      edge
  71   2              }       
  72   1              DATA =1;                                        //release DATA
  73   1              SCK =1;                                         //checks answer  sequence
  74   1              error =DATA;                            //checks the answer state ,if the  acknowledge the DATA =0;
  75   1          SCK =0;     
  76   1              return error;                                   
  77   1      
  78   1      }
  79          
  80          
  81          
  82          char Read_Byte(uchar ack)
  83          {
  84   1              uchar i,val =0;
  85   1              DATA =1;                        //release the DATA line
  86   1              for(i=0x80;i>0;i/=2)     // continuous right shift 8 bit
  87   1              {
  88   2                      SCK =1;
  89   2                      if(DATA)  
  90   2                              val =(val|i);
  91   2                      SCK =0;
  92   2                      
  93   2              
  94   2              }
  95   1              DATA =!ack;        //if acknowledge DATA =0;
  96   1              SCK =1;
  97   1              _nop_(); _nop_(); _nop_(); //delay 5 us
  98   1              SCK =0;
  99   1              DATA =1;                        //release the DATA line
 100   1              return  val;
 101   1      
 102   1      }
 103          
 104          
 105          
 106          void Transmit_Start(void)          //transmit start
 107          {
 108   1              DATA =1;
 109   1              SCK =0;
 110   1              _nop_();
 111   1              SCK =1;
 112   1              _nop_();
 113   1              DATA =0;
 114   1              _nop_();
 115   1              SCK =0;
 116   1              _nop_();
 117   1              _nop_();
C51 COMPILER V8.08   SHT11                                                                 04/07/2009 22:03:19 PAGE 3   

 118   1              _nop_();
 119   1              SCK =1;
 120   1              _nop_();
 121   1              DATA =1;
 122   1          _nop_();
 123   1              SCK =0;
 124   1              
 125   1      }
 126          
 127          
 128          
 129          
 130          void Connection_Reset(void)   //if communication  breaked ,      make use of the sequense start the commnicati
             -on
 131          {
 132   1              unsigned char i;
 133   1              DATA =1;
 134   1              SCK =0;
 135   1              for(i=9;i>0;i--)          //continuous 9 drop edge for connection the transmit
 136   1              {
 137   2                      SCK=1;
 138   2                      SCK=0;
 139   2              
 140   2              }
 141   1              Transmit_Start();  //transmit start
 142   1      
 143   1      }
 144          
 145          
 146          
 147          char Soft_Reset(void)   //reset the SHT11 and clear the value of the  register
 148          {
 149   1              uchar error =0;
 150   1              Connection_Reset();
 151   1              error += Write_Byte(RESET);       
 152   1              return error;                      //return the error for judgment answer
 153   1      
 154   1      }
 155          
 156          
 157          
 158          char Measure_Mode(uchar *P_Value, uchar *P_Checksum,  uchar Mode)  //make sure the style to measure
 159          {
 160   1              uchar  error=0;
 161   1              uchar i;
 162   1              Transmit_Start();
 163   1              switch(Mode)
 164   1              {
 165   2                      case 0:error +=Write_Byte(measure_temp);break;          // judgment the mode of measure
 166   2                      case 1:error +=Write_Byte(measure_humi);break;
 167   2                      default : break;
 168   2              
 169   2              }
 170   1              for(i=65535;i>0;i--)
 171   1                      if(DATA==0)break;        //wait until sht11 complete  the measure
 172   1               if(DATA) error +=1;             //time overflow  2second
 173   1               *P_Value =Read_Byte(ACK);               //get the high byte
 174   1               *(P_Value+1) =Read_Byte(ACK);    //get the low byte
 175   1               *P_Checksum =Read_Byte(noACK);   //get the checksum
 176   1               return error;
 177   1      }
 178          
C51 COMPILER V8.08   SHT11                                                                 04/07/2009 22:03:19 PAGE 4   

 179          
 180          
 181          void Calculate_SHT11(float *P_Temperature , float *P_Humidity) // calculate the temperature and humidity a
             -fter compensated
 182          {
 183   1        const float C1=-4.0;              //for 12 Bit
 184   1        const float C2=+0.0405;           //for 12 Bit
 185   1        const float C3=-0.0000028;        //for 12 Bit
 186   1        const float T1=+0.01;             //for 14 Bit @ 5V
 187   1        const float T2=+0.00008;           //for 14 Bit @ 5V  
 188   1      
 189   1        float rh=*P_Humidity;             //rh:      Humidity [Ticks] 12 Bit  
 190   1        float t=*P_Temperature;           //t:       Temperature [Ticks] 14 Bit
 191   1        float rh_lin;                     //rh_lin:  Humidity linear 
 192   1        float rh_true;                    //rh_true: Temperature compensated humidity
 193   1        float t_C;                        //t_C   :  Temperature [癈]*/ 
 194   1      
 195   1        t_C=t*0.01 - 40;                     //calc. temperature from ticks to 
 196   1        rh_lin=C3*rh*rh + C2*rh + C1;        //calc. humidity from ticks to [%RH]
 197   1        rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;  //calc. temperature compensated humidity [%RH]
 198   1        if(rh_true>100)rh_true=100;          //cut if the value is outside of
 199   1        if(rh_true<0.1)rh_true=0.1;          //the physical possible range
 200   1      
 201   1        *P_Temperature=t_C;                  //return temperature 
 202   1        *P_Humidity=rh_true;                 //return humidity[%RH]                   
 203   1              
 204   1      }
 205          
 206          
 207          
 208          /***************************************************************************************/
 209          void DelayUs(void)        //delay 2 us;
 210          {
 211   1              _nop_();_nop_();
 212   1      }
 213          
 214          
 215          void DelayMs(uint a)  //delay a*1Ms
 216          {
 217   1              uint i,j;
 218   1              for(i=a;i>0;i--)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -