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

📄 sht.lst

📁 DS18B20用来测温度十分方便
💻 LST
📖 第 1 页 / 共 3 页
字号:
C51 COMPILER V3.96,  SN-83203013  SHT                                                      09/12/05  17:49:31  PAGE 1   


DOS C51 COMPILER V3.96, COMPILATION OF MODULE SHT
OBJECT MODULE PLACED IN .\OUT\SHT.OBJ
COMPILER INVOKED BY: C:\C51\BIN\C51.EXE SHT.C-CODE-DEBUG-SMALL-OBJECTEXTEND-OJ(.\OUT\SHT.OBJ) 

stmt level    source

   1          //***********************************************************************
   2          //FILE: sht.c
   3          //功能:	温湿渡传感器SHT11的全部操作
   4          //***********************************************************************
   5          
   6          //***********************************************************************
   7          #include "main.h"
   8          #include "intrins.h"
   9          #include "tools.h"
  10          #include "sht.h"
  11          #include "stdio.h"
  12          #include "math.h"
  13          //***********************************************************************
  14          #define  _Nop()  _nop_(),_nop_(),_nop_()     //定义空指令
  15          
  16          /*************************************
  17          名称:  s_write_byte
  18          功能:  写入一个字节函数
  19          *************************************/
  20          char s_write_byte(uchar tvalue)
  21          // writes a byte on the Sensibus and checks the acknowledge 
  22          { 
  23   1        uchar i,error=0; 
  24   1        for (i=0x80;i>0;i/=2)             //shift bit for masking
  25   1        { 
  26   2        	if (i & tvalue) 
  27   2          DATA=1;                         //masking value with i , write to SENSI-BUS
  28   2          else DATA=0;                        
  29   2          SCK=1;                          //clk for SENSI-BUS
  30   2          _Nop();_Nop();_Nop();           //pulswith approx. 5 us  	
  31   2          SCK=0;
  32   2          _Nop();
  33   2        }
  34   1        DATA=1;                           //release DATA-line
  35   1        SCK=1;                            //clk #9 for ack 
  36   1        error=DATA;                       //check ack (DATA will be pulled down by SHT11)
  37   1        SCK=0;
  38   1        return error;                     //error=1 in case of no acknowledge
  39   1      
  40   1      }
  41          
  42          
  43          /*************************************
  44          名称:  s_read_byte
  45          功能:  读出一个字节函数
  46          *************************************/
  47          char s_read_byte(uchar ack)
  48          // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" 
  49          { 
  50   1        uchar i,val=0;
  51   1        DATA=1;                           //release DATA-line
  52   1        for (i=0x80;i>0;i/=2)             //shift bit for masking
  53   1        {
  54   2        	SCK=1;                          //clk for SENSI-BUS
  55   2        	_Nop();                         
C51 COMPILER V3.96,  SN-83203013  SHT                                                      09/12/05  17:49:31  PAGE 2   

  56   2          if (DATA) val=(val | i);        //read bit  
  57   2          SCK=0;                         
  58   2          _Nop(); 					 
  59   2        }
  60   1        DATA=!ack;                        //in case of "ack==1" pull down DATA-Line
  61   1        SCK=1;                            //clk #9 for ack
  62   1        _Nop();                           //pulswith approx. 5 us 
  63   1        SCK=0;
  64   1        _Nop();						            
  65   1        DATA=1;                           //release DATA-line
  66   1        return val;
  67   1      }
  68          
  69          
  70          /*************************************
  71          名称:  s_transstart
  72          功能:  传输开始
  73          *************************************/
  74          void s_transstart(void)
  75          // generates a transmission start 
  76          //       _____         ________
  77          // DATA:      |_______|
  78          //           ___     ___
  79          // SCK : ___|   |___|   |______
  80          {  
  81   1      
  82   1         DATA=1; SCK=0;                   //Initial state
  83   1         _Nop();
  84   1         SCK=1;
  85   1         _Nop();
  86   1         DATA=0;
  87   1         _Nop();
  88   1         SCK=0;  
  89   1         _Nop();
  90   1         SCK=1;
  91   1         _Nop();
  92   1         DATA=1;		   
  93   1         _Nop();
  94   1         SCK=0;		   
  95   1      }
  96          
  97          /*************************************
  98          名称:  s_connectionreset
  99          功能:  重新连接
 100          *************************************/
 101          void s_connectionreset(void)
 102          // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
 103          //       _____________________________________________________         ________
 104          // DATA:                                                      |_______|
 105          //          _    _    _    _    _    _    _    _    _        ___     ___
 106          // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______
 107          {  
 108   1        uchar i; 
 109   1        DATA=1; SCK=0;                    //Initial state
 110   1        for(i=0;i<9;i++)                  //9 SCK cycles
 111   1        { 
 112   2        	 SCK=1;
 113   2        	_Nop();               
 114   2           SCK=0;
 115   2          _Nop();
 116   2        }
 117   1        s_transstart();                   //transmission start
C51 COMPILER V3.96,  SN-83203013  SHT                                                      09/12/05  17:49:31  PAGE 3   

 118   1      }
 119          
 120          
 121          
 122          /*************************************
 123          名称:  s_read_statusreg
 124          功能:  读状态寄存器
 125          *************************************/
 126          /*
 127          char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
 128          // reads the status register with checksum (8-bit)
 129          { 
 130            uchar error=0;
 131            s_transstart();                   //transmission start
 132            error=s_write_byte(STATUS_REG_R); //send command to sensor
 133            *p_value=s_read_byte(ACK);        //read status register (8-bit)
 134            *p_checksum=s_read_byte(noACK);   //read checksum (8-bit) 
 135              Prints("status=");
 136              Puthexbyte(*p_value);
 137              Prints("\r\n"); 
 138            return error;                     //error=1 in case of no response form the sensor
 139          }
 140          */
 141          
 142          /*************************************
 143          名称:  s_write_statusreg
 144          功能:  写状态寄存器
 145          *************************************/
 146          /*
 147          char s_write_statusreg(unsigned char *p_value)
 148          // writes the status register with checksum (8-bit)
 149          { 
 150            uchar error=0;
 151            //s_transstart();                   //transmission start
 152             s_connectionreset();             //后加
 153            error+=s_write_byte(STATUS_REG_W);//send command to sensor
 154            error+=s_write_byte(*p_value);    //send value of status register
 155            return error;                     //error>=1 in case of no response form the sensor
 156          }
 157          */
 158          
 159          /*************************************
 160          名称:  s_softreset
 161          功能:  重置寄存器
 162          *************************************/
 163          /*
 164          char s_softreset(void)
 165          // resets the sensor by a softreset 
 166          { 
 167          
 168            uchar error=0;  
 169            s_connectionreset();              //reset communication
 170            error+=s_write_byte(RESET);       //send RESET-command to sensor
 171            return error;                     //error=1 in case of no response form the sensor
 172          }
 173          */
 174          
 175          /*************************************
 176          名称:  s_humi
 177          功能:  测量湿度
 178          *************************************/
 179          char s_humi(uchar *p_value, uchar *p_checksum)
C51 COMPILER V3.96,  SN-83203013  SHT                                                      09/12/05  17:49:31  PAGE 4   

 180          // measure the huminity
 181          { 
 182   1      	
 183   1        uchar error=0;
 184   1        uint i;
 185   1        s_transstart();                   //transmission start
 186   1        error=s_write_byte(MEASURE_HUMI); //send messure huminity command to sensor
 187   1        for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement
 188   1        if(DATA) error+=1;                // or timeout (~2 sec.) is reached
 189   1        *(p_value)  =s_read_byte(ACK);    //read the first byte  (MSB)
 190   1        *(p_value+1)=s_read_byte(ACK);    //read the second byte (LSB)
 191   1        *p_checksum=s_read_byte(noACK);   //read checksum (8-bit) 
 192   1          Prints("\r\n");
 193   1          Prints("humi=");
 194   1          Puthexbyte(*p_value);
 195   1          Puthexbyte(*(p_value+1));
 196   1          Prints("\r\n");
 197   1          return error;                     //error=1 in case of no response form the sensor
 198   1      }
 199          
 200          /*************************************
 201          名称:  s_temp
 202          功能:  测量温度
 203          *************************************/
 204          char s_temp(uchar *p_value, uchar *p_checksum)
 205          // measure the temprature
 206          { 
 207   1        uchar error=0;
 208   1        uint i;
 209   1        s_transstart();                   //transmission start
 210   1        error=s_write_byte(MEASURE_TEMP); //send messure tempture command to sensor
 211   1        for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement
 212   1        if(DATA) error+=1;                // or timeout (~2 sec.) is reached
 213   1        *(p_value)  =s_read_byte(ACK);    //read the first byte  (MSB)
 214   1        *(p_value+1)=s_read_byte(ACK);    //read the second byte (LSB)
 215   1        *p_checksum=s_read_byte(noACK);   //read checksum (8-bit) 
 216   1          Prints("temp=");
 217   1          Puthexbyte(*p_value);
 218   1          Puthexbyte(*(p_value+1));
 219   1          Prints("\r\n");
 220   1          return error;                   //error=1 in case of no response form the sensor
 221   1      }
 222          
 223          /*************************************
 224          名称:  calc_sth11
 225          功能:  计算温湿渡的值
 226          *************************************/
 227          void calc_sth11(float *p_humidity ,float *p_temperature)
 228          
 229          // calculates temperature  and humidity [%RH] 
 230          // input :  humi [Ticks] (12 bit) 
 231          //          temp [Ticks] (14 bit)
 232          // output:  humi [%RH]
 233          //          temp 
 234          { 
 235   1        const float C1=-4.0;               // for 12 Bit
 236   1        const float C2=+0.0405;            // for 12 Bit
 237   1        const float C3=-0.0000028;         // for 12 Bit
 238   1        const float T11=+0.01;             // for 14 Bit @ 5V
 239   1        const float T22=+0.00008;          // for 14 Bit @ 5V	
 240   1      
 241   1        float rh=*p_humidity;              // rh:      Humidity [Ticks] 12 Bit 
C51 COMPILER V3.96,  SN-83203013  SHT                                                      09/12/05  17:49:31  PAGE 5   

 242   1        float t=*p_temperature;            // t:       Temperature [Ticks] 14 Bit
 243   1        float rh_lin;                      // rh_lin:  Humidity linear
 244   1        float rh_true;                     // rh_true: Temperature compensated humidity
 245   1        float t_C;                         // t_C   :  Temperature 
 246   1      
 247   1        t_C=t*0.01 - 40;                   //calc. temperature from ticks to
 248   1        rh_lin=C3*rh*rh + C2*rh + C1;      //calc. humidity from ticks to [%RH]
 249   1        rh_true=(t_C-25)*(T11+T22*rh)+rh_lin;   //calc. temperature compensated humidity [%RH]
 250   1        if(rh_true>100)rh_true=100;        //cut if the value is outside of
 251   1        if(rh_true<0.1)rh_true=0.1;        //the physical possible range
 252   1        Humi=rh_true*10;
 253   1        Temp=t_C*10;
 254   1      }
 255          
 256          
 257          
 258          /*************************************
 259          名称:  calc_sth11
 260          功能:  计算露点值
 261          *************************************/
 262          /*
 263          float calc_dewpoint(float h,float t)
 264          // calculates dew point
 265          // input:   humidity [%RH], temperature
 266          // output:  dew point 
 267          { float logEx,dew_point;
 268            logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
 269            dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
 270            return dew_point;
 271          }
 272          */
C51 COMPILER V3.96,  SN-83203013  SHT                                                      09/12/05  17:49:31  PAGE 6   

ASSEMBLY LISTING OF GENERATED OBJECT CODE


             ; FUNCTION _s_write_byte (BEGIN)
;---- Variable 'tvalue' assigned to Register 'R7' ----
                                           ; SOURCE LINE # 20
                                           ; SOURCE LINE # 22
                                           ; SOURCE LINE # 23
;---- Variable 'error' assigned to Register 'R6' ----
0000 E4            CLR     A
0001 FE            MOV     R6,A
                                           ; SOURCE LINE # 24
;---- Variable 'i' assigned to Register 'R5' ----
0002 7D80          MOV     R5,#080H
0004         ?C0001:
0004 ED            MOV     A,R5
0005 D3            SETB    C
0006 9400          SUBB    A,#00H
0008 4020          JC      ?C0002
                                           ; SOURCE LINE # 25
                                           ; SOURCE LINE # 26
000A ED            MOV     A,R5
000B 5F            ANL     A,R7
000C 6004          JZ      ?C0004
                                           ; SOURCE LINE # 27
000E D285          SETB    DATA
0010 8002          SJMP    ?C0005
0012         ?C0004:
                                           ; SOURCE LINE # 28
0012 C285          CLR     DATA
0014         ?C0005:
                                           ; SOURCE LINE # 29
0014 D284          SETB    SCK
                                           ; SOURCE LINE # 30
0016 00            NOP     

⌨️ 快捷键说明

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