搜索
bottom↓
回复: 3

MSP430 Launchpad SHT10传感器

[复制链接]

出0入0汤圆

发表于 2014-9-29 10:22:39 | 显示全部楼层 |阅读模式
在MSP430 Launchpad(开发板便宜) MSP430G2452上调试 可以使用 可以修改传感器的测量分辨率
  1. //********************************File Information*******************************
  2. //** File Name:        Sht10.c
  3. //** Platform:         MSP430 LaunchPad MSP430G2452
  4. //** System Function:  Sht10 Sensirion Inc driver code
  5. //** Created by:       ding
  6. //** Created date:     2014-09-15
  7. //** Version:          Demo V1.0
  8. //******************************************************************************
  9. //---------------------------------------
  10. //硬件连线-接口定义:
  11. //SCK: -->P1.5 //定义通讯时钟端口
  12. //SDA: -->P1.6 //定义通讯数据端口
  13. //---------------------------------------

  14. #include <msp430g2452.h>//Library
  15. #include <intrinsics.h> //__no_operation();
  16. //#include <math.h>       //Library  
  17. //#include <stdio.h>      //printf();

  18. //宏定义,延时函数,参数为1时相应延时分别为1us和1ms
  19. #define CPU_F               (double)1000000
  20. #define delay_us(x)         __delay_cycles((long)(CPU_F * (double)x/1000000.0))
  21. #define delay_ms(x)         __delay_cycles((long)(CPU_F * (double)x/1000.0))

  22. #define uint unsigned int
  23. #define uchar unsigned char
  24. #define ulong unsigned long
  25.                               //adr command r/w
  26. #define STATUS_REG_W 0x06     //000   0011    0 //写状态寄存器
  27. #define STATUS_REG_R 0x07     //000   0011    1 //读状态寄存器
  28. #define MEASURE_TEMP 0x03     //000   0001    1 //温度测量
  29. #define MEASURE_HUMI 0x05     //000   0010    1 //湿度测量
  30. #define RESET        0x1e     //000   1111    0 //软复位

  31. #define bitselect     0x01    //选择温度与湿度的低位读
  32. #define noACK         0       //没有返回ACK
  33. #define ACK           1       //返回ACK
  34. #define HUMIDITY      2
  35. #define TEMPERATURE   1

  36. #define SCK           BIT5  //P1.5
  37. #define SDA           BIT6  //P1.6
  38. #define SCK_H         P1OUT|=SCK  //高
  39. #define SCK_L         P1OUT&=~SCK //低
  40. #define SDA_H         P1OUT|=SDA  //高
  41. #define SDA_L         P1OUT&=~SDA //低

  42. typedef union  //定义了两个共用体
  43. {
  44. unsigned int i;
  45. float f;
  46. }value;

  47. /**********************************************************************************************************
  48. **Function Name:      S_Init
  49. **Description:        初始化
  50. **Input Parameters:   no
  51. **Output Parameters:  no
  52. **********************************************************************************************************/
  53. void S_Init()
  54. {
  55.   P1SEL&=~(SCK+SDA);
  56.   P1DIR|=SCK;
  57.   P1DIR&=~SDA;
  58. //BCSCTL1=(XT2OFF+RSEL2);    //关闭XT2,1MHz DOC     
  59. //DCOCTL=DCO2;               //设定DCO频率为1MHz
  60. }

  61. /**********************************************************************************************************
  62. **Function Name:      S_Transstart
  63. **Description:        start时序                  
  64. **                    generates a transmission start
  65. **                          _____         ________
  66. **                    DATA:      |_______|
  67. **                              ___     ___
  68. **                    SCK : ___|   |___|   |______
  69. **********************************************************************************************************/
  70. void S_Transstart()
  71. {
  72. P1DIR|=SDA;
  73. SDA_H;SCK_L;
  74. __no_operation();
  75. SCK_H;
  76. __no_operation();
  77. SDA_L;
  78. __no_operation();
  79. SCK_L;
  80. __no_operation();
  81. __no_operation();
  82. __no_operation();
  83. SCK_H;
  84. __no_operation();
  85. SDA_H;
  86. __no_operation();
  87. SCK_L;
  88. P1DIR&=~SDA;
  89. }

  90. /**********************************************************************************************************
  91. **Function Name:      S_WriteByte
  92. **Description:        写函数
  93. **********************************************************************************************************/
  94. char S_WriteByte(unsigned char value)
  95. {
  96. // writes a byte on the Sensibus and checks the acknowledge
  97. unsigned char i,error=0;
  98. P1DIR|=SDA;
  99. for(i=0x80;i>0;i/=2)              //shift bit for masking
  100. {
  101.     if(i&value) SDA_H;            //masking value with i , write to SENSI-BUS
  102.     else SDA_L;                        
  103.       SCK_H;                      //clk for SENSI-BUS
  104.       __no_operation();__no_operation();__no_operation();//pulswith approx. 5 us
  105.       SCK_L;
  106. }
  107. SDA_H;                            //release DATA-line
  108. P1DIR&=~SDA;                      //Change SDA to be input 0:input 1:ouput
  109. SCK_H;                            //clk #9 for ack
  110. error=P1IN;                       //check ack (DATA will be pulled down by SHT11)
  111. error&=SDA;
  112. P1DIR|=SDA;                       //Change SDA to be output 0:input 1:ouput
  113. SCK_L;
  114. if(error)
  115.     return 1;                     //error=1 in case of no acknowledge
  116.     return 0;
  117. }

  118. /**********************************************************************************************************
  119. **Function Name:      S_ReadByte
  120. **Description:        读函数
  121. **Input Parameters:   ack--->reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
  122. **********************************************************************************************************/
  123. char S_ReadByte(unsigned char ack)
  124. {
  125. // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"  
  126. unsigned char i,val=0;
  127. P1DIR|=SDA;                      //Change SDA to be output 0:input 1:ouput
  128. SDA_H;                           //release DATA-line
  129. P1DIR&=~SDA;                     //Change SDA to be input 0:input 1:ouput
  130. for(i=0x80;i>0;i/=2)             //shift bit for masking
  131. {
  132.     SCK_H;                       //clk for SENSI-BUS
  133.     if(P1IN&SDA)                 
  134.       val=(val|i);               //read bit
  135.       SCK_L;      
  136. }
  137. P1DIR|=SDA;                      //Change SDA to be output 0:input 1:ouput
  138. if(ack)                          //in case of "ack==1" pull down DATA-Line
  139.     SDA_L;
  140. else
  141.     SDA_H;                        
  142. SCK_H;                           //clk #9 for ack
  143. __no_operation();__no_operation();__no_operation();//pulswith approx. 5 us
  144. SCK_L;         
  145. SDA_H;                           //release DATA-line
  146. P1DIR&=~SDA;                     //Change SDA to be output 0:input 1:ouput
  147. return val;
  148. }

  149. /**********************************************************************************************************
  150. **Function Name:      S_Connectionreset
  151. **Description:        连接复位函数
  152. **                    communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
  153. **                          _____________________________________________________         ________
  154. **                    DATA:                                                      |_______|
  155. **                             _    _    _    _    _    _    _    _    _        ___     ___
  156. **                    SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______
  157. **********************************************************************************************************/
  158. void S_Connectionreset()
  159. {
  160. unsigned char ClkCnt;
  161. P1DIR|=SDA;                          //Change SDA to be input 0:input 1:ouput
  162. SDA_H;SCK_L;                         //Initial state
  163. for(ClkCnt=0;ClkCnt<9;ClkCnt++)      //9 SCK cycles
  164. {
  165.     SCK_H;
  166.     SCK_L;
  167. }                                
  168. S_Transstart();                      //transmission start
  169. }
  170. /**********************************************************************************************************
  171. **Function Name:      S_Softreset
  172. **Description:        软件复位函数 resets the sensor by a softreset
  173. **********************************************************************************************************/
  174. char S_Softreset()
  175. {
  176. unsigned char error=0;
  177. S_Connectionreset();              //reset communication
  178. error+=S_WriteByte(RESET);        //send RESET-command to sensor
  179. return error;                     //error=1 in case of no response form the sensor
  180. }

  181. /**********************************************************************************************************
  182. **Function Name:      S_WriteStatusReg
  183. **Description:        写状态寄存器
  184. **Input Parameters:   *p_value
  185. **********************************************************************************************************/
  186. char S_WriteStatusReg(unsigned char *p_value)
  187. {
  188. unsigned char error=0;
  189. S_Transstart();                           //transmission start
  190. error+=S_WriteByte(STATUS_REG_W);         //send command to sensor
  191. error+=S_WriteByte(*p_value);             //send value of status register
  192. return error;                             //error>=1 in case of no response form the sensor
  193. }

  194. /**********************************************************************************************************
  195. **Function Name:      S_Mearsure
  196. **Description:        读时序      makes a measurement (humidity/temperature) with checksum
  197. **Input Parameters:   *p_value       ,*p_checknum       ,mode                  
  198. **********************************************************************************************************/
  199. unsigned char S_Measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
  200. {
  201. unsigned error=0;
  202. unsigned int i;

  203. S_Transstart();                   //transmission start
  204. switch(mode)
  205. {                                 //send command to sensor
  206.     case TEMPERATURE: error+=S_WriteByte(MEASURE_TEMP); break;
  207.     case HUMIDITY:    error+=S_WriteByte(MEASURE_HUMI); break;
  208. }
  209. P1DIR&=~SDA;                      //Change SDA to be input 0:input 1:ouput
  210. for(i=0;i<65535;i++) if((P1IN&SDA)==0) break; //wait until sensor has finished the measurement
  211. if(P1IN&SDA) error+=1;            //or timeout (~2 sec.) is reached
  212. *(p_value)=S_ReadByte(ACK);       //read the first byte (MSB) 高8位
  213. *(p_value+1)=S_ReadByte(ACK);     //read the second byte (LSB)低8位
  214. *p_checksum=S_ReadByte(noACK);    //read checksum
  215. return error;
  216. }

  217. /**********************************************************************************************************
  218. **Function Name:      S_Calculate
  219. **Description:        计算
  220. **Input Parameters:   humi [Ticks] (12 bit) 默认
  221. **                    temp [Ticks] (14 bit) 默认                          
  222. **Output Parameters: humi [%RH]
  223. **                    temp [℃]
  224. **********************************************************************************************************/
  225. void S_Calculate(unsigned int *p_humidity ,unsigned int *p_temperature)
  226. //’1’ =  8bit  湿度 / 12bit 温度分辨率   -->可修改分辨率
  227. //’0’ =  12bit 湿度 / 14bit 温度.分辨率  -->默认选择
  228. {
  229. //12bit 湿度 / 14bit 温度.分辨率
  230. const float C1=-2.0468;           // for 12 Bit 湿度转换参数
  231. const float C2=0.0367;           // for 12 Bit 湿度转换参数
  232. const float C3=-0.0000015955;     // for 12 Bit 湿度转换参数
  233. const float D1=-39.6;             // for 14 Bit @ 3V 温度
  234. const float D2=0.01;             // for 14 Bit @ 3V 温度
  235. const float T1=0.01;              // for 12 bit 湿度信号的温度补偿
  236. const float T2=0.00008;           // for 12 bit 湿度信号的温度补偿

  237.   
  238. //8bit  湿度 / 12bit 温度分辨率
  239. /*
  240. const float C1=-2.0468;           // for 8 Bit 湿度转换参数
  241. const float C2=+0.5872;           // for 8 Bit 湿度转换参数
  242. const float C3=-0.00040845;       // for 8 Bit 湿度转换参数
  243. const float D1=-39.6;             // for 12 Bit @ 3V 温度
  244. const float D2=+0.04;             // for 12 Bit @ 3V 温度
  245. const float T1=0.01;              // for 8 bit 湿度信号的温度补偿
  246. const float T2=0.00128;           // for 8 bit 湿度信号的温度补偿
  247. */
  248. float rh=*p_humidity;             // rh: Humidity [Ticks] 12 Bit
  249. float t=*p_temperature;           // t:  Temperature [Ticks] 14 Bit
  250. float rh_lin;                     // rh_lin: Humidity linear
  251. float rh_true;                    // rh_true: Temperature compensated humidity
  252. float t_C;                        // t_C : Temperature [℃]

  253. t_C=t*D2+D1;                          //calc. temperature from ticks to [℃]
  254. rh_lin=C3*rh*rh + C2*rh + C1;         //calc. humidity from ticks to [%RH]
  255. rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;   //calc. temperature compensated humidity [%RH]
  256. if(rh_true>100)rh_true=100;           //cut if the value is outside of
  257. if(rh_true<0.1)rh_true=0.1;           //the physical possible range

  258. *p_temperature = (uint)t_C;             //return temperature [℃]
  259. *p_humidity = (uint)rh_true;            //return humidity[%RH]
  260. }

  261. //************************************************************************************
  262. //**Function Name:      calc_dewpoint
  263. //**Description:        露点
  264. //************************************************************************************
  265. /*
  266. float Calc_Dewpoint(float h,float t)
  267. // calculates dew point
  268. // input:   humidity [%RH], temperature [℃]
  269. // output:  dew point [℃]
  270. { float logEx,dew_point;
  271.   logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
  272.   dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
  273.   return dew_point;
  274. }
  275. */

  276. /**********************************************************************************************************
  277. **Function Name:      主函数
  278. **********************************************************************************************************/
  279. void main()
  280. {
  281. // sample program that shows how to use SHT11 functions
  282. // 1. connection reset
  283. // 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
  284. // 3. calculate humidity [%RH] and temperature [℃]
  285. // 4. calculate dew point [℃]
  286. // 5. print temperature, humidity, dew point
  287.   
  288. value humi_val,temp_val;
  289. //float dew_point;   //露点
  290. unsigned char error,checksum;
  291. //unsigned int temphigh,templow,humilow;
  292. unsigned int temphigh,templow,humihigh,humilow;
  293. //unsigned int Reg_Resolution=0x01;

  294. WDTCTL=WDTPW+WDTHOLD; //Stop watchdog timer

  295. S_Init(); //初始化
  296. S_Connectionreset(); //连接复位
  297. //S_WriteStatusReg((unsigned char *)&Reg_Resolution);//修改测量分辨率:8bit湿度 / 12bit温度
  298. while(1)
  299. {
  300.     error=0;
  301.     error+=S_Measure((unsigned char*) &humi_val.i,&checksum,HUMIDITY);     //measure humidity
  302.     error+=S_Measure((unsigned char*) &temp_val.i,&checksum,TEMPERATURE); //measure temperature
  303.     if(error!=0)
  304.       S_Connectionreset();  //in case of an error: connection reset
  305.     else
  306.     {
  307.       //Note:修改分辨率需要传感器重新上下电才可生效
  308.       //-------------测量分辨率:8bit湿度 / 12bit温度----------------//
  309.       //8bit humidity MSB=0x0000,00000(invalid) LSB=0xmmmm,mmmm(valid)
  310.       //humilow=(humi_val.i&0xff00);
  311.       //humi_val.i=humilow>>8;      //Humidity
  312.       //12bit temperature MSB=0x0000,mmmm( 4bits valid) LSB=0xmmmm,mmmm(valid)
  313.       //temphigh=((temp_val.i&0x0f)<<8);
  314.       //templow=((temp_val.i&0xff00)>>8);
  315.       //temp_val.i=temphigh+templow;//Temperature
  316.       
  317.       //-------------测量分辨率:12bit湿度 / 14bit温度----------------//
  318.       //12bit humidity MSB=0x0000,mmmm(4bits valid) LSB=0xmmmm,mmmm(valid)
  319.       humihigh=((humi_val.i&0x0f)<<8);
  320.       humilow=((humi_val.i&0xff00)>>8);
  321.       humi_val.i=humihigh+humilow;//Humidity
  322.       
  323.       //14bit temperature MSB=0x00mm,mmmm( 6bit valid) LSB=0xmmmm,mmmm(valid)
  324.       temphigh=((temp_val.i&0x3f)<<8);
  325.       templow=((temp_val.i&0xff00)>>8);
  326.       temp_val.i=temphigh+templow;//Temperature
  327.       
  328.       S_Calculate(&humi_val.i,&temp_val.i);           //calculate humidity, temperature
  329.       
  330.       //dew_point=Calc_Dewpoint(humi_val.i,temp_val.i); //calculate dew point
  331.       //printf("temp:%5.1fC humi:%5.1f%% dew point:%5.1fC\n",temp_val.i,humi_val.i,dew_point);
  332.     }   
  333.     //----------wait approx. 0.8s to avoid heating up SHTxx------------------------------      
  334.     for (uint i=0;i<40000;i++);//(be sure that the compiler doesn't eliminate this line!)
  335.     //-----------------------------------------------------------------------------------                       
  336. }
  337. }
复制代码

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出50入0汤圆

发表于 2014-9-30 08:39:28 | 显示全部楼层
mark,谢谢分享

出0入0汤圆

发表于 2014-9-30 09:15:00 | 显示全部楼层
谢谢分享!

出0入0汤圆

发表于 2014-9-30 09:23:27 | 显示全部楼层
谢谢分享
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-3-29 19:55

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

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