温湿度传感器注意:低版本的keil软件不能编译成功,因为无法处理浮点运算。eeuintunsignedintucharunsignedcharnoACK0ACK1STATUS_REG_W0x06STATUS_REG_R0x07MEASURE_TEMP0x03MEASURE_HUMI0x05RESET0x1eenum{TEMP,HUMI};typedefunion//定义共用同类型{unsignedinti;floatf;}value;sbitlcdrs=P2^0;sbitlcdrw=P2^1;sbitlcden=P2^2;sbitSCK=P1^0;sbitDATA=P1^1;uchartable2[]=温湿度检测;uchartable3[]=温度为:℃;uchartable4[]=湿度为:;uchartable5[]=.;ucharwendu[6];ucharshidu[6];voiddelay(intz){intx,y;for(x=z;x0;x--)for(y=125;y0;y--);}voiddelay_50us(uintt){uintj;for(;t0;t--)for(j=19;j0;j--);}voiddelay_50ms(uintt){uintj;for(;t0;t--)for(j=6245;j0;j--);}voidwrite_12864com(ucharcom){lcdrs=0;lcdrw=0;delay_50us(1);P0=com;lcden=1;delay_50us(10);lcden=0;delay_50us(2);}voidwrite_dat(uchardat){lcdrs=1;lcdrw=0;delay_50us(1);P0=dat;lcden=1;delay_50us(10);lcden=0;delay_50us(2);}voidinit12864lcd(void){delay_50ms(2);write_12864com(0x30);delay_50us(4);write_12864com(0x30);delay_50us(4);write_12864com(0x0f);delay_50us(4);write_12864com(0x01);delay_50us(240);write_12864com(0x06);delay_50us(10);write_12864com(0x0c);delay_50us(10);}voiddisplay1(void){uchari;write_12864com(0x80);for(i=0;i18;i++){write_dat(table2[i]);delay_50us(1);}}voiddisplay2(void){uchari;write_12864com(0x90);for(i=0;i18;i++){write_dat(table3[i]);delay_50us(1);}}voiddisplay3(void){uchari;write_12864com(0x88);for(i=0;i8;i++){write_dat(table4[i]);delay_50us(1);}}voiddisplaywendu(void){uchari;write_12864com(0x94);for(i=0;i3;i++){write_dat(wendu[i]);delay_50us(1);}for(i=0;i1;i++){write_dat(table5[i]);delay_50us(1);}for(i=4;i5;i++){write_dat(wendu[i]);delay_50us(1);}}voiddisplayshidu(void){uchari;write_12864com(0x8C);for(i=0;i3;i++){write_dat(shidu[i]);delay_50us(1);}for(i=0;i1;i++){write_dat(table5[i]);delay_50us(1);}for(i=4;i5;i++){write_dat(shidu[i]);delay_50us(1);}}//写字节程序chars_write_byte(unsignedcharvalue){unsignedchari,error=0;for(i=0x80;i0;i=1)//高位为1,循环右移{if(i&value)DATA=1;//和要发送的数相与,结果为发送的位elseDATA=0;SCK=1;_nop_();_nop_();_nop_();//延时3usSCK=0;}DATA=1;//释放数据线SCK=1;error=DATA;//检查应答信号,确认通讯正常_nop_();_nop_();_nop_();SCK=0;DATA=1;returnerror;//error=1通讯错误}//读字节程序chars_read_byte(unsignedcharack){unsignedchari,val=0;DATA=1;//释放数据线for(i=0x80;i0;i=1)//高位为1,循环右移{SCK=1;if(DATA)val=(val|i);//读一位数据线的值SCK=0;}DATA=!ack;//如果是校验,读取完后结束通讯;SCK=1;_nop_();_nop_();_nop_();//延时3usSCK=0;_nop_();_nop_();_nop_();DATA=1;//释放数据线returnval;}//启动传输voids_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;}//连接复位voids_connectionreset(void){unsignedchari;DATA=1;SCK=0;//准备for(i=0;i9;i++)//DATA保持高,SCK时钟触发9次,发送启动传输,通迅即复位{SCK=1;SCK=0;}s_transstart();//启动传输}//软复位程序chars_softreset(void){unsignedcharerror=0;s_connectionreset();//启动连接复位error+=s_write_byte(RESET);//发送复位命令returnerror;//error=1通讯错误}//温湿度测量chars_measure(unsignedchar*p_value,unsignedchar*p_checksum,unsignedcharmode){unsignederror=0;unsignedinti;s_transstart();//启动传输switch(mode)//选择发送命令{caseTEMP:error+=s_write_byte(MEASURE_TEMP);break;//测量温度caseHUMI:error+=s_write_byte(MEASURE_HUMI);break;//测量湿度default:break;}for(i=0;i65535;i++)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);//readCRC校验码returnerror;//error=1通讯错误}//温湿度值标度变换及温度补偿voidcalc_sth10(float*p_humidity,float*p_temperature){constfloatC1=-4.0;//12位湿度精度修正公式constfloatC2=+0.0405;//12位湿度精度修正公式constfloatC3=-0.0000028;//12位湿度精度修正公式constfloatT1=+0.01;//14位温度精度5V条件修正公式constfloatT2=+0.00008;//14位温度精度5V条件修正公式floatrh=*p_humidity;//rh:12位湿度floatt=*p_temperature;//t:14位温度floatrh_lin;//rh_lin:湿度linear值floatrh_true;//rh_true:湿度ture值floatt_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_true100)rh_true=100;//湿度最大修正if(rh_true0.1)rh_true=0.1;//湿度最小修正*p_temperature=t_C;//返回温度结果*p_humidity=rh_true;//返回湿度结果}voidmain(void){unsignedinttemp,humi;valuehumi_val,temp_val;//定义两个共同体,一个用于湿度,一个用于温度unsignedcharerror;//用于检验是否出现错误unsignedcharchecksum;//CRCinit12864lcd();display1();display2();display3();s_connectionreset();//启动连接复位while(1){error=0;//初始化error=0,即没有错误error+=s_measure((unsignedchar*)&temp_val.i,&checksum,TEMP);//温度测量error+=s_measure((unsignedchar*)&humi_val.i,&checksum,HUMI);//湿度测量if(error!=0)s_connectionreset();////如果发生错误,系统复位else{humi_val.f=(float)humi_val.i;//转换为浮点数temp_val.f=(float)temp_val.i;//转换为浮点数calc_sth10(&humi_val.f,&temp_val.f);//修正相对湿度及温度temp=temp_val.f*10;humi=humi_val.f*10;wendu[0]=temp/1000+'0';//温度百位wendu[1]=temp00/100+'0';//温度十位wendu[2]=temp0/10+'0';//温度个位wendu[3]=0x2E;//小数点wendu[4]=temp+'0';//温度小数点后第一位displaywendu();shidu[0]=humi/1000+'0';//湿度百位shidu[1]=humi00/100+'0';//湿度十位shidu[2]=humi0/10+'0';//湿度个位shidu[3]=0x2E;//小数点shidu[4]=humi+'0';//湿度小数点后第一位displayshidu();}delay(800);//等待足够长的时间,以现行下一次转换}}
本网站关键词专注于温湿度变送器 、温湿度传感器、温湿度记录仪、水浸传感器 、断电报警器
本文地址:http://www.sdrkck.com/news/hangye/305.html,欢迎转载。