sht10.txt
资源名称:sht10.rar [点击查看]
上传用户:lgjdgy413
上传日期:2015-02-24
资源大小:3k
文件大小:8k
源码类别:
家庭/个人应用
开发平台:
C/C++
- 温湿传感器sht10的C程序搞定了,进来坐坐!完美中文注释
- 用官方的51的C程序修改的,我的sht10样品还没到,不知道能不能用,如果ndust版主进来,请一定要发表的意见,欢迎大家批评指正!
- #include <pic.h>
- #include <math.h>
- #define DATA RC7 //定义通讯数据端口
- #define DATA_IO TRISC7 //用于设置IO状态
- #define SCK RC6 //定义通讯时钟端口
- #define noACK 0 //继续传输数据,用于判断是否结束通讯
- #define ACK 1 //结束数据传输;
- //地址 命令
- #define MEASURE_TEMP 0x03 //000 00011
- #define MEASURE_HUMI 0x05 //000 00101
- void init_uart(void);
- void s_connectionreset(void);
- void s_transstart(void);
- char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
- char s_write_byte(unsigned char value);
- char s_read_byte(unsigned char ack);
- void calc_sth11(float *p_humidity ,float *p_temperature);
- float calc_dewpoint(float h,float t);
- void delay (unsigned int time);
- union
- { unsigned int i;
- float f;
- }humi_val,temp_val; //定义两个共同体,一个用于湿度,一个用于温度
- /*******************************************************************************
- 延时 1MS 带参数(int)子程序
- *******************************************************************************/
- void delay (unsigned int time){
- unsigned int a,b;
- for(a=0;a<time;a++){
- for(b=0;b<88;b++);
- }
- }
- //----------------------------------------------------------------------------------
- void init_uart(void)
- //----------------------------------------------------------------------------------
- // 端口初始化
- {
- TRISC7=0;
- TRISC6=0;
- }
- //----------------------------------------------------------------------------------
- void s_connectionreset(void)
- //----------------------------------------------------------------------------------
- // 连接复位;
- // _____________________________________________________ ________
- // DATA: |_______|
- // _ _ _ _ _ _ _ _ _ ___ ___
- // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
- {
- unsigned char i;
- DATA=1; SCK=0; //准备
- for(i=0;i<9;i++) //DATA保持高,SCK时钟触发9次,发送启动传输,通迅即复位
- { SCK=1;
- SCK=0;
- }
- s_transstart(); //启动传输
- }
- //----------------------------------------------------------------------------------
- void s_transstart(void)
- //----------------------------------------------------------------------------------
- // 启动传输
- // _____ ________
- // DATA: |_______|
- // ___ ___
- // SCK : ___| |___| |______
- {
- DATA=1; SCK=0;
- NOP();
- SCK=1;
- NOP();
- DATA=0;
- NOP();
- SCK=0;
- NOP();NOP();NOP();
- SCK=1;
- NOP();
- DATA=1;
- NOP();
- SCK=0;
- }
- //----------------------------------------------------------------------------------
- char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
- //----------------------------------------------------------------------------------
- // 进行温度或者湿度转换,由参数mode决定转换内容;
- {
- unsigned error=0;
- unsigned char i;
- s_transstart(); //启动传输
- switch(mode){
- case 02 : error+=s_write_byte(MEASURE_TEMP); break;
- case 01 : error+=s_write_byte(MEASURE_HUMI); break;
- default : break;
- }
- for (i=0;i<110;i++){
- delay(2);
- if(DATA==0) break; //等待测量结束;
- }
- if(DATA) error+=1; // 如果长时间数据线没有拉低,说明测量错误
- *(p_value) =s_read_byte(ACK); //读第一个字节,高字节 (MSB)
- *(p_value+1)=s_read_byte(ACK); //读第二个字节,低字节 (LSB)
- *p_checksum =s_read_byte(noACK); //read CRC校验码
- return error;
- }
- //----------------------------------------------------------------------------------
- char s_write_byte(unsigned char value)
- //----------------------------------------------------------------------------------
- // 写字节函数
- {
- unsigned char i,error=0;
- for (i=0x80;i>0;i/=2) //高位为1,循环右移
- { if (i & value) DATA=1; //和要发送的数相与,结果为发送的位
- else DATA=0;
- SCK=1;
- NOP();NOP();NOP();
- SCK=0;
- }
- DATA=1; //释放数据线
- DATA_IO=1;
- SCK=1;
- error=DATA; //检查应答信号,确认通讯正常
- SCK=0;
- return error; //error=1 通讯错误
- }
- //----------------------------------------------------------------------------------
- char s_read_byte(unsigned char ack)
- //----------------------------------------------------------------------------------
- // 读数据;
- {
- unsigned char i,val=0;
- DATA_IO=0;
- DATA=1; //数据线为高
- DATA_IO=1;
- for (i=0x80;i>0;i/=2) //右移位
- { SCK=1;
- if (DATA) val=(val | i); //读数据线的值
- SCK=0;
- }
- DATA_IO=0;
- DATA=!ack; //如果是校验,读取完后结束通讯;
- SCK=1;
- NOP();NOP();NOP();
- SCK=0;
- DATA=1; //释放数据线
- return val;
- }
- //----------------------------------------------------------------------------------------
- void calc_sth11(float *p_humidity ,float *p_temperature)
- //----------------------------------------------------------------------------------------
- // 补偿及输出温度和相对湿度
- { const float C1=-4.0; // for 12 Bit 湿度修正公式
- const float C2=+0.0405; // for 12 Bit 湿度修正公式
- const float C3=-0.0000028; // for 12 Bit 湿度修正公式
- const float T1=+0.01; // for 14 Bit @ 5V 温度修正公式
- const float T2=+0.00008; // for 14 Bit @ 5V 温度修正公式
- float rh=*p_humidity;
- float t=*p_temperature;
- float rh_lin;
- float rh_true;
- float t_C;
- t_C=t*0.01 - 40; //补偿温度
- rh_lin=C3*rh*rh + C2*rh + C1; //相对湿度非线性补偿
- rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //相对湿度对于温度依赖性补偿
- if(rh_true>100)rh_true=100; //湿度最大修正
- if(rh_true<0.1)rh_true=0.1; //湿度最小修正
- *p_temperature=t_C; //返回温度结果
- *p_humidity=rh_true; //返回湿度结果
- }
- //--------------------------------------------------------------------
- float calc_dewpoint(float h,float t)
- //--------------------------------------------------------------------
- // 计算绝对湿度值
- { float logEx,dew_point;
- logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
- dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
- return dew_point;
- }
- //----------------------------------------------------------------------------------
- void main()
- //----------------------------------------------------------------------------------
- // 示例程序,完成如下功能
- // 1. 复位
- // 2. 湿度的12测量以及温度的14位精度测量
- // 3. 补偿及修正温湿度
- // 4. 计数并得出绝对湿度
- { float dew_point;
- unsigned char error,checksum;
- unsigned char HUMI,TEMP;
- HUMI=0X01;
- TEMP=0X02;
- init_uart();
- s_connectionreset();
- while(1)
- { error=0;
- error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //湿度测量
- error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //温度测量
- if(error!=0) s_connectionreset(); //如果发生错误,系统复位
- else
- { humi_val.f=(float)humi_val.i; //转换为浮点数
- temp_val.f=(float)temp_val.i; //转换为浮点数
- calc_sth11(&humi_val.f,&temp_val.f); //修正相对湿度及温度
- dew_point=calc_dewpoint(humi_val.f,temp_val.f); //计算绝对湿度值
- // printf("temp:%5.1fC humi:%5.1f%% dew point:%5.1fCn",temp_val.f,humi_val.f,dew_point);
- }
- delay(800); //等待足够长的时间,以现行下一次转换
- }
- }