久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 5988|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

SHT10溫濕度傳感器應(yīng)用,含單片機(jī)源碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:322688 發(fā)表于 2018-5-5 12:25 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
早幾年用了SHT10的溫濕度傳感器,穩(wěn)定,可靠,就是更新數(shù)據(jù)時(shí)間比較長(zhǎng)些,
電路很簡(jiǎn)單,請(qǐng)看手冊(cè),
已經(jīng)在產(chǎn)品中使用了,請(qǐng)放心使用和指正,
  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. ** 函數(shù)名稱: s_write_byte()
  39. ** 函數(shù)功能: SHT10寫單個(gè)字節(jié), 寫一個(gè)字節(jié)并輸出ACK驗(yàn)證
  40. ** 入口參數(shù): 寫入單個(gè)字節(jié)
  41. ** 出口參數(shù): ACK驗(yàn)證
  42. ** 說(shuō)    明: 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. ** 函數(shù)名稱: s_read_byte()                                                                                
  85. ** 函數(shù)功能: SHT10讀單個(gè)字節(jié),                                                      
  86. ** 入口參數(shù): ACK驗(yàn)證                                                                                
  87. ** 出口參數(shù): 讀單個(gè)字節(jié)                                                                                       
  88. ** 說(shuō)    明: reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" 讀字節(jié)并給一個(gè)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. ** 函數(shù)名稱: s_transstart()                                                                                
  127. ** 函數(shù)功能: 啟動(dòng)傳輸                                                     
  128. ** 入口參數(shù): 無(wú)                                                                                
  129. ** 出口參數(shù): 無(wú)                                                                                    
  130. ** 說(shuō)    明: generates a transmission start //啟動(dòng)傳輸   
  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. ** 函數(shù)名稱: s_connectionreset()                                                                                
  162. ** 函數(shù)功能: 復(fù)位SHT10    //復(fù)位通訊時(shí)序                                                
  163. ** 入口參數(shù): 無(wú)                                                                                 
  164. ** 出口參數(shù): 無(wú)                                                                                      
  165. ** 說(shuō)    明: reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" 讀字節(jié)并給一個(gè)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. ** 函數(shù)名稱: s_measure()                                                                              
  198. ** 函數(shù)功能: SHT10測(cè)量溫度 或者濕度 ,帶校驗(yàn)                                               
  199. ** 入口參數(shù): 讀出溫濕度值存入地址,校驗(yàn)值存入地址,測(cè)量模式                                                                                 
  200. ** 出口參數(shù): 測(cè)量出錯(cuò)次數(shù)                                                                                   
  201. ** 說(shuō)    明: makes a measurement (humidity/temperature) with checksum  產(chǎn)生算法
  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. ** 函數(shù)名稱: s_write_byte_1()
  234. ** 函數(shù)功能: SHT10寫單個(gè)字節(jié), 寫一個(gè)字節(jié)并輸出ACK驗(yàn)證
  235. ** 入口參數(shù): 寫入單個(gè)字節(jié)
  236. ** 出口參數(shù): ACK驗(yàn)證
  237. ** 說(shuō)    明: 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. ** 函數(shù)名稱: s_read_byte_1()                                                                                
  278. ** 函數(shù)功能: SHT10讀單個(gè)字節(jié),                                                      
  279. ** 入口參數(shù): ACK驗(yàn)證                                                                                
  280. ** 出口參數(shù): 讀單個(gè)字節(jié)                                                                                       
  281. ** 說(shuō)    明: reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" 讀字節(jié)并給一個(gè)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. ** 函數(shù)名稱: s_transstart_1()                                                                                
  319. ** 函數(shù)功能: 啟動(dòng)傳輸                                                                                      
  320. ** 入口參數(shù): 無(wú)                                                                                            
  321. ** 出口參數(shù): 無(wú)                                                                                            
  322. ** 說(shuō)    明: generates a transmission start //啟動(dòng)傳輸                                                     
  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. ** 函數(shù)名稱: s_connectionreset_1()                                                                           
  352. ** 函數(shù)功能: 復(fù)位SHT10 ,復(fù)位通訊時(shí)序                                                                  
  353. ** 入口參數(shù): 無(wú)                                                                                            
  354. ** 出口參數(shù): 無(wú)                                                                                            
  355. ** 說(shuō)    明: reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" 讀字節(jié)并給一個(gè)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. ** 函數(shù)名稱: s_measure_1()                                                                                 
  387. ** 函數(shù)功能: SHT10測(cè)量溫度 或者濕度 ,帶校驗(yàn)                                                               
  388. ** 入口參數(shù): 讀出溫濕度值存入地址,校驗(yàn)值存入地址,測(cè)量模式                                                
  389. ** 出口參數(shù): 測(cè)量出錯(cuò)次數(shù)                                                                                 
  390. ** 說(shuō)    明: makes a measurement (humidity/temperature) with checksum  產(chǎn)生算法                           
  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. ** 函數(shù)名稱: calc_sht10()                                                                                 
  423. ** 函數(shù)功能: 計(jì)算溫度和濕度                                                            
  424. ** 入口參數(shù): 讀出濕度值存入地址,讀出溫度值存入地址                                                
  425. ** 出口參數(shù): 無(wú)                                                                                 
  426. ** 說(shuō)    明: 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. ** 函數(shù)名稱: calc_sht10()                                                                                 
  470. ** 函數(shù)功能: 計(jì)算溫度和濕度                                                            
  471. ** 入口參數(shù): 讀出濕度值存入地址,讀出溫度值存入地址                                                
  472. ** 出口參數(shù): 無(wú)                                                                                 
  473. ** 說(shuō)    明: 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. ** 函數(shù)名稱: calc_dewpoint()                                                                                 
  493. ** 函數(shù)功能: 計(jì)算露點(diǎn)                                                      
  494. ** 入口參數(shù): humidity [%RH], temperature [°C]                                             
  495. ** 出口參數(shù): dew point [°C]                                                                                 
  496. ** 說(shuō)    明: 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. ** 函數(shù)名稱: Temp_Humi_Check()                                                                                
  507. ** 函數(shù)功能: 溫度濕度檢測(cè)                          
  508. ** 入口參數(shù): 無(wú)                           
  509. ** 出口參數(shù): 無(wú)                           
  510. ** 說(shuō)    明:                           
  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     濕度測(cè)量  
  520.                    error += s_measure((unsigned char*) &temp_val,&checksum,TEMP);     //measure temperature  溫度測(cè)量      
  521.                if(error!=0)                                                                                    
  522.                {                                                                                               
  523.                  s_connectionreset();                                            //in case of an error: connection reset                                
  524.                          todao[0]=0;                                                     //濕度測(cè)量  1                                                                          
  525.                      todao[1]=0;                                                     //溫度測(cè)量  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;                                             //溫度測(cè)量  1                                                            
  536.                      todao[1]=humi_val1;                                             //濕度測(cè)量  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     濕度測(cè)量
  544.               error+=s_measure_1((unsigned char*) &temp_val,&checksum,TEMP);       //measure temperature  溫度測(cè)量  
  545.               if(error!=0)                                                                                   
  546.                {                                                                                             
  547.                      s_connectionreset_1();                                              //in case of an error: connection reset                                   
  548.                      todao[2]=0;                                                       //濕度測(cè)量  2                                                                        
  549.                      todao[3]=0;                                                       //溫度測(cè)量   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;                                                  //溫度測(cè)量  2                                                              
  560.                      todao[3]=humi_val1;                                                  //濕度測(cè)量  2      
  561.                    }                                                         
  562.            }                                                                                                
  563.                                                                                                                                              
  564. }                                                                                                        
  565. /**********************************************************************************************************
  566. ** 函數(shù)名稱: Temp_Humi_Init()                                                                          
  567. ** 函數(shù)功能: 溫度濕度檢測(cè)                                                                                            
  568. ** 入口參數(shù): 無(wú)                                                                                            
  569. ** 出口參數(shù): 無(wú)                                                                                            
  570. ** 說(shuō)    明: 檢測(cè)到溫度時(shí) ,置 標(biāo)志位                                                                                             
  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     濕度測(cè)量      
  578.           error+=s_measure((unsigned char*) &temp_val,&checksum,TEMP);          //measure temperature  溫度測(cè)量        
  579.       if(error!=0)                                                                                          
  580.                 {                                                                                                        
  581.                s_connectionreset();                                              //in case of an error: connection reset                                    
  582.                   todao[0]=0;                                                       //濕度測(cè)量  1                                                                           
  583.                   todao[1]=0;                                                       //溫度測(cè)量  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;                                                //濕度測(cè)量  1                                                                     
  594.               todao[1]=humi_val1;                                                //溫度測(cè)量  1                                                                     
  595.         }                                                               
  596.           error=0;                                                                                             
  597.           s_connectionreset_1();                                                           //傳送重置                                                            
  598.              error+=s_measure_1((unsigned char*) &humi_val,&checksum,HUMI);       //measure humidity     濕度測(cè)量   
  599.           error+=s_measure_1((unsigned char*) &temp_val,&checksum,TEMP);       //measure temperature  溫度測(cè)量   
  600.       if(error!=0)                                                                                       
  601.        {                                                                                                  
  602.              s_connectionreset_1();                                            //in case of an error: connection reset                                   
  603.                  todao[2]=0;                                                       //溫度測(cè)量  2                                                                             
  604.                  todao[3]=0;                                                       //濕度測(cè)量  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;                                              //溫度測(cè)量  2                                                                     
  615.              todao[3] = humi_val1;                                              //濕度測(cè)量  2      
  616.            }                                                         
  617.       if(todao[1]==0)                                                                                
  618.            {                                                                                                   
  619.             if( todao[0]==0)                                                                                   
  620.             {
  621.                wendu1_biaozhi = 0;
  622.             }                                                                              
  623.            }                                                                                                   
  624.           else                                                                 // 有濕度時(shí)一定有溫度                                    
  625.            {                                                                                                   
  626.           wendu1_biaozhi = 1;                                                                              
  627.            }                                                                                                   
  628.       if(todao[3]==0)                                                                                                                                 
  629.            {                                                                                                   
  630.              if( todao[2]==0)                                                                                   
  631.               {
  632.                        wendu2_biaozhi = 0;
  633.               }                                                                                
  634.            }                                                                    // 有濕度時(shí)一定有溫度?                                 
  635.           else                                                                                                
  636.            {                                                                                                   
  637.           wendu2_biaozhi = 1;                                                                                
  638.            }                                                                                                   
  639. }                                                                                                                                      
復(fù)制代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏4 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:168971 發(fā)表于 2018-5-13 20:21 | 只看該作者
試試吧,看看用在stc上可以不
回復(fù)

使用道具 舉報(bào)

板凳
ID:814176 發(fā)表于 2022-1-19 13:03 | 只看該作者
向日葵男人 發(fā)表于 2018-5-13 20:21
試試吧,看看用在stc上可以不

STC89C52 OK
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: tube国产| 成人中文网 | 日韩第一区 | 夜夜爽99久久国产综合精品女不卡 | 亚洲成人一区 | 久久久久一区二区三区四区 | 91精品一区二区三区久久久久久 | 久久视频免费看 | 国产一区二区三区网站 | 日韩精品一区二区久久 | 国产一区二区高清在线 | 亚洲欧美中文日韩在线v日本 | 国产午夜精品久久久久免费视高清 | 中国大陆高清aⅴ毛片 | 精品国产一区二区三区日日嗨 | 中文字幕av一区 | 日韩精品一区二区三区在线播放 | 成人午夜精品一区二区三区 | 久久久国产精品视频 | av免费网站在线观看 | 亚洲一区视频在线播放 | 免费一级欧美在线观看视频 | 2019天天干夜夜操 | 国产亚洲www | 精品国产一区久久 | 中文字幕高清av | 一区二区三区四区免费视频 | 久久久久国产精品一区二区 | 少妇特黄a一区二区三区88av | 久久99精品久久久久久国产越南 | 亚洲成人免费在线 | 性高朝久久久久久久3小时 av一区二区三区四区 | 国产精品91网站 | 超碰导航 | 国产亚洲成av人片在线观看桃 | 国产视频福利一区 | 九九综合 | 久久久观看 | 天天操狠狠操 | 日本粉嫩一区二区三区视频 | 午夜电影网站 |