1、51温度湿度c语言代码#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 *
2、p_checksum, 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;sbi
3、t led=P16;sbit 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 /继续传输数据,用于判断是否结束通讯
4、#define ACK 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)
5、 /高位为1,循环右移 if (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; /
6、释放数据线 for(i=0x80;i0;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;
7、_nop_();_nop_();_nop_(); 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 m
8、ode)/ 进行温度或者湿度转换,由参数mode决定转换内容; 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_tr
9、ue=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; flo
10、at f; value;void write_com(uchar com)/lcd部分 lcdrs=0; lcdrw=0; delay(5); lcden=1; P0=com; delay(5); lcden=0;void write_data(uchar date) lcdrs=1; lcden=1; delay(5); P0=date; delay(5); lcden=0;void lcd_init() lcdrw=0; lcden=0; delay(15); write_com(0x38); delay(5); write_com(0x38); delay(5); write_com(0
11、x38); write_com(0x38); write_com(0x08); write_com(0x01); write_com(0x06); write_com(0x0c); write_com(0x80+1); TMOD=0x11; TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; ET0=1; TR0=1; write_com(0x80+1); for(num=0;num8;num+) write_data(table1num); delay(20); write_com(0x80+0x40+1); for(num=0;num16
12、;num+) write_data(table2num); delay(20); void write_sfm(uchar add,uchar date) uchar shi,ge; shi=date/10; ge=date%10; write_com(0x80+0x40+add); write_data(0x30+shi); write_data(0x30+ge);void write_shedingsfm(uchar add1,uchar date1) uchar shi1,ge1; shi1=date1/10; ge1=date1%10; write_com(0x80+add1); wr
13、ite_data(0x30+shi1); write_data(0x30+ge1);void keyscan() if(shi_s1=0) delay(5); if(shi_s1=0) s1num+; while(!shi_s1); if(s1num=1) write_com(0x80+0x40+8); write_com(0x0f); if(s1num=2) write_com(0x80+0x40+5); write_com(0x0f); if(s1num=3) write_com(0x80+0x40+2); write_com(0x0f); if(s1num=4) TR0=1; write
14、_com(0x80+8); write_com(0x0f); if(s1num=5) write_com(0x80+5); write_com(0x0f); if(s1num=6) write_com(0x80+2); write_com(0x0f); if(s1num=7) s1num=0; write_com(0x0c); if(s1num!=0) if(shi_s2=0) delay(5); if(shi_s2=0) while (!shi_s2); if(s1num=1) miao+; if(miao=60) miao=0; write_sfm(7,miao); write_com(0
15、x80+0x40+8); if(s1num=2) fen+; if(fen=60) fen=0; write_sfm(4,fen); write_com(0x80+0x40+5); if(s1num=3) shi+; if(shi=24) shi=0; write_sfm(1,shi); write_com(0x80+0x40+2); if(s1num=4) TR0=1; miao1+; if(miao1=60) miao1=0; write_shedingsfm(7,miao1); write_com(0x80+8); if(s1num=5) fen1+; if(fen1=60) fen1=
16、0; write_shedingsfm(4,fen1); write_com(0x80+5); if(s1num=6) shi1+; if(shi1=24) shi1=0; write_shedingsfm(1,shi1); write_com(0x80+2); if(shi_s3=0) delay(5); if(shi_s3=0) while (!shi_s3); if(s1num=1) miao-; if(miao=-1) miao=59; write_sfm(7,miao); write_com(0x80+0x40+8); if(s1num=2) fen-; if(fen=-1) fen
17、=59; write_sfm(4,fen); write_com(0x80+0x40+5); if(s1num=3) shi-; if(shi=-1) shi=23; write_sfm(1,shi); write_com(0x80+0x40+2); if(s1num=4) TR0=1; miao1-; if(miao1=-1) miao1=59; write_shedingsfm(7,miao1); write_com(0x80+8); if(s1num=5) fen1-; if(fen1=-1) fen1=59; write_shedingsfm(4,fen1); write_com(0x80+5); if(s1num=6) shi1-; if(shi1=-1) shi1=23; write_shedingsfm(1,shi1); write_com(0x80+2); void baojing()if(shui1=0)led=led;delay(150);for(i=0;ihumi)relay=0; elserelay=1; if(shui2=0)relay=1; void timer0() interrupt 1 TH0=(65536-50000)/256; TL0=(65536-50000)%256; count+;