完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
|
#include #include #include #include #include #include #define uchar unsigned char #define uint unsigned int #define DataPort P0 //LCD接口 #define noACK 0 #define ACK 1 uchar code temp[]={"temp: "}; uchar code tempset[]={"tempset: "}; uchar code hun[]={"hun: "}; uchar code hunset[]={"hunset: "}; uchar code set[]={"step in set!"}; uchar tempset1,s1num,s4num,hunset1,hun1,hun2; float temp1=0; uchar stop=0; uchar xiaoying=0; // stop=0; int t_bai,t_shi,t_ge,t_qian, h_bai,h_shi,h_ge,h_qian; ***it s1=P1^0; ***it s2=P1^1; ***it s3=P1^2; ***it s4=P1^3; ***it s5=P1^4; ***it s6=P1^5; ***it wbj=P2^2; ***it sw=P2^3; ***it jw=P2^4; ***it ***j=P3^5; ***it js=P3^6; ***it zs=P3^7; ***it lcdrs=P2^5; ***it lcdrw=P2^6; ***it lcden=P2^7; ***it DATA = P2^1; ***it SCK = P2^0; void delay(uint z) //延时函数 { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } //SHT11写字节 uchar s_write_byte(uchar value) { uchar i,error=0; for (i=0x80;i>0;i/=2) // //高位为1,循环右移 { if (i & value) // 和要发送的数相与,结果为发送的位 DATA=1; else DATA=0; SCK=1; _nop_(); _nop_(); _nop_(); //延时3us SCK=0; } DATA=1; //释放数据线 SCK=1; error=DATA; //检查应答信号,确认通讯正常 SCK=0; return error; //error=1 通讯错误 } // SHT11读字节程序 uchar s_read_byte(uchar ack) { uchar i,val=0; DATA=1; //释放数据线 for (i=0x80;i>0;i/=2) { SCK=1; if (DATA) val=(val | i); //读一位数据线的值 SCK=0; } DATA=!ack; //如果是校验,读取完后结束通讯 SCK=1; _nop_(); _nop_(); _nop_(); SCK=0; DATA=1; return val; } //SHT11启动传输 void s_transstart(void) { DATA=1; SCK=0; _nop_(); SCK=1; _nop_(); DATA=0; _nop_(); SCK=0; _nop_(); _nop_(); _nop_(); SCK=1; _nop_(); DATA=1; _nop_(); SCK=0; } //连接复位 void s_connectionreset(void) { uchar i; DATA=1; SCK=0; for(i=0;i<9;i++) //dat保持高,SCK时钟触发9次,发送启动传输,通迅即复位 { SCK=1; SCK=0; } s_transstart(); //启动传输 } //SHT11温度检测 uint measure_T() { uchar val_1,val_2; uint tempval,error=0; s_connectionreset(); s_transstart(); error+=s_write_byte(0x03); //测量温度 if(error!=0) s_connectionreset(); else { while(DATA==1) _nop_(); if(DATA==0) { val_1=s_read_byte(ACK); val_2=s_read_byte(noACK); tempval=val_2+val_1*256; //转换成 16 位的 int 型 } else error=1; } return tempval; } float c_T(uint temp_val) //将检测到的数据转化为相应的温度数据 { uchar i; const float d1=-40.0; // 14位温度精度 5V条件 修正公式 const float d2=+0.01; // 14位温度精度 5V条件 修正公式 float temp_final; temp_val=temp_val&0x3fff; //取低14位 temp_final=d1+d2*(float)temp_val; for(i=100;i>0;i--) delay(1); return temp_final; } //SHT11湿度检测 uint measure_H() { uchar val_1,val_2; uint tempval,error=0; s_connectionreset(); s_transstart(); error+=s_write_byte(0x05); //测量温度 if(error!=0) s_connectionreset(); else { while(DATA==1) _nop_(); if(DATA==0) { val_1=s_read_byte(ACK); val_2=s_read_byte(noACK); tempval=val_2+val_1*256; //转换成 16 位的 int 型 } else error=1; } return tempval; } //检测到的数据转化为相应的湿度数据 float c_H(float t,float h) { uchar i; const float C1=-4.0; // 12位湿度精度 修正公式 const float C2=+0.0405; // 12位湿度精度 修正公式 const float C3=-0.0000028; // 12位湿度精度 修正公式 const float T1=+0.01; // 14位温度精度 5V条件 修正公式 const float T2=+0.00008; // 14位温度精度 5V条件 修正公式 float rh=h; float rh_lin; // rh_lin: 湿度 linear值 float rh_true; // rh_true: 湿度 ture值 float t_C; // 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; //湿度最小修正 for(i=100;i>0;i--) delay(1); return rh_true; } //读温度 void read_T(float temperature) { float temp; temp=temperature*100; t_qian=(int)temp/1000; t_bai =(int)temp%1000/100; t_shi =(int)temp%100/10; t_ge =(int)temp%10; } //读湿度 void read_H(float humidity) { float hum; hum=humidity*100; h_qian=(int)hum/1000; h_bai =(int)hum%1000/100; h_shi =(int)hum%100/10; h_ge =(int)hum%10; } /////////////////////////////////////////////////////////// void write_com(uchar com) //lcd写命令函数 { lcdrs=0; lcdrw=0; lcden=0; P0=com; delay(10); //延时 lcden=1; //下三行表示E高脉冲到来就开始转换 delay(10); lcden=0; } void write_data(uchar date) //lcd写数据函数 { lcdrs=1; lcdrs=1; lcdrw=0; lcden=0; P0=date; delay(1); lcden=1; delay(1); lcden=0; } void init() //初始化函数 { s1=1; tempset1=25; hunset1=50; lcdrw=0; lcden=0; write_com(0x38); delay(10); write_com(0x0f); delay(10); write_com(0x06); delay(10); write_com(0x01); delay(10); } void print(uchar a,uchar *str) //输出字符 { write_com(a); while(*str!=' |
