找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4267|回复: 0
打印 上一主题 下一主题
收起左侧

SHT11.h下载 温湿度传感器SHT11的驱动程序,提供了外界调用接口函数

[复制链接]
跳转到指定楼层
楼主


所有文件下载:
温湿度传感器SHT11的驱动程序.rar (44.81 KB, 下载次数: 50)


下面是部分程序的预览:
  1. /*******************************************************************
  2. *【文 件 名】:SHT11.h                                             *
  3. *【建立日期】:7月5日                                        *
  4. *【修改日期】:7月5日                                        *
  5. *【文件作用】:温湿度传感器SHT11的驱动程序,提供了外界调用接口函数 *
  6. *【说    明】:          *
  7. *------------------------------------------------------------------*
  8. *【作    者】:郭鑫(ben)                                           *
  9. *【版    权】:国家创新性实验项目,编号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 //测量温度命令
  18. #define measure_humi 0x05 //测量湿度命令
  19. #define RESET        0x1e //软启动

  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. //------------------------数据结构体定义---------------------------
  27. typedef union                      //保存所测得的温度&湿度值
  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. /***************************函数声明*******************************/
  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)             /*连续向右移动8位*/
  53.   {
  54.     if (i & value) DATA=1;          /*把相应的位送数据线*/
  55.     else DATA=0;                        
  56.     SCK=1;                          /*时序脉冲,应严格按着此标准*/
  57.     _nop_();
  58.     _nop_();
  59.     _nop_();                        /*大约0.5us*/       
  60.     SCK=0;
  61.   }
  62.   DATA=1;                           /*释放数据线*/
  63.   SCK=1;                            /*第9位作为响应位*/
  64.   error=DATA;                       /*检测响应情况,如果有响应数据线就会被SHT10拉低*/
  65.   SCK=0;        
  66.   return error;                     /*返回1表示没响应*/
  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;                           /*释放数据线*/
  73.   for (i=0x80;i>0;i/=2)             /*连续向右移动8位*/
  74.   {
  75.     SCK=1;                          /*clk for SENSI-BUS*/
  76.     if (DATA) val=(val | i);        /*read bit */
  77.     SCK=0;                                           
  78.   }
  79.   DATA=!ack;                        /*当ack=1时拉低数据线*/
  80.   SCK=1;                            /*clk #9 for ack*/
  81.   _nop_();_nop_();_nop_();          /*pulswith approx. 5 us */
  82.   SCK=0;                                                    
  83.   DATA=1;                           /*释放数据线*/
  84.   return val;
  85. }


  86. /*******************************************************************
  87. *【函 数 名】:transstart                                          *
  88. *【修改日期】:2008年7月5日                                        *
  89. *【函数作用】:传输控制,发出传输开始命令                           *
  90. *------------------------------------------------------------------*
  91. *【备    注】:                                                    *
  92. *******************************************************************/
  93. void transstart(void)
  94. {
  95.     DATA=1;
  96.     SCK=0;                   //初始状态
  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. *【函 数 名】:connectionreset                                     *
  112. *【修改日期】:2008年7月5日                                        *
  113. *【函数作用】:复位:当DATA线处于高低平时,触发SCK9次以上(含9次)  *
  114. *              此后应接发一个"传输开始"命令                        *
  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. //---------------温湿度值标度变换及温度补偿-------
  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. *【函 数 名】:DateToStr                                           *
  170. *【修改日期】:2008年7月5日                                        *
  171. *【函数作用】:数值转换,供显示使用                                *
  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. *【函 数 名】:call_sht11                                          *
  200. *【修改日期】:2008年7月5日                                        *
  201. *【函数作用】:sht11驱动调用,调用即可完成一次测量                 *
  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("温湿测量系统");
  237.     lcd_setposition(2,0);
  238.     lcd_str_w("当前温度:");
  239.     lcd_setposition(3,0);
  240.     lcd_str_w("当前湿度:");

  241. }


  242. #endif
复制代码



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩

相关帖子

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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