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

QQ登錄

只需一步,快速開始

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

SHT11.h下載 溫濕度傳感器SHT11的驅(qū)動(dòng)程序,提供了外界調(diào)用接口函數(shù)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:140725 發(fā)表于 2016-10-11 19:29 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式


所有文件下載:
溫濕度傳感器SHT11的驅(qū)動(dòng)程序.rar (44.81 KB, 下載次數(shù): 50)


下面是部分程序的預(yù)覽:
  1. /*******************************************************************
  2. *【文 件 名】:SHT11.h                                             *
  3. *【建立日期】:7月5日                                        *
  4. *【修改日期】:7月5日                                        *
  5. *【文件作用】:溫濕度傳感器SHT11的驅(qū)動(dòng)程序,提供了外界調(diào)用接口函數(shù) *
  6. *【說    明】:          *
  7. *------------------------------------------------------------------*
  8. *【作    者】:郭鑫(ben)                                           *
  9. *【版    權(quán)】:國(guó)家創(chuàng)新性實(shí)驗(yàn)項(xiàng)目,編號(hào)GCS07001                    *
  10. *******************************************************************/

  11. #ifndef _SHT11_08_07_5_
  12. #define _SHT11 _08_07_5_


  13. /***************************頭文件部分*****************************/
  14. #include <reg52.h>

  15. #define ACK 1
  16. #define noACK 0
  17. #define measure_temp 0x03 //測(cè)量溫度命令
  18. #define measure_humi 0x05 //測(cè)量濕度命令
  19. #define RESET        0x1e //軟啟動(dòng)

  20. #define     uchar    unsigned   char
  21. #define     uint     unsigned   int
  22. #define     ulong    unsigned   long

  23. //-------------------------管腳定義--------------------------------
  24. sbit   DATA=P1^0;
  25. sbit   SCK=P1^1;


  26. //------------------------數(shù)據(jù)結(jié)構(gòu)體定義---------------------------
  27. typedef union                      //保存所測(cè)得的溫度&濕度值
  28. { uint  i;
  29.   float f;
  30. } value;

  31. typedef struct __WENSHIDU__
  32. {
  33.         uchar gewei;
  34.         uchar shiwei;
  35.         uchar DateString1[5];
  36.     uchar DateString2[6];
  37. }WENSHIDU;

  38. /***************************函數(shù)聲明*******************************/
  39. char write_byte(unsigned char value);           // write a byte and checks the ack signal
  40. char read_byte(unsigned char ack);                //read a byte and checks the ack signal
  41. void transstart(void);
  42. void connectionreset(void);
  43. char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
  44. void calc_sth11(float *p_humidity ,float *p_temperature);
  45. void DateToStr(WENSHIDU *Time,float datax,float datax1);
  46. void call_sht11(void);                    
  47. void sht11_window(void);                      //溫濕度界面顯示

  48. //--------- write a byte and checks the ack signal-----------------
  49. char write_byte(uchar value)
  50. {
  51.   uchar i,error=0;  
  52.   for (i=0x80;i>0;i/=2)             /*連續(xù)向右移動(dòng)8位*/
  53.   {
  54.     if (i & value) DATA=1;          /*把相應(yīng)的位送數(shù)據(jù)線*/
  55.     else DATA=0;                        
  56.     SCK=1;                          /*時(shí)序脈沖,應(yīng)嚴(yán)格按著此標(biāo)準(zhǔn)*/
  57.     _nop_();
  58.     _nop_();
  59.     _nop_();                        /*大約0.5us*/       
  60.     SCK=0;
  61.   }
  62.   DATA=1;                           /*釋放數(shù)據(jù)線*/
  63.   SCK=1;                            /*第9位作為響應(yīng)位*/
  64.   error=DATA;                       /*檢測(cè)響應(yīng)情況,如果有響應(yīng)數(shù)據(jù)線就會(huì)被SHT10拉低*/
  65.   SCK=0;        
  66.   return error;                     /*返回1表示沒響應(yīng)*/
  67. }

  68. //--------- read a byte and checks the ack signal-----------------
  69. char read_byte(uchar ack)
  70. {
  71.   uchar i,val=0;
  72.   DATA=1;                           /*釋放數(shù)據(jù)線*/
  73.   for (i=0x80;i>0;i/=2)             /*連續(xù)向右移動(dòng)8位*/
  74.   {
  75.     SCK=1;                          /*clk for SENSI-BUS*/
  76.     if (DATA) val=(val | i);        /*read bit */
  77.     SCK=0;                                           
  78.   }
  79.   DATA=!ack;                        /*當(dāng)ack=1時(shí)拉低數(shù)據(jù)線*/
  80.   SCK=1;                            /*clk #9 for ack*/
  81.   _nop_();_nop_();_nop_();          /*pulswith approx. 5 us */
  82.   SCK=0;                                                    
  83.   DATA=1;                           /*釋放數(shù)據(jù)線*/
  84.   return val;
  85. }


  86. /*******************************************************************
  87. *【函 數(shù) 名】:transstart                                          *
  88. *【修改日期】:2008年7月5日                                        *
  89. *【函數(shù)作用】:傳輸控制,發(fā)出傳輸開始命令                           *
  90. *------------------------------------------------------------------*
  91. *【備    注】:                                                    *
  92. *******************************************************************/
  93. void transstart(void)
  94. {
  95.     DATA=1;
  96.     SCK=0;                   //初始狀態(tài)
  97.     _nop_();
  98.     SCK=1;
  99.     _nop_();
  100.     DATA=0;
  101.     _nop_();
  102.     SCK=0;
  103.     _nop_();_nop_();_nop_();
  104.     SCK=1;
  105.     _nop_();
  106.     DATA=1;
  107.         _nop_();
  108.     SCK=0;
  109. }



  110. /*******************************************************************
  111. *【函 數(shù) 名】:connectionreset                                     *
  112. *【修改日期】:2008年7月5日                                        *
  113. *【函數(shù)作用】:復(fù)位:當(dāng)DATA線處于高低平時(shí),觸發(fā)SCK9次以上(含9次)  *
  114. *              此后應(yīng)接發(fā)一個(gè)"傳輸開始"命令                        *
  115. *------------------------------------------------------------------*
  116. *【備    注】:                                                    *
  117. *******************************************************************/
  118. void connectionreset(void)
  119. {  
  120.   unsigned char i;
  121.   DATA=1; SCK=0;                    //Initial state
  122.   for(i=0;i<9;i++)                  //9 SCK cycles
  123.   {
  124.     SCK=1;
  125.     SCK=0;
  126.   }
  127.   transstart();                   //transmission start
  128. }


  129. //-------makes a measurement (humidity/temperature) with checksum-----
  130. char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
  131. {
  132.   unsigned error=0;
  133.   uint i;

  134.   transstart();                   //transmission start
  135.   switch(mode){                     //send command to sensor*/
  136.     case 0        : error+=write_byte(measure_temp); break;
  137.     case 1        : error+=write_byte(measure_humi); break;
  138.     default     : break;         
  139.   }
  140.   for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement
  141.   if(DATA) error+=1;                       //or timeout (~2 sec.) is reached
  142.   *(p_value)  =read_byte(ACK);           //read the first byte (MSB)
  143.   *(p_value+1)=read_byte(ACK);           //read the second byte (LSB)
  144.   *p_checksum =read_byte(noACK);         //read checksum
  145.   return error;
  146. }

  147. //---------------溫濕度值標(biāo)度變換及溫度補(bǔ)償-------
  148. void calc_sth11(float *p_humidity ,float *p_temperature)      
  149. {
  150.   const float C1=-4.0;              //for 12 Bit
  151.   const float C2=+0.0405;           //for 12 Bit
  152.   const float C3=-0.0000028;        //for 12 Bit
  153.   const float T1=+0.01;             //for 14 Bit @ 5V
  154.   const float T2=+0.00008;           //for 14 Bit @ 5V        

  155.   float rh=*p_humidity;             //rh:      Humidity [Ticks] 12 Bit
  156.   float t=*p_temperature;           //*t:       Temperature [Ticks] 14 Bit
  157.   float rh_lin;                     //rh_lin:  Humidity linear
  158.   float rh_true;                    //rh_true: Temperature compensated humidity
  159.   float t_C;                        //t_C   :  Temperature [癈]

  160.   t_C=t*0.01 - 40;                     //calc. temperature from ticks to [癈]
  161.   rh_lin=C3*rh*rh + C2*rh + C1;        //calc. humidity from ticks to [%RH]
  162.   rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;  //calc. temperature compensated humidity [%RH]
  163.   if(rh_true>100)rh_true=100;          //cut if the value is outside of
  164.   if(rh_true<0.1)rh_true=0.1;          //the physical possible range

  165.   *p_temperature=t_C;                  //return temperature [癈]
  166.   *p_humidity=rh_true;                 //return humidity[%RH]
  167. }


  168. /*******************************************************************
  169. *【函 數(shù) 名】:DateToStr                                           *
  170. *【修改日期】:2008年7月5日                                        *
  171. *【函數(shù)作用】:數(shù)值轉(zhuǎn)換,供顯示使用                                *
  172. *------------------------------------------------------------------*
  173. *【備    注】:                                                    *
  174. *******************************************************************/
  175. void DateToStr(WENSHIDU *Time,float datax,float datax1)
  176. {
  177.         uint i;
  178.     i=(int)datax;
  179.         Time->shiwei=i/10;
  180.         Time->gewei =i%10;
  181.     Time->DateString1[0] = Time->shiwei + '0';
  182.         Time->DateString1[1] = Time->gewei + '0';
  183.         i=(int)((datax-i)*10);
  184.         Time->DateString1[2] ='.';
  185.         Time->DateString1[3] = i + '0';
  186.         Time->DateString1[4] = '\0';

  187.     i=(int)datax1;
  188.         Time->shiwei=i/10;
  189.         Time->gewei =i%10;
  190.     Time->DateString2[0] = Time->shiwei + '0';
  191.         Time->DateString2[1] = Time->gewei + '0';
  192.         i=(int)((datax1-i)*10);
  193.         Time->DateString2[2] ='.';
  194.         Time->DateString2[3] = i + '0';
  195.         Time->DateString2[4] = '%';
  196.         Time->DateString2[5] = '\0';
  197. }


  198. /*******************************************************************
  199. *【函 數(shù) 名】:call_sht11                                          *
  200. *【修改日期】:2008年7月5日                                        *
  201. *【函數(shù)作用】:sht11驅(qū)動(dòng)調(diào)用,調(diào)用即可完成一次測(cè)量                 *
  202. *------------------------------------------------------------------*
  203. *【備    注】:                                                    *
  204. *******************************************************************/
  205. void call_sht11(void)
  206. {
  207.    WENSHIDU S;
  208.    value humi_val,temp_val;
  209.    unsigned char error,checksum;
  210.    unsigned int i;

  211.       error=0;
  212.       error+=s_measure((unsigned char*) &humi_val.i,&checksum,1);   /*measure humidity*/
  213.       error+=s_measure((unsigned char*) &temp_val.i,&checksum,0);   /*measure temperature*/
  214.       if(error!=0)
  215.              connectionreset();                                /*in case of an error:connection reset*/
  216.       else
  217.       {
  218.          humi_val.f=(float)humi_val.i;                     /*converts integer to float*/
  219.          temp_val.f=(float)temp_val.i;                     /*converts integer to float*/
  220.          calc_sth11(&humi_val.f,&temp_val.f);              /*calculate humidity,temperature*/
  221.          DateToStr(&S,temp_val.f,humi_val.f);                                 
  222.          lcd_setposition(2,5);
  223.          lcd_str_w(S.DateString1);         
  224.          lcd_setposition(3,5);
  225.          lcd_str_w(S.DateString2);
  226.      }
  227.      //----------wait approx. 0.8s to avoid heating up SHT10------------------------------ */      
  228.      for (i=0;i<10000;i++);     //(be sure that the compiler doesn't eliminate this line!)
  229.      //----------------------------------------------------------------------------------- */                    
  230. }

  231. //--------------------溫濕度界面顯示------------------------
  232. void sht11_window(void)
  233. {
  234.         lcd_cmd_w(0x01);
  235.         lcd_setposition(1,1);
  236.         lcd_str_w("溫濕測(cè)量系統(tǒng)");
  237.     lcd_setposition(2,0);
  238.     lcd_str_w("當(dāng)前溫度:");
  239.     lcd_setposition(3,0);
  240.     lcd_str_w("當(dāng)前濕度:");

  241. }


  242. #endif
復(fù)制代碼



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表