sht10.txt
上传用户:lgjdgy413
上传日期:2015-02-24
资源大小:3k
文件大小:8k
源码类别:

家庭/个人应用

开发平台:

C/C++

  1. 温湿传感器sht10的C程序搞定了,进来坐坐!完美中文注释
  2. 用官方的51的C程序修改的,我的sht10样品还没到,不知道能不能用,如果ndust版主进来,请一定要发表的意见,欢迎大家批评指正!
  3. #include <pic.h>
  4. #include <math.h>
  5. #define DATA    RC7 //定义通讯数据端口
  6. #define DATA_IO TRISC7 //用于设置IO状态
  7. #define SCK    RC6 //定义通讯时钟端口
  8. #define noACK 0       //继续传输数据,用于判断是否结束通讯
  9. #define ACK   1       //结束数据传输;
  10.                             //地址  命令
  11. #define MEASURE_TEMP 0x03   //000   00011
  12. #define MEASURE_HUMI 0x05   //000   00101
  13. void init_uart(void);
  14. void s_connectionreset(void);
  15. void s_transstart(void);
  16. char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
  17. char s_write_byte(unsigned char value);
  18. char s_read_byte(unsigned char ack);
  19. void calc_sth11(float *p_humidity ,float *p_temperature);
  20. float calc_dewpoint(float h,float t);
  21. void delay (unsigned int time);
  22. union
  23. { unsigned int i;
  24.   float f;
  25. }humi_val,temp_val; //定义两个共同体,一个用于湿度,一个用于温度
  26. /*******************************************************************************
  27. 延时 1MS 带参数(int)子程序
  28. *******************************************************************************/
  29. void delay (unsigned int time){
  30. unsigned int a,b;
  31. for(a=0;a<time;a++){
  32.   for(b=0;b<88;b++);
  33. }
  34. }
  35. //----------------------------------------------------------------------------------
  36. void init_uart(void)
  37. //----------------------------------------------------------------------------------
  38. // 端口初始化
  39. {
  40. TRISC7=0;
  41. TRISC6=0;
  42. }
  43. //----------------------------------------------------------------------------------
  44. void s_connectionreset(void)
  45. //----------------------------------------------------------------------------------
  46. // 连接复位;
  47. //       _____________________________________________________         ________
  48. // DATA:                                                      |_______|
  49. //          _    _    _    _    _    _    _    _    _        ___     ___
  50. // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______
  51. {  
  52.   unsigned char i; 
  53.   DATA=1; SCK=0;                    //准备
  54.   for(i=0;i<9;i++)                  //DATA保持高,SCK时钟触发9次,发送启动传输,通迅即复位
  55.   { SCK=1;
  56.     SCK=0;
  57.   }
  58.   s_transstart();                   //启动传输
  59. }
  60. //----------------------------------------------------------------------------------
  61. void s_transstart(void)
  62. //----------------------------------------------------------------------------------
  63. // 启动传输
  64. //       _____         ________
  65. // DATA:      |_______|
  66. //           ___     ___
  67. // SCK : ___|   |___|   |______
  68. {  
  69.    DATA=1; SCK=0;                   
  70.    NOP();
  71.    SCK=1;
  72.    NOP();
  73.    DATA=0;
  74.    NOP();
  75.    SCK=0;  
  76.    NOP();NOP();NOP();
  77.    SCK=1;
  78.    NOP();
  79.    DATA=1;     
  80.    NOP();
  81.    SCK=0;     
  82. }
  83. //----------------------------------------------------------------------------------
  84. char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
  85. //----------------------------------------------------------------------------------
  86. // 进行温度或者湿度转换,由参数mode决定转换内容;
  87.   unsigned error=0;
  88.   unsigned char i;
  89.   s_transstart();                   //启动传输
  90.   switch(mode){
  91.     case 02 : error+=s_write_byte(MEASURE_TEMP); break;
  92.     case 01 : error+=s_write_byte(MEASURE_HUMI); break;
  93.     default : break;  
  94.   }
  95.   for (i=0;i<110;i++){
  96.      delay(2);
  97.   if(DATA==0) break; //等待测量结束;
  98.   }
  99.   if(DATA) error+=1;                // 如果长时间数据线没有拉低,说明测量错误
  100.   *(p_value)  =s_read_byte(ACK);    //读第一个字节,高字节 (MSB)
  101.   *(p_value+1)=s_read_byte(ACK);    //读第二个字节,低字节 (LSB)
  102.   *p_checksum =s_read_byte(noACK);  //read CRC校验码
  103.   return error;
  104. }
  105. //----------------------------------------------------------------------------------
  106. char s_write_byte(unsigned char value)
  107. //----------------------------------------------------------------------------------
  108. // 写字节函数 
  109.   unsigned char i,error=0;  
  110.   for (i=0x80;i>0;i/=2)             //高位为1,循环右移
  111.   { if (i & value) DATA=1;          //和要发送的数相与,结果为发送的位
  112.     else DATA=0;                        
  113.     SCK=1;                          
  114.     NOP();NOP();NOP();        
  115.     SCK=0;
  116.   }
  117.   DATA=1;                           //释放数据线
  118.   DATA_IO=1;
  119.   SCK=1;
  120.   error=DATA;                       //检查应答信号,确认通讯正常
  121.   SCK=0;        
  122.   return error;                     //error=1 通讯错误
  123. }
  124. //----------------------------------------------------------------------------------
  125. char s_read_byte(unsigned char ack)
  126. //----------------------------------------------------------------------------------
  127. // 读数据;
  128.   unsigned char i,val=0;
  129.   DATA_IO=0;
  130.   DATA=1;                           //数据线为高
  131.   DATA_IO=1;
  132.   for (i=0x80;i>0;i/=2)             //右移位
  133.   { SCK=1;
  134.     if (DATA) val=(val | i);        //读数据线的值
  135.     SCK=0;        
  136.   }
  137.   DATA_IO=0;
  138.   DATA=!ack;                          //如果是校验,读取完后结束通讯;
  139.   SCK=1;                           
  140.   NOP();NOP();NOP();         
  141.   SCK=0;          
  142.   DATA=1;                           //释放数据线
  143.   return val;
  144. }
  145. //----------------------------------------------------------------------------------------
  146. void calc_sth11(float *p_humidity ,float *p_temperature)
  147. //----------------------------------------------------------------------------------------
  148. // 补偿及输出温度和相对湿度
  149. { const float C1=-4.0;              // for 12 Bit 湿度修正公式
  150.   const float C2=+0.0405;           // for 12 Bit 湿度修正公式
  151.   const float C3=-0.0000028;        // for 12 Bit 湿度修正公式
  152.   const float T1=+0.01;             // for 14 Bit @ 5V 温度修正公式
  153.   const float T2=+0.00008;           // for 14 Bit @ 5V  温度修正公式
  154.   float rh=*p_humidity;             
  155.   float t=*p_temperature;           
  156.   float rh_lin;                     
  157.   float rh_true;                    
  158.   float t_C;                        
  159.   t_C=t*0.01 - 40;                  //补偿温度
  160.   rh_lin=C3*rh*rh + C2*rh + C1;     //相对湿度非线性补偿
  161.   rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;   //相对湿度对于温度依赖性补偿
  162.   if(rh_true>100)rh_true=100;       //湿度最大修正
  163.   if(rh_true<0.1)rh_true=0.1;       //湿度最小修正
  164.   *p_temperature=t_C;               //返回温度结果
  165.   *p_humidity=rh_true;              //返回湿度结果
  166. }
  167. //--------------------------------------------------------------------
  168. float calc_dewpoint(float h,float t)
  169. //--------------------------------------------------------------------
  170. // 计算绝对湿度值
  171. { float logEx,dew_point;
  172.   logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
  173.   dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
  174.   return dew_point;
  175. }
  176. //----------------------------------------------------------------------------------
  177. void main()
  178. //----------------------------------------------------------------------------------
  179. // 示例程序,完成如下功能
  180. // 1. 复位 
  181. // 2. 湿度的12测量以及温度的14位精度测量
  182. // 3. 补偿及修正温湿度
  183. // 4. 计数并得出绝对湿度
  184. { float dew_point;
  185.   unsigned char error,checksum;
  186. unsigned char HUMI,TEMP;
  187. HUMI=0X01;
  188. TEMP=0X02;
  189.   init_uart();
  190.   s_connectionreset();
  191.   while(1)
  192.   { error=0;
  193.     error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);  //湿度测量
  194.     error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);  //温度测量
  195.     if(error!=0) s_connectionreset();                 //如果发生错误,系统复位
  196.     else
  197.     { humi_val.f=(float)humi_val.i;                   //转换为浮点数
  198.       temp_val.f=(float)temp_val.i;                   //转换为浮点数
  199.       calc_sth11(&humi_val.f,&temp_val.f);            //修正相对湿度及温度
  200.       dew_point=calc_dewpoint(humi_val.f,temp_val.f); //计算绝对湿度值
  201. //      printf("temp:%5.1fC humi:%5.1f%% dew point:%5.1fCn",temp_val.f,humi_val.f,dew_point);
  202.     }
  203.     delay(800);     //等待足够长的时间,以现行下一次转换
  204.   }