DS18B20传感器温度数码管显示#includereg51.h#include1820twj.h//传感器DS18B20#includeintrins.h//字符操作库sbitg1=P2^7;sbitg2=P2^6;sbitg3=P2^5;sbitg4=P2^4;sbitlv=P2^2;sbitjin=P3^3;//进sbitjia=P3^4;//加sbitjian=P3^5;//减uintshiwen;uintzuigao=30;//最高30度uintzuidi=20;//最低20度//*键盘调节/ucharflag=0;//记录第一个按键次数flag=0正常1最高2最低ucharflag2=0;//记录第四个按键次数flag2=0正常1调节小时2调节分钟uchartableshu[]={0x28,0xeb,0x32,0xa2,0xe1,0xa4,0x24,0xea,0x20,0xe0,0xff,0xf7};uchartableshudi[]={0x08,0xcb,0x12,0x82,0xc1,0x84,0x04,0xca,0x00,0xc0,0xff};voidmain(){ucharTL;//储存暂存器的温度低位ucharTH;//储存暂存器的温度高位uintTN;//储存温度的整数部分ucharTD;//储存温度的小数部分ucharba,sh,ge,di,ba1,sh1,ge1,di1;bitflag1;//判断显示正负0负1正delay1(5);//延时5ms给硬件一点反应时间while(1)//不断检测并显示温度{init();//读温度准备if(flag1==1)dis(ba,sh,ge,di);elsedis(11,sh,ge,di);TL=ReadOneChar();//先读的是温度值低位TH=ReadOneChar();//接着读的是温度值高位if((TH&0xf8)!=0x00)//判断高五位得到温度是负{flag1=0;TL=~TL;//取反TH=~TH;//取反TL=TL+1;//低位加1TN=(TH*256+TL)*0.625;//实际温度值10倍=(TH*256+TL)*0.625,if(TN%160==0)TN=TN+160;sh=TN/100%10;ge=TN/10%10;di=TN%10;dis(ba,sh,ge,di);//显示温度}else//判断高五位得到温度是正{flag1=1;TN=(TH*256+TL)*0.625;//实际温度值10倍=(TH*256+TL)*0.625,//这样前几位是温度的整数部分,最后一位是温度的小数部分ba=TN/1000;sh=TN/100%10;ge=TN/10%10;di=TN%10;dis(ba,sh,ge,di);//显示温度的整数部分}shiwen=TN;biaozhun();tfz();if(flag1==1)dis(ba,sh,ge,di);elsedis(11,sh,ge,di);kongzhi()//温度调整驱动程序}voidkongzhi(){if((shiwenzuigao)&&(shiwenzuidi))lv=0;//控制P2口输出电平elselv=1;}voiddis(ucharba,ucharsh,ucharge,uchardi){g1=0;P0=tableshu[ba];delay1(5);g1=1;P0=tableshu[10];g2=0;P0=tableshu[sh];delay1(5);g2=1;P0=tableshu[10];g3=0;P0=tableshudi[ge];delay1(5);g3=1;P0=tableshudi[10];g4=0;P0=tableshu[di];delay1(5);g4=1;P0=tableshu[10];}voiddisdi(ucharba,ucharsh,ucharge,uchardi){g1=0;P0=tableshudi[ba];delay1(5);g1=1;P0=tableshu[10];g2=0;P0=tableshudi[sh];delay1(5);g2=1;P0=tableshu[10];g3=0;P0=tableshudi[ge];delay1(5);g3=1;P0=tableshudi[10];g4=0;P0=tableshudi[di];delay1(5);g4=1;P0=tableshu[10];}voidbiaozhun(){if(jin==0){delay1(5);if(jin==0){while(!jin);flag++;if(flag==3)flag=0;}}}/*调幅值*/voidtfz(){ucharba,sh,ge,di;if(flag==1){while(1){ba=zuigao/1000;sh=zuigao/100%10;ge=zuigao/10%10;di=zuigao%10;dis(ba,sh,ge,di);if(jia==0)delay1(5);if(jia==0){zuigao=zuigao+10;while(!jia);dis(ba,sh,ge,di);}if(jian==0){zuigao=zuigao-10;while(!jian);dis(11,sh,ge,di);}biaozhun();if(flag!=1)break;}}if(flag==2){while(1){ba=zuidi/1000;sh=zuidi/100%10;ge=zuidi/10%10;di=zuidi%10;dis(ba,sh,ge,di);if(jia==0)delay1(5);if(jia==0){zuidi=zuidi+10;while(!jia);}if(jian==0){zuidi=zuidi-10;while(!jian);}biaozhun();if(flag!=2)break;}}}