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

📄 sht10.lst

📁 51单片机实现的温湿度采集监控
💻 LST
字号:
C51 COMPILER V8.08   SHT10                                                                 01/11/2009 23:45:33 PAGE 1   


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

line level    source

   1          #include"SHT10.h"
   2          #include <reg52.h>
   3          #include<intrins.h>
   4          #include<delay.h>
   5          
   6          
   7          sbit SCK  = P2^0;      //define clock interface 
   8          sbit DATA = P2^1;      //define data interface
   9          
  10          
  11          
  12          
  13          typedef union   
  14          { unsigned int i;      
  15            float f;  
  16          } value;
  17          
  18          /********************************************************************
  19          函数名称:transStartSHT10        启动传输
  20          入口参数:无
  21          返回参数:无
  22          *********************************************************************/
  23          void TransStartSHT10(void)
  24          {    
  25   1         DATA=1; SCK=0;                  
  26   1         _nop_();  
  27   1         SCK=1;  
  28   1         _nop_();  
  29   1         DATA=0;  
  30   1         _nop_();  
  31   1         SCK=0;    
  32   1         _nop_();_nop_();_nop_();  
  33   1         SCK=1;  
  34   1         _nop_();  
  35   1         DATA=1;         
  36   1         _nop_();  
  37   1         SCK=0;         
  38   1      }  
  39          
  40          /*********************************************************************
  41          函数名称:connectionResetSHT10   重新连接
  42          入口参数:无
  43          返回参数:无
  44          *********************************************************************/
  45          void ConnectionResetSHT10(void)
  46          {    
  47   1        uchar i;   
  48   1        DATA=1; SCK=0;                    //Initial state  
  49   1        for(i=0;i<9;i++)                  //9 SCK cycles  
  50   1        {  
  51   2          SCK=1; 
  52   2          SCK=0;  
  53   2        }  
  54   1        TransStartSHT10();                   //transmission start  
  55   1      }  
C51 COMPILER V8.08   SHT10                                                                 01/11/2009 23:45:33 PAGE 2   

  56          
  57          /********************************************************************
  58          函数名称:writeSHT10    写数据
  59          入口参数:value
  60          返回参数:error (0成功/1失败)
  61          ********************************************************************/
  62          
  63          char WriteSHT10(uchar value)
  64          {   
  65   1        uchar i,error=0;    
  66   1        for (i=0x80;i>0;i/=2)             //shift bit for masking  
  67   1        {   
  68   2          if (i & value) DATA=1;          //masking value with i , write to SENSI-BUS  
  69   2          else DATA=0;                          
  70   2          SCK=1;                          //clk for SENSI-BUS  
  71   2          _nop_();_nop_();_nop_();        //pulswith approx. 5 us      
  72   2          SCK=0;  
  73   2        }  
  74   1        DATA=1;                           //release DATA-line  
  75   1        SCK=1;                            //clk #9 for ack   
  76   1        error=DATA;                       //check ack (DATA will be pulled down by DHT90),DATA在第9个上升沿将被D
             -HT90自动下拉为低电平。  
  77   1        _nop_();_nop_();_nop_(); 
  78   1        SCK=0; 
  79   1        DATA=1;                           //release DATA-line  
  80   1        return error;                     //error=1 in case of no acknowledge //返回:0成功,1失败 
  81   1      }  
  82          
  83          /********************************************************************
  84          函数名称:readSHT10      读数据
  85          入口参数:ack
  86          返回参数:val(ack=0时读数据,返回val)
  87          *********************************************************************/
  88          
  89          char ReadSHT10(uchar ack)
  90          {   
  91   1        uchar i,val=0;  
  92   1        DATA=1;                           //release DATA-line  
  93   1        for (i=0x80;i>0;i/=2)             //shift bit for masking  
  94   1        { SCK=1;                          //clk for SENSI-BUS  
  95   2          if (DATA) val=(val | i);        //read bit    
  96   2              _nop_();_nop_();_nop_();        //pulswith approx. 5 us 
  97   2          SCK=0;               
  98   2        }  
  99   1        if(ack==1)DATA=0;                 //in case of "ack==1" pull down DATA-Line  
 100   1        else DATA=1;                      //如果是校验(ack==0),读取完后结束通讯 
 101   1        _nop_();_nop_();_nop_();          //pulswith approx. 5 us  
 102   1        SCK=1;                            //clk #9 for ack  
 103   1        _nop_();_nop_();_nop_();          //pulswith approx. 5 us   
 104   1        SCK=0;                  
 105   1        _nop_();_nop_();_nop_();          //pulswith approx. 5 us  
 106   1        DATA=1;                           //release DATA-line  
 107   1        return val;  
 108   1      }  
 109          
 110          /*********************************************************************
 111          函数名称:measureSHT10 测量温度 
 112          入口参数:*p_value,*p_checksum,mode
 113          返回参数:error(0成功/1失败)
 114          *********************************************************************/
 115          
 116          char MeasureSHT10(uchar *p_value, uchar *p_checksum, uchar mode)
C51 COMPILER V8.08   SHT10                                                                 01/11/2009 23:45:33 PAGE 3   

 117          {   
 118   1        unsigned error=0;  
 119   1        uchar i;  
 120   1        
 121   1        TransStartSHT10();                   //transmission start  
 122   1        switch(mode){                     //send command to sensor  
 123   2          case 01  : error+=WriteSHT10(MEASURE_TEMP); break;  
 124   2          case 02  : error+=WriteSHT10(MEASURE_HUMI); break;  
 125   2          default     : break;     
 126   2        }  
 127   1        for (i=0;i<110;i++) {delay(2);if(DATA==0) break;} //wait until sensor has finished the measurement  
 128   1        if(DATA) error+=1;                // or timeout (~2 sec.) is reached  
 129   1        *(p_value)  =ReadSHT10(ACK);    //read the first byte (MSB)  
 130   1        *(p_value+1)=ReadSHT10(ACK);    //read the second byte (LSB)  
 131   1        *p_checksum =ReadSHT10(noACK);  //read checksum  
 132   1        return error;  
 133   1      }  
 134          
 135          /********************************************************************
 136          函数名称: calcSHT10                                             计算温度和相对湿度值
 137          入口参数:*p_humidity ,*p_temperature
 138          返回参数:无
 139          ********************************************************************/
 140          void CalcSHT10(float *p_humidity ,float *p_temperature)
 141          // calculates temperature [C] and humidity [%RH]  
 142          // input :  humi [Ticks] (12 bit)  
 143          //          temp [Ticks] (14 bit) 
 144          // output:  humi [%RH] 
 145          //          temp [C] 
 146          { const float C1=-4.0;              // for 12 Bit 
 147   1        const float C2=0.0405;           // for 12 Bit 
 148   1        const float C3=-0.0000028;        // for 12 Bit 
 149   1        const float T1=0.01;             // for 14 Bit @ 5V 
 150   1        const float T2=0.00008;           // for 14 Bit @ 5V  
 151   1      
 152   1        float rh=*p_humidity;             // rh:      Humidity [Ticks] 12 Bit  
 153   1        float t=*p_temperature;           // t:       Temperature [Ticks] 14 Bit 
 154   1        float rh_lin;                     // rh_lin:  Humidity linear 
 155   1        float rh_true;                    // rh_true: Temperature compensated humidity 
 156   1        float t_C;                        // t_C   :  Temperature [C] 
 157   1      
 158   1        t_C=t*0.01 - 40;                  //calc. temperature from ticks to [C] 
 159   1        rh_lin=C3*rh*rh + C2*rh + C1;     //calc. humidity from ticks to [%RH] 
 160   1        rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;   //calc. temperature compensated humidity [%RH] 
 161   1        if(rh_true>100)rh_true=100;       //cut if the value is outside of 
 162   1        if(rh_true<0.1)rh_true=0.1;       //the physical possible range 
 163   1      
 164   1        *p_temperature=t_C;               //return temperature [C] 
 165   1        *p_humidity=rh_true;              //return humidity[%RH] 
 166   1      }
 167          
 168          /********************************************************************
 169          函数名称: getSHT10()
 170          入口参数:float temp,float humi
 171          返回参数:无
 172          ********************************************************************/
 173          void GetSHT10(float *temp,float *humi)
 174          {
 175   1               value humi_val,temp_val; 
 176   1           uchar error,checksum; 
 177   1               error=0;  
 178   1                error+=MeasureSHT10((uchar*) &humi_val.i,&checksum,HUMI);  //measure humidity  
C51 COMPILER V8.08   SHT10                                                                 01/11/2009 23:45:33 PAGE 4   

 179   1                error+=MeasureSHT10((uchar*) &temp_val.i,&checksum,TEMP);  //measure temperature  
 180   1                if(error!=0) ConnectionResetSHT10();                 //in case of an error: connection reset  
 181   1                else  
 182   1                { humi_val.f=(float)humi_val.i;                   //converts integer to float 
 183   2                  temp_val.f=(float)temp_val.i;                   //converts integer to float 
 184   2                  CalcSHT10(&humi_val.f,&temp_val.f);            //calculate humidity, temperature 
 185   2                              
 186   2                              *temp=temp_val.f;
 187   2                              *humi=humi_val.f;
 188   2      
 189   2                        }
 190   1      }
 191          
 192          
 193          
 194            


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    833    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----      72
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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