【TI 技术资料分享】温湿度传感器SHT1X驱动程序

[复制链接]
1673|1
 楼主| 必胜客A 发表于 2015-3-10 12:06 | 显示全部楼层 |阅读模式
基于MSP430F149单片机的温湿度传感器SHT1X驱动程序
  1.    
  2. #include "msp430x14x.h"            //头文件
  3. #include "SHT1X.h"
  4. #define _nop_() _NOP()

  5. //float iAdResult[MM];

  6. //----------------------------------------------------------------------------------
  7. // modul-var    温湿度传感器SHT10驱动程序开始
  8. //----------------------------------------------------------------------------------


  9. //----------------------------------------------------------------------------------
  10. char s_write_byte(unsigned char value)
  11. //----------------------------------------------------------------------------------
  12. // writes a byte on the Sensibus and checks the acknowledge
  13. {
  14.   unsigned char i,error=0;
  15.   DATA_OutDIR();
  16.   for (i=0x80;i>0;i/=2)             //shift bit for masking
  17.   { if (i & value) Set_DATA();      //DATA=1 masking value with i , write to SENSI-BUS
  18.   else Clear_DATA();              //DATA=0         
  19.     Set_CLK();                      //SCK=1 clk for SENSI-BUS
  20.     _nop_();_nop_();_nop_();        //pulswith approx. 5 us         
  21.     Clear_CLK();                    //SCK=0
  22.   }
  23.   Set_DATA();                       //DATA=1 release DATA-line
  24.   DATA_InDIR();
  25.   Set_CLK();                        //SCK=1  clk #9 for ack
  26.   
  27.   //DATA_InDIR();
  28.   error=P1IN&DATA;                  // error=DATA  check ack (DATA will be pulled down by SHT11)
  29.   DATA_OutDIR();
  30.   Clear_CLK();                      //SCK=0
  31.   if(error)
  32.     return 1;                     
  33.   return 0;                          //error=1 in case of no acknowledge
  34. }

  35. //----------------------------------------------------------------------------------
  36. char s_read_byte(unsigned char ack)
  37. //----------------------------------------------------------------------------------
  38. // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
  39. {
  40.   unsigned char i,val=0;
  41.   DATA_OutDIR();
  42.   Set_DATA();                       //DATA=1  release DATA-line
  43.   DATA_InDIR();
  44.   for (i=0x80;i>0;i/=2)             //shift  bit for masking
  45.   { Set_CLK();                      //SCK=1  clk for SENSI-BUS
  46.    
  47.      if (P1IN&DATA) val=(val | i);   //(DATA) read bit  
  48.    
  49.     Clear_CLK();                      //SCK=0                 
  50.   }
  51.     DATA_OutDIR();
  52.     if(ack) Clear_DATA() ;
  53.     else Set_DATA();             // DATA=!ack;  in case of "ack==1" pull down DATA-Line
  54.   
  55.   Set_CLK();                        //SCK=1 clk #9 for ack
  56.   _nop_();_nop_();_nop_();          //pulswith approx. 5 us
  57.   Clear_CLK();                            //SCK=0       
  58.   Set_DATA();                       //DATA=1 release DATA-line
  59.   DATA_InDIR();
  60.   return val;
  61. }

  62. //----------------------------------------------------------------------------------
  63. void s_transstart(void)
  64. //----------------------------------------------------------------------------------
  65. // generates a transmission start
  66. //       _____         ________
  67. // DATA:      |_______|
  68. //           ___     ___
  69. // SCK : ___|   |___|   |______
  70. {  
  71.    DATA_OutDIR();
  72.    Set_DATA(); Clear_CLK();        //DATA=1 SCK=0 Initial state
  73.    _nop_();
  74.    Set_CLK();                      //SCK=1
  75.    _nop_();
  76.    Clear_DATA();                    //DATA=0
  77.    _nop_();
  78.    Clear_CLK();                     //SCK=0
  79.    _nop_();_nop_();_nop_();
  80.    Set_CLK();                       //SCK=1
  81.    _nop_();
  82.    Set_DATA();                           //DATA=1
  83.    _nop_();
  84.    Clear_CLK();                            //SCK=0
  85.    DATA_InDIR();
  86. }

  87. //----------------------------------------------------------------------------------
  88. void s_connectionreset(void)
  89. //----------------------------------------------------------------------------------
  90. // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
  91. //       _____________________________________________________         ________
  92. // DATA:                                                      |_______|
  93. //          _    _    _    _    _    _    _    _    _        ___     ___
  94. // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______
  95. {  
  96.   unsigned char i;
  97.   DATA_OutDIR();
  98.   Set_DATA();
  99.   Clear_CLK();        //DATA=1 SCK=0 Initial state
  100.   for(i=0;i<9;i++)                  //9 SCK cycles
  101.   { Set_CLK();                      //SCK=1
  102.     Clear_CLK();                    //SCK=0
  103.   }
  104.   s_transstart();                   //transmission start
  105. }

  106. //----------------------------------------------------------------------------------
  107. char s_softreset(void)
  108. //----------------------------------------------------------------------------------
  109. // resets the sensor by a softreset
  110. {
  111.   unsigned char error=0;  
  112.   s_connectionreset();              //reset communication
  113.   error+=s_write_byte(RESET);       //send RESET-command to sensor
  114.   return error;                     //error=1 in case of no response form the sensor
  115. }

  116. //----------------------------------------------------------------------------------
  117. char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
  118. //----------------------------------------------------------------------------------
  119. // reads the status register with checksum (8-bit)
  120. {
  121.   unsigned char error=0;
  122.   s_transstart();                   //transmission start
  123.   error=s_write_byte(STATUS_REG_R); //send command to sensor
  124.   *p_value=s_read_byte(ACK);        //read status register (8-bit)
  125.   *p_checksum=s_read_byte(noACK);   //read checksum (8-bit)  
  126.   return error;                     //error=1 in case of no response form the sensor
  127. }

  128. //----------------------------------------------------------------------------------
  129. char s_write_statusreg(unsigned char *p_value)
  130. //----------------------------------------------------------------------------------
  131. // writes the status register with checksum (8-bit)
  132. {
  133.   unsigned char error=0;
  134.   s_transstart();                   //transmission start
  135.   error+=s_write_byte(STATUS_REG_W);//send command to sensor
  136.   error+=s_write_byte(*p_value);    //send value of status register
  137.   return error;                     //error>=1 in case of no response form the sensor
  138. }
  139.                                                           
  140. //----------------------------------------------------------------------------------
  141. char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
  142. //----------------------------------------------------------------------------------
  143. // makes a measurement (humidity/temperature) with checksum
  144. {
  145.   unsigned error=0;
  146.   unsigned int i;

  147.   s_transstart();                   //transmission start
  148.   switch(mode)
  149.   {                     //send command to sensor
  150.     case TEMP        : error+=s_write_byte(MEASURE_TEMP); break;
  151.     case HUMI        : error+=s_write_byte(MEASURE_HUMI); break;
  152.     default     : break;         
  153.   }
  154.    
  155.   DATA_InDIR();
  156.   for (i=0;i<65535;i++)
  157.     if((P1IN&DATA)==0) break;         //(DATA==0) wait until sensor has finished the measurement
  158.   
  159.   if((P1IN&DATA)!=0) error+=1;      // (DATA)   or timeout (~2 sec.) is reached
  160.   *(p_value)  =s_read_byte(ACK);    //read the first byte (MSB)
  161.   *(p_value+1)=s_read_byte(ACK);    //read the second byte (LSB)
  162.   *p_checksum =s_read_byte(noACK);  //read checksum
  163.   
  164.   return error;
  165. }

  166. /**********************************************************************************************************
  167. **Function Name:      S_Calculate
  168. **Description:        计算
  169. **Input Parameters:   humi [Ticks] (12 bit)
  170. **                    temp [Ticks] (14 bit)                           
  171. **Output Parameters: humi [%RH]
  172. **                    temp [癈]
  173. **********************************************************************************************************/
  174. void S_Calculate(   float *p_humidity ,   float *p_temperature )
  175. {
  176. const float C1=-4.0;              // for 8 Bit
  177. const float C2=+0.648;            // for 8 Bit
  178. const float C3=-0.0000072;        // for 8 Bit
  179. const float D1=-39.6;             // for 12 Bit [url=home.php?mod=space&uid=72445]@[/url] 3V
  180. const float D2=+0.04;             // for 12 Bit @ 3V
  181. const float T1=0.01;              // for 8 bit
  182. const float T2=0.00128;           // for 8 bit

  183. float rh=*p_humidity;             // rh:      Humidity [Ticks] 12 Bit
  184. float t=*p_temperature;           // t:       Temperature [Ticks] 14 Bit
  185. float rh_lin;                     // rh_lin: Humidity linear
  186. float rh_true;                    // rh_true: Temperature compensated humidity
  187. float t_C;                        // t_C   : Temperature [癈]

  188. t_C=t*D2+D1;                      //calc. temperature from ticks to [癈]
  189. rh_lin=C3*rh*rh + C2*rh + C1;     //calc. humidity from ticks to [%RH]
  190. rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;   //calc. temperature compensated humidity [%RH]
  191. if(rh_true>100)rh_true=100;       //cut if the value is outside of
  192. if(rh_true<0.1)rh_true=0.1;       //the physical possible range

  193. *p_temperature=t_C;               //return temperature [癈]
  194. *p_humidity=rh_true;              //return humidity[%RH]

  195. wen=*p_temperature;
  196. shi=*p_humidity;
  197. }
  198. //uchar  cTData[2];        //温度数据,2个字节
  199. //uchar  cHData[2];      //湿度数据,2个字节


  200.   //                 温湿度传感器SHT10驱动程序开始





本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
zmlzz 发表于 2018-4-22 14:55 | 显示全部楼层
你的时钟用的多少MHZ的呢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

55

主题

176

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部