温度湿度c语言代码

上传人:我*** 文档编号:132279944 上传时间:2020-05-14 格式:DOCX 页数:14 大小:14.13KB
返回 下载 相关 举报
温度湿度c语言代码_第1页
第1页 / 共14页
温度湿度c语言代码_第2页
第2页 / 共14页
温度湿度c语言代码_第3页
第3页 / 共14页
温度湿度c语言代码_第4页
第4页 / 共14页
温度湿度c语言代码_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《温度湿度c语言代码》由会员分享,可在线阅读,更多相关《温度湿度c语言代码(14页珍藏版)》请在金锄头文库上搜索。

1、#ifndef _TOU_H_/如果没有被编译过#define _TOU_H_/那么标记为编译过#include#include #include #include#define uint unsigned int #define uchar unsigned charenum TEMP,HUMI;sbit DATA = P25;/温湿度传感器位定义sbit SCK = P24;void s_connectionreset(void);/* SHT11函数声明 */char s_measure(unsigned char *p_value, unsigned char *p_checksum,

2、 unsigned char mode);void calc_sth10(float *p_humidity ,float *p_temperature);float calc_dewpoint(float h,float t);#endif/结束#ifndef,如果已经编译过就跳过之间的部分uchar code table1=00:00:00;uchar code table2=00:00:00RH:00.0;sbit shui1=P10;sbit shui2=P11;sbit shi_s1=P12;sbit shi_s2=P13;sbit shi_s3=P14;sbit led=P16;s

3、bit sound=P17;sbit lcden=P22;sbit lcdrw=P21;sbit lcdrs=P20;sbit relay=P23;sbit s1=P27;uchar data disdata4;uchar num,count,s1num;char shi,fen,miao,shi1,fen1,miao1;uint bian,i,j;void delay(uint z) uint x,y; for(x=z;x0;x-) for(y=110;y0;y-);/SHT11程序(SHT11.c):#define noACK 0/继续传输数据,用于判断是否结束通讯#define ACK

4、1 /结束数据传输;/地址 命令 读/写#define STATUS_REG_W 0x06 /000 0011 0#define STATUS_REG_R 0x07 /000 0011 1#define MEASURE_TEMP 0x03 /000 0001 1#define MEASURE_HUMI 0x05 /000 0010 1#define RESET 0x1e /000 1111 0/写字节程序char s_write_byte(unsigned char value) unsigned char i,error=0; for (i=0x80;i0;i=1) /高位为1,循环右移 i

5、f (i&value) DATA=1; /和要发送的数相与,结果为发送的位 else DATA=0; SCK=1; _nop_();_nop_();_nop_(); /延时3us SCK=0;DATA=1; /释放数据线SCK=1; error=DATA; /检查应答信号,确认通讯正常_nop_();_nop_();_nop_();SCK=0; DATA=1;return error; /error=1 通讯错误/读字节程序char s_read_byte(unsigned char ack) unsigned char i,val=0;DATA=1; /释放数据线for(i=0x80;i0;

6、i=1) /高位为1,循环右移 SCK=1; if(DATA) val=(val|i); /读一位数据线的值 SCK=0; DATA=!ack; /如果是校验,读取完后结束通讯;SCK=1; _nop_();_nop_();_nop_(); /延时3us SCK=0; _nop_();_nop_();_nop_(); DATA=1; /释放数据线return val;/启动传输void s_transstart(void) DATA=1; SCK=0; _nop_(); SCK=1; _nop_(); DATA=0; _nop_(); SCK=0; _nop_();_nop_();_nop_(

7、); SCK=1; _nop_(); DATA=1; _nop_(); SCK=0; void s_connectionreset(void) unsigned char i; DATA=1; SCK=0; /准备for(i=0;i9;i+) /DATA保持高,SCK时钟触发9次,发送启动传输,通迅即复位 SCK=1; SCK=0;s_transstart(); /启动传输 /温湿度测量char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)/ 进行温度或者湿度转换,由参数mode

8、决定转换内容; unsigned error=0;unsigned int i;s_transstart(); /启动传输switch(mode) /选择发送命令 case TEMP : error+=s_write_byte(MEASURE_TEMP); break; /测量温度 case HUMI : error+=s_write_byte(MEASURE_HUMI); break; /测量湿度 default : break; for (i=0;i100)rh_true=100; /湿度最大修正if(rh_true0.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 = (0.66077-logEx)*237.3/(logEx-8.16077);return dew_point; typedef union /定义共用同类型 unsigned int i;float f; value;void writ

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 事务文书

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号