找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6007|回复: 2
收起左侧

SHT10温湿度传感器应用,含单片机源码

[复制链接]
ID:322688 发表于 2018-5-5 12:25 | 显示全部楼层 |阅读模式
早几年用了SHT10的温湿度传感器,稳定,可靠,就是更新数据时间比较长些,
电路很简单,请看手册,
已经在产品中使用了,请放心使用和指正,
  1. #include <iom64v.h>
  2. #include <macros.h>
  3. #include <math.h>
  4. /*********************************************************************************************************/         
  5. #include "sht.h"
  6. #include "uart.h"
  7. #include "key.h"
  8. #include "IO.h"
  9. #include "Lcd.h"
  10. /*********************************************************************************************************/
  11. enum {TEMP,HUMI};  
  12. /*********************************************************************************************************/
  13. //0: 温度1, 1:湿度1 , 2 : 温度2, 3: 湿度2
  14. float   todao[4];                                        //{temp,rh};  模拟量寄存器从1到2      
  15. float   humi_val1;                                       //湿度变量
  16. float   temp_val1;                                       //温度变量

  17. unsigned int   humi_val;                                //湿度变量
  18. unsigned int   temp_val;                                //温度变量
  19. unsigned char  checksum;
  20. /*
  21. const float C1=-4;                                      // for 12 Bit RH
  22. const float C2=+0.0405;                                 // for 12 Bit RH
  23. const float C3=-0.0000028;                              // for 12 Bit RH
  24. const float T1=+0.01;                                   // for 12 Bit RH
  25. const float T2=+0.00008;                                // for 12 Bit RH
  26. */
  27. const float C1=-2.0468;                                 // for 12 Bit RH
  28. const float C2=+0.0367;                                 // for 12 Bit RH
  29. const float C3=-0.0000015955;                           // for 12 Bit RH
  30. const float T1=+0.01;                                   // for 12 Bit RH
  31. const float T2=+0.00008;                                // for 12 Bit RH

  32. /*********************************************************************************************************/
  33. extern unsigned char wendu1_biaozhi;
  34. extern unsigned char wendu2_biaozhi;   
  35. extern unsigned char  R_KeyValue1;                                                                                
  36. /*********************************************************************************************************/

  37. /**********************************************************************************************************
  38. ** 函数名称: s_write_byte()
  39. ** 函数功能: SHT10写单个字节, 写一个字节并输出ACK验证
  40. ** 入口参数: 写入单个字节
  41. ** 出口参数: ACK验证
  42. ** 说    明: writes a byte on the Sensibus and checks the acknowledge
  43. *********************************************************************************************************/
  44.                      
  45. unsigned char s_write_byte(unsigned char value)                                                                           
  46. {                                                                                                                 
  47.     unsigned char i,j,error1=0;                                                                                       
  48.     SHT10_SDA1_OUT;
  49.     for(i=0x80;i>0;i/=2)                  //shift bit for masking                                                        
  50.      {                                                                                                                  
  51.        if (i & value)                                                                                                   
  52.                SHT10_SDA1_OUT1;                  //masking value with i , write to SENSI-BUS                                                
  53.        else                                                                                                           
  54.               SHT10_SDA1_OUT0;                                                                                                      
  55.        SHT10_SCK1_OUT1;                   //clk for SENSI-BUS                                                           
  56.        for(j = 0; j<40;j++)
  57.            {
  58.              NOP();                           //pulswith approx. 5 us                                                                                                         
  59.        }
  60.        SHT10_SCK1_OUT0;                                                                                                           
  61.      }   
  62.         NOP();                                                                                                         
  63.         NOP();                                                                                                           
  64.         NOP();                                                                                                            
  65.     SHT10_SDA1_OUT1;                     //release DATA-line      
  66.         NOP();                                                      
  67.     SHT10_SCK1_OUT1;                     //clk #9 for ack     
  68.         NOP();  
  69.     SHT10_SDA1_IN;         
  70.         NOP();      
  71.         if(SHT10_SDA1)                       //check ack (DATA will be pulled down by SHT11)
  72.          {                                               
  73.       error1 = 1;   
  74.          }
  75.         else
  76.          {
  77.           error1 = 0;
  78.          }                                                     
  79.     SHT10_SCK1_OUT0;                                                                                                               
  80.     return error1;                        //error=1 in case of no acknowledge                                          
  81. }                                                                                                                 
  82.                                                                                                                   
  83. /**********************************************************************************************************
  84. ** 函数名称: s_read_byte()                                                                                
  85. ** 函数功能: SHT10读单个字节,                                                      
  86. ** 入口参数: ACK验证                                                                                
  87. ** 出口参数: 读单个字节                                                                                       
  88. ** 说    明: reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" 读字节并给一个ACK                                    
  89. *********************************************************************************************************/  
  90. unsigned char s_read_byte(unsigned char ack)                                                                              
  91. {                                                                                                                 
  92.     unsigned char i,j,val=0;                                                                                          
  93.     SHT10_SDA1_OUT;
  94.         NOP();  
  95.     SHT10_SDA1_OUT1;                       //release DATA-line                                                                     
  96.     for(i=0x80;i>0;i/=2)                   //shift bit for masking                                                                 
  97.     {                                                                                                               
  98.       SHT10_SCK1_OUT1;                     //clk for SENSI-BUS   
  99.           NOP();                                                                                                         
  100.           NOP();                                                                  
  101.       SHT10_SDA1_IN;  
  102.           NOP();                  
  103.       if(SHT10_SDA1)                                                                                                           
  104.                     val = (val | i);                 //read bit                                                                                 
  105.       SHT10_SCK1_OUT0;                                                                                                                                          
  106.     }                                 
  107.     SHT10_SDA1_OUT;  
  108.         NOP();                                                                                                         
  109.         NOP();                  
  110.     if(!ack)                                                                                                                 
  111.        SHT10_SDA1_OUT1;                    //in case of "ack==1" pull down DATA-Line     
  112.     else                              
  113.        SHT10_SDA1_OUT0;                                               
  114.     SHT10_SCK1_OUT1;                       //clk #9 for ack      
  115.         for(j = 0; j<40;j++)
  116.          {
  117.              NOP();                            //pulswith approx. 5 us                                                                                                         
  118.      }                                                                                               
  119.     SHT10_SCK1_OUT0;                                                                                                                                                     
  120.     NOP();                                                                                                         
  121.         NOP();
  122.     SHT10_SDA1_OUT1;                       //release DATA-line                                                                     
  123.     return val;                                                                                                     
  124. }   
  125. /**********************************************************************************************************
  126. ** 函数名称: s_transstart()                                                                                
  127. ** 函数功能: 启动传输                                                     
  128. ** 入口参数: 无                                                                                
  129. ** 出口参数: 无                                                                                    
  130. ** 说    明: generates a transmission start //启动传输   
  131. //       _____         ________                                                                                   
  132. // DATA:      |_______|                                                                                          
  133. //           ___     ___                                                                                          
  134. // SCK : ___|   |___|   |______   
  135. *********************************************************************************************************/  
  136. void s_transstart(void)                                                                                          
  137. {                                                                                                                 
  138.     SHT10_SDA1_OUT;
  139.         NOP();  
  140.         NOP();  
  141.     SHT10_SDA1_OUT1;                                                                                                        
  142.     NOP();                                                                                                      
  143.     SHT10_SCK1_OUT0;                   //Initial state                                                                       
  144.     NOP();                                                                                                      
  145.     NOP();                                                                                                      
  146.     SHT10_SCK1_OUT1;                                                                                                         
  147.     NOP();                                                                                                      
  148.     SHT10_SDA1_OUT0;                                                                                                        
  149.     NOP();                                                                                                      
  150.     SHT10_SCK1_OUT0;                                                                                                         
  151.     NOP();                                                                                                      
  152.     NOP();                                                                                                      
  153.     NOP();                                                                                                      
  154.     SHT10_SCK1_OUT1;                                                                                                         
  155.     NOP();                                                                                                      
  156.     SHT10_SDA1_OUT1;                                                                                                                     
  157.     NOP();                                                                                                      
  158.     SHT10_SCK1_OUT0;                                                                                                                     
  159. }                                                                                                                 
  160. /**********************************************************************************************************
  161. ** 函数名称: s_connectionreset()                                                                                
  162. ** 函数功能: 复位SHT10    //复位通讯时序                                                
  163. ** 入口参数: 无                                                                                 
  164. ** 出口参数: 无                                                                                      
  165. ** 说    明: reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" 读字节并给一个ACK
  166. //----------------------------------------------------------------------------------                              
  167. // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart                              
  168. //       _____________________________________________________         ________                                   
  169. // DATA:                                                      |_______|                                          
  170. //          _    _    _    _    _    _    _    _    _        ___     ___                                          
  171. // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______                                       
  172. *********************************************************************************************************/                                                                                                                    
  173. void s_connectionreset(void)                                                                              
  174. {                                                                                                                 
  175.     unsigned char i;
  176.     SHT10_SDA1_OUT;      
  177.         NOP();  
  178.         NOP();                                                                                          
  179.     SHT10_SDA1_OUT1;  
  180.         NOP();
  181.         NOP();                                                                                                         
  182.     SHT10_SCK1_OUT0;                            //Initial state           
  183.         NOP();
  184.         NOP();                                                               
  185.     for(i=0;i<9;i++)                            //9 SCK cycles                                                               
  186.      {                                                                                                                        
  187.       SHT10_SCK1_OUT1;                                                                                                                  
  188.           NOP();  
  189.           NOP();                                                                                                                  
  190.       SHT10_SCK1_OUT0                                                                                                                  
  191.           NOP();     
  192.           NOP();                                                                                                               
  193.      }                                                                                                                        
  194.     s_transstart();                             //transmission start                                                         
  195. }                                                                                                                 
  196. /**********************************************************************************************************
  197. ** 函数名称: s_measure()                                                                              
  198. ** 函数功能: SHT10测量温度 或者湿度 ,带校验                                               
  199. ** 入口参数: 读出温湿度值存入地址,校验值存入地址,测量模式                                                                                 
  200. ** 出口参数: 测量出错次数                                                                                   
  201. ** 说    明: makes a measurement (humidity/temperature) with checksum  产生算法
  202. *********************************************************************************************************/
  203. unsigned char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)                             
  204. {                                                                                                                 
  205.     unsigned char error1=0;                                                                                               
  206.     unsigned long i;                                                                                                
  207.                                                                                                                     
  208.     s_transstart();                                     //transmission start                                                                     
  209.     switch(mode)                                                                                                   
  210.     {                                                   //send command to sensor                                                                  
  211.       case TEMP        : error1+=s_write_byte(MEASURE_TEMP); break;                                                         
  212.       case HUMI        : error1+=s_write_byte(MEASURE_HUMI); break;                                                         
  213.       default     : break;                                                                                                
  214.     }  
  215.     SHT10_SDA1_IN;     
  216.           NOP();
  217.           NOP();   
  218.           //delay_nms(150);                                                                                                  
  219.     for(i=0;i<90000;i++)
  220.           {
  221.              if(SHT10_SDA1 == 0)     //wait until sensor has finished the measurement                                                                                               
  222.       break;                                                  
  223.           }  
  224.                                           
  225.     if(SHT10_SDA1)                                                                                                      
  226.             error1 += 1;                                            // or timeout (~2 sec.) is reached                                                
  227.     *(p_value+1 )  =s_read_byte(ACK);                     // read the first byte (MSB)                                                   
  228.     *(p_value)=s_read_byte(ACK);                       // read the second byte (LSB)                                                  
  229.     *p_checksum =s_read_byte(noACK);                    // read checksum                                                               
  230.     return error1;                                                                                                   
  231. }                                                                                                                 
  232. /**********************************************************************************************************
  233. ** 函数名称: s_write_byte_1()
  234. ** 函数功能: SHT10写单个字节, 写一个字节并输出ACK验证
  235. ** 入口参数: 写入单个字节
  236. ** 出口参数: ACK验证
  237. ** 说    明: writes a byte on the Sensibus and checks the acknowledge
  238. *********************************************************************************************************/                                                                                                                  
  239. unsigned char s_write_byte_1(unsigned char value)                                                                                                      
  240. {                                                                                                                 
  241.     unsigned char i,error1=0;                                                                                       
  242.     SHT10_SDA2_OUT;
  243.     for(i=0x80;i>0;i/=2)                        //shift bit for masking                                                        
  244.      {                                                                                                               
  245.       if (i & value)                                                                                                
  246.               SHT10_SDA2_OUT1;                        //masking value with i , write to SENSI-BUS                                             
  247.       else                                                                                                        
  248.             SHT10_SDA2_OUT0;                                                                                                
  249.       SHT10_SCK2_OUT1;                          //clk for SENSI-BUS                                                         
  250.       NOP();                                                                                                      
  251.           NOP();                                                                                                         
  252.           NOP();                                    //pulswith approx. 5 us                                                                                
  253.       SHT10_SCK2_OUT0;                                                                                                      
  254.      }                                                                                                               
  255.     SHT10_SDA2_OUT1;                           //release DATA_1-line   
  256.         NOP();                                                                                                         
  257.         NOP();                                                   
  258.     SHT10_SCK2_OUT1;                           //clk #9 for ack      
  259.         NOP();                                                                                                         
  260.         NOP();                                                      
  261.     SHT10_SDA2_IN;
  262.         NOP();                                                                                                         
  263.         NOP();
  264.         if(SHT10_SDA2)                             //check ack (DATA_1 will be pulled down by SHT11)     
  265.          {
  266.        error1 = 1;                       
  267.          }      
  268.         else
  269.          {
  270.            error1 = 0;  
  271.          }              
  272.     SHT10_SCK2_OUT0;                                                                                                        
  273.     return error1;                              //error=1 in case of no acknowledge                                          
  274. }                                                                                                                 
  275.                                                                                                                   
  276. /**********************************************************************************************************
  277. ** 函数名称: s_read_byte_1()                                                                                
  278. ** 函数功能: SHT10读单个字节,                                                      
  279. ** 入口参数: ACK验证                                                                                
  280. ** 出口参数: 读单个字节                                                                                       
  281. ** 说    明: reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" 读字节并给一个ACK                                    
  282. *********************************************************************************************************/  
  283. unsigned char s_read_byte_1(unsigned char ack)                                                                             
  284. {                                                                                                                 
  285.     unsigned char i,val=0;                                                                                          
  286.     SHT10_SDA2_OUT;
  287.         NOP();  
  288.     SHT10_SDA2_OUT1;                             //release DATA_1-line                                                                 
  289.     for(i=0x80;i>0;i/=2)                         //shift bit for masking                                                                 
  290.      {                                                                                                                           
  291.       SHT10_SCK2_OUT1;                            //clk for SENSI-BUS   
  292.           NOP();                                                                 
  293.       SHT10_SDA2_IN;      
  294.           NOP();                                                                                                         
  295.           NOP();                     
  296.       if(SHT10_SDA2)                                                                                                               
  297.                   val=(val | i);                             //read bit                                                                                 
  298.       SHT10_SCK2_OUT0;                                                                                                                                               
  299.      }                                          
  300.     SHT10_SDA2_OUT;   
  301.         NOP();
  302.         NOP();                          
  303.     if(!ack)                                                                                                                          
  304.       SHT10_SDA2_OUT1;                            //in case of "ack==1" pull down DATA_1-Line                                          
  305.     else                                       
  306.       SHT10_SDA2_OUT0;                          
  307.     SHT10_SCK2_OUT1;                              //clk #9 for ack                                                                     
  308.     NOP();                                                                                                                    
  309.     NOP();                                                                                                                    
  310.     NOP();                                        //pulswith approx. 5 us                                                                 
  311.     SHT10_SCK2_OUT0;         
  312.         NOP();                                                                                                                                                
  313.     SHT10_SDA2_OUT1;                             //release DATA_1-line                                                                 
  314.     return val;                                                                                                                 
  315. }                                                                                                                 
  316.                                                                                                                   
  317. /**********************************************************************************************************
  318. ** 函数名称: s_transstart_1()                                                                                
  319. ** 函数功能: 启动传输                                                                                      
  320. ** 入口参数: 无                                                                                            
  321. ** 出口参数: 无                                                                                            
  322. ** 说    明: generates a transmission start //启动传输                                                     
  323. //       _____         ________                                                                             
  324. // DATA:      |_______|                                                                                    
  325. //           ___     ___                                                                                    
  326. // SCK : ___|   |___|   |______                                                                             
  327. *********************************************************************************************************/  
  328. void s_transstart_1(void)                                                                                         
  329. {                                                                                                                 
  330.      SHT10_SDA2_OUT;
  331.      SHT10_SDA2_OUT1;                                                                                                      
  332.      NOP();                                                                                                      
  333.      SHT10_SCK2_OUT0;                   //Initial state                                                                     
  334.      NOP();                                                                                                      
  335.      NOP();                                                                                                      
  336.      SHT10_SCK2_OUT1;                                                                                                      
  337.      NOP();                                                                                                      
  338.      SHT10_SDA2_OUT0;                                                                                                      
  339.      NOP();                                                                                                      
  340.      SHT10_SCK2_OUT0;                                                                                                      
  341.      NOP();                                                                                                      
  342.      NOP();                                                                                                      
  343.      NOP();                                                                                                      
  344.      SHT10_SCK2_OUT1;                                                                                                      
  345.      NOP();                                                                                                      
  346.      SHT10_SDA2_OUT1;                                                                                                                  
  347.      NOP();                                                                                                      
  348.      SHT10_SCK2_OUT0;                                                                                                                    
  349. }                                                                                                                 
  350. /**********************************************************************************************************
  351. ** 函数名称: s_connectionreset_1()                                                                           
  352. ** 函数功能: 复位SHT10 ,复位通讯时序                                                                  
  353. ** 入口参数: 无                                                                                            
  354. ** 出口参数: 无                                                                                            
  355. ** 说    明: reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" 读字节并给一个ACK  
  356. //----------------------------------------------------------------------------------                        
  357. // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart                        
  358. //       _____________________________________________________         ________                             
  359. // DATA:                                                      |_______|                                    
  360. //          _    _    _    _    _    _    _    _    _        ___     ___                                    
  361. // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______                             
  362. *********************************************************************************************************/  
  363. void s_connectionreset_1(void)                                                                           
  364. {                                                                                                                 
  365.      unsigned char i;                                                                                                
  366.      SHT10_SDA2_OUT;
  367.          NOP();  
  368.          NOP();  
  369.      SHT10_SDA2_OUT1;  
  370.          NOP();  
  371.          NOP();                                                                                                      
  372.      SHT10_SCK2_OUT0;                              //Initial state     
  373.          NOP();  
  374.          NOP();                                                                  
  375.      for(i=0;i<9;i++)                              //9 SCK cycles                                                               
  376.      {                                                                                                                        
  377.              SHT10_SCK2_OUT1;                                                                                                               
  378.             NOP();                                                                                                                 
  379.         SHT10_SCK2_OUT0;                                                                                                               
  380.             NOP();                                                                                                                  
  381.      }                                                                                                                        
  382.      s_transstart_1();                             //transmission start                                                        
  383. }                                                                                                                 
  384.                                                                                                                                                                   
  385. /**********************************************************************************************************
  386. ** 函数名称: s_measure_1()                                                                                 
  387. ** 函数功能: SHT10测量温度 或者湿度 ,带校验                                                               
  388. ** 入口参数: 读出温湿度值存入地址,校验值存入地址,测量模式                                                
  389. ** 出口参数: 测量出错次数                                                                                 
  390. ** 说    明: makes a measurement (humidity/temperature) with checksum  产生算法                           
  391. *********************************************************************************************************/
  392. unsigned char s_measure_1(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)                           
  393. {                                                                                                                 
  394.      unsigned char error1=0;                                                                                               
  395.      unsigned long i;                                                                                                
  396.                                                                                                                      
  397.      s_transstart_1();                                     //transmission start                                                                    
  398.      switch(mode)                                                                                                   
  399.      {                                                     //send command to sensor                                                                  
  400.        case TEMP        : error1+=s_write_byte_1(MEASURE_TEMP); break;                                                      
  401.        case HUMI        : error1+=s_write_byte_1(MEASURE_HUMI); break;                                                      
  402.        default     : break;                                                                                                
  403.      }
  404.      SHT10_SDA2_IN;
  405.            NOP();   
  406.            NOP();
  407.            //delay_nms(150);                                                                                                           
  408.      for (i=0;i<90000;i++)   
  409.            {                                                                                       
  410.              if(SHT10_SDA2==0)                                                                                                
  411.       break;                                                                         //wait until sensor has finished the measurement                                          
  412.      }
  413.      
  414.            if(SHT10_SDA2)                                                                                                      
  415.              error1 += 1;                                                // or timeout (~2 sec.) is reached                                                
  416.      *(p_value+1) =s_read_byte_1(ACK);                      //read the first byte (MSB)                                                
  417.      *(p_value)=s_read_byte_1(ACK);                         //read the second byte (LSB)                                                
  418.      *p_checksum =s_read_byte_1(noACK);                     //read checksum                                                            
  419.      return error1;                                                                                                   
  420. }                                                                                                                 
  421. /**********************************************************************************************************
  422. ** 函数名称: calc_sht10()                                                                                 
  423. ** 函数功能: 计算温度和湿度                                                            
  424. ** 入口参数: 读出湿度值存入地址,读出温度值存入地址                                                
  425. ** 出口参数: 无                                                                                 
  426. ** 说    明: See an application note Non-linearity Compensation from www.Sensirion.com for details
  427. ***********************************************************************************************************/
  428. //----------------------------------------------------------------------------------------                        
  429. // calculates temperature [Deg Cel] and humidity [%RH]                                             
  430. // input :  humi [Ticks] (12 bit)                                                                                 
  431. //          temp [Ticks] (14 bit)                                                                                 
  432. // output:  humi [10x %RH] for 1 decimal place                                                                    
  433. //          temp [100x Deg Cel] for 2 decimal places                              
  434. /*********************************************************************************************************/                                                                                                                  
  435. /*
  436. void calc_sht10( float *p_humidity ,float *p_temperature)                                                     
  437. {                                                                                                                 
  438.            float rh = *p_humidity;                                                                                 
  439.            float t = *p_temperature;                                                                                       
  440.            float t_C;                                                                                                        
  441.            float rh_lin;                                                                                            
  442.                                                                                                                      
  443.            t_C=t*0.01-40.1;                                                                                                
  444.            //multiplied by 100, t_C=t-4000                                                                                 
  445.            //t_C = t - 4000;                                                                                                
  446.                                                                                                                      
  447.            //58<=rh<=1720,         (1430*rh-5120*16)/4096,         in x10                                                              
  448.            //1721<=rh<=3273,         (1110*rh+28930*16)/4096,         in x10                                                            
  449.            //From : Application Note Non-Linearity compensation, Sensirion                                                
  450.            if(rh<=1720)                                                                                                   
  451.             {                                                                                                               
  452.                      rh_lin = (1430UL*(long)rh)>>12;                                                                                 
  453.                     (rh_lin>=20)?(rh_lin = rh_lin-20):(rh_lin=0);                                                                 
  454.             }                                                                                                               
  455.            else                                                                                                            
  456.             {                                                                                                               
  457.                     //rh_lin = (1110*rh + 12320 + 4096*110)/4096, in x10                                                         
  458.                     rh_lin = ((1110UL*(long)rh + 12320UL) >> 12) + 110;                                                                  
  459.                     if(rh_lin>1000)                                                                                               
  460.                            rh_lin=1000;                                                                                                
  461.             }                                                                                                               
  462.                                                                                                                      
  463.            //rh_true = (t_C-25)*(rh*0.00008+0.01)+rh_lin                                                                  
  464.            //~0.12 %RH /deg C, this factor has been ignored at this moment                                                
  465.            *p_temperature = t_C;                                                                                          
  466.            *p_humidity = rh_lin;                                                                                          
  467. } */
  468. /**********************************************************************************************************
  469. ** 函数名称: calc_sht10()                                                                                 
  470. ** 函数功能: 计算温度和湿度                                                            
  471. ** 入口参数: 读出湿度值存入地址,读出温度值存入地址                                                
  472. ** 出口参数: 无                                                                                 
  473. ** 说    明: See an application note Non-linearity Compensation from www.Sensirion.com for details
  474. ***********************************************************************************************************/
  475. void calc_sht10(float *p_humidity ,float *p_temperature)
  476. {
  477.    
  478.     float rh=*p_humidity;                    // rh: Humidity [Ticks] 12 Bit
  479.     float t=*p_temperature;                  // t: Temperature [Ticks] 14 Bit
  480.     float rh_lin;                            // rh_lin: Humidity linear
  481.     float rh_true;                           // rh_true: Temperature compensated humidity
  482.     float t_C;                               // t_C : Temperature [°C]
  483.     t_C=t*0.01 - 40.1;                       // calc. temperature[°C]from 14 bit temp.ticks @5V
  484.     rh_lin=C3*rh*rh + C2*rh + C1;            // calc. humidity from ticks to [%RH]
  485.     rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;      // calc. temperature compensated humidity[%RH]

  486.     if(rh_true>100)rh_true=100;              // cut if the value is outside of
  487.     if(rh_true<0.1)rh_true=0.1;              // the physical possible range
  488.     *p_temperature=t_C;                      // return temperature [°C]
  489.     *p_humidity=rh_true;                     // return humidity[%RH]
  490. }
  491. /**********************************************************************************************************
  492. ** 函数名称: calc_dewpoint()                                                                                 
  493. ** 函数功能: 计算露点                                                      
  494. ** 入口参数: humidity [%RH], temperature [°C]                                             
  495. ** 出口参数: dew point [°C]                                                                                 
  496. ** 说    明: See an application note Non-linearity Compensation from www.Sensirion.com for details
  497. ***********************************************************************************************************/   
  498. float calc_dewpoint(float h,float t)
  499. {
  500.      float k,dew_point ;
  501.      k = (log10(h)-2)/0.4343 + (17.62*t)/(243.12+t);
  502.      dew_point = 243.12*k/(17.62-k);
  503.      return dew_point;
  504. }                                                                                                            
  505. /**********************************************************************************************************
  506. ** 函数名称: Temp_Humi_Check()                                                                                
  507. ** 函数功能: 温度湿度检测                          
  508. ** 入口参数: 无                           
  509. ** 出口参数: 无                           
  510. ** 说    明:                           
  511. *********************************************************************************************************/
  512. void Temp_Humi_Check(void)                                                                                                                       
  513. {                                                                                                        
  514.      unsigned char error=0;
  515.          if(wendu1_biaozhi==1)                                                                                 
  516.            {                                                                                                   
  517.                error=0;                                                                                       
  518.                    s_connectionreset();                                                              //传送重置                                                            
  519.                       error += s_measure((unsigned char*) &humi_val,&checksum,HUMI);     //measure humidity     湿度测量  
  520.                    error += s_measure((unsigned char*) &temp_val,&checksum,TEMP);     //measure temperature  温度测量      
  521.                if(error!=0)                                                                                    
  522.                {                                                                                               
  523.                  s_connectionreset();                                            //in case of an error: connection reset                                
  524.                          todao[0]=0;                                                     //湿度测量  1                                                                          
  525.                      todao[1]=0;                                                     //温度测量  1                                                                        
  526.                 }                                                                                                
  527.                else                                                                                            
  528.                   {
  529.                           humi_val1=(float)humi_val;                                      //converts integer to float
  530.              temp_val1=(float)temp_val;                                      //converts integer to float
  531.                         
  532.                          calc_sht10(&humi_val1,&temp_val1);                              //calculate humidity, temperature
  533.              //calc_dewpoint(humi_val1,temp_val1);
  534.                         
  535.                          todao[0]=temp_val1;                                             //温度测量  1                                                            
  536.                      todao[1]=humi_val1;                                             //湿度测量  1     
  537.                         }                                                           
  538.                 }                                                                                            
  539.           if(wendu2_biaozhi==1)                                                                                
  540.                 {                                                                                                   
  541.               error=0;                                                                                         
  542.                   s_connectionreset_1();                                                           ////传送重置                                                         
  543.                      error+=s_measure_1((unsigned char*) &humi_val,&checksum,HUMI);       //measure humidity     湿度测量
  544.               error+=s_measure_1((unsigned char*) &temp_val,&checksum,TEMP);       //measure temperature  温度测量  
  545.               if(error!=0)                                                                                   
  546.                {                                                                                             
  547.                      s_connectionreset_1();                                              //in case of an error: connection reset                                   
  548.                      todao[2]=0;                                                       //湿度测量  2                                                                        
  549.                      todao[3]=0;                                                       //温度测量   2                                                                        
  550.              return;                                                                                 
  551.            }                                                                                            
  552.               else                                                                                             
  553.                  {
  554.                           humi_val1=(float)humi_val;                                      //converts integer to float
  555.              temp_val1=(float)temp_val;                                      //converts integer to float
  556.                          calc_sht10(&humi_val1,&temp_val1);                              //calculate humidity, temperature         
  557.                    //calc_dewpoint(humi_val1,temp_val1);
  558.                         
  559.                          todao[2]=temp_val1;                                                  //温度测量  2                                                              
  560.                      todao[3]=humi_val1;                                                  //湿度测量  2      
  561.                    }                                                         
  562.            }                                                                                                
  563.                                                                                                                                              
  564. }                                                                                                        
  565. /**********************************************************************************************************
  566. ** 函数名称: Temp_Humi_Init()                                                                          
  567. ** 函数功能: 温度湿度检测                                                                                            
  568. ** 入口参数: 无                                                                                            
  569. ** 出口参数: 无                                                                                            
  570. ** 说    明: 检测到温度时 ,置 标志位                                                                                             
  571. *********************************************************************************************************/  
  572. void Temp_Humi_Init(void)                                                                           
  573. {                                                                                                        
  574.       unsigned char error=0;
  575.           //float Dew_Point;
  576.           s_connectionreset();                                                                //传送重置                                                               
  577.              error+=s_measure((unsigned char*) &humi_val,&checksum,HUMI);          //measure humidity     湿度测量      
  578.           error+=s_measure((unsigned char*) &temp_val,&checksum,TEMP);          //measure temperature  温度测量        
  579.       if(error!=0)                                                                                          
  580.                 {                                                                                                        
  581.                s_connectionreset();                                              //in case of an error: connection reset                                    
  582.                   todao[0]=0;                                                       //湿度测量  1                                                                           
  583.                   todao[1]=0;                                                       //温度测量  1                                                                        
  584.                 }                                                                                                        
  585.       else                                                                                                   
  586.               {                     
  587.               humi_val1=(float)humi_val;                                     //converts integer to float
  588.           temp_val1=(float)temp_val;                                     //converts integer to float
  589.                   
  590.                   calc_sht10(&humi_val1,&temp_val1);                             //calculate humidity, temperature     
  591.               //Dew_Point=calc_dewpoint(humi_val1,temp_val1);
  592.                                                                                              
  593.           todao[0]=temp_val1;                                                //湿度测量  1                                                                     
  594.               todao[1]=humi_val1;                                                //温度测量  1                                                                     
  595.         }                                                               
  596.           error=0;                                                                                             
  597.           s_connectionreset_1();                                                           //传送重置                                                            
  598.              error+=s_measure_1((unsigned char*) &humi_val,&checksum,HUMI);       //measure humidity     湿度测量   
  599.           error+=s_measure_1((unsigned char*) &temp_val,&checksum,TEMP);       //measure temperature  温度测量   
  600.       if(error!=0)                                                                                       
  601.        {                                                                                                  
  602.              s_connectionreset_1();                                            //in case of an error: connection reset                                   
  603.                  todao[2]=0;                                                       //温度测量  2                                                                             
  604.                  todao[3]=0;                                                       //湿度测量  2                                                                             
  605.        }                                                                                                
  606.              else                                                                                             
  607.        {        
  608.                humi_val1=(float)humi_val;                                     //converts integer to float
  609.          temp_val1=(float)temp_val;                                     //converts integer to float
  610.                
  611.                  calc_sht10(&humi_val1,&temp_val1);                            //calculate humidity, temperature
  612.          //Dew_Point=calc_dewpoint(humi_val1,temp_val1);                       
  613.                                                                                                                                                                                 
  614.              todao[2] = temp_val1;                                              //温度测量  2                                                                     
  615.              todao[3] = humi_val1;                                              //湿度测量  2      
  616.            }                                                         
  617.       if(todao[1]==0)                                                                                
  618.            {                                                                                                   
  619.             if( todao[0]==0)                                                                                   
  620.             {
  621.                wendu1_biaozhi = 0;
  622.             }                                                                              
  623.            }                                                                                                   
  624.           else                                                                 // 有湿度时一定有温度                                    
  625.            {                                                                                                   
  626.           wendu1_biaozhi = 1;                                                                              
  627.            }                                                                                                   
  628.       if(todao[3]==0)                                                                                                                                 
  629.            {                                                                                                   
  630.              if( todao[2]==0)                                                                                   
  631.               {
  632.                        wendu2_biaozhi = 0;
  633.               }                                                                                
  634.            }                                                                    // 有湿度时一定有温度?                                 
  635.           else                                                                                                
  636.            {                                                                                                   
  637.           wendu2_biaozhi = 1;                                                                                
  638.            }                                                                                                   
  639. }                                                                                                                                      
复制代码


回复

使用道具 举报

ID:168971 发表于 2018-5-13 20:21 | 显示全部楼层
试试吧,看看用在stc上可以不
回复

使用道具 举报

ID:814176 发表于 2022-1-19 13:03 | 显示全部楼层
向日葵男人 发表于 2018-5-13 20:21
试试吧,看看用在stc上可以不

STC89C52 OK
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表