用数码管与DS18B20设计温度报警器

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedintsbitDQ=P3^6;sbitBEEP=P3^7;sbitHI_LED=P1^4;sbitLO_LED=P1^5;bitHI_Alarm=0,LO_Alarm=0;bitDS18B20_IS_OK=1;ucharcodeDSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};//数码管共阴极段码ucharcodedf_Table[]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};//温度小数位对照表ucharCurrentT=0;//温度整数部分ucharDisplay_Digit[]={0,0,0,0};//数码管待显示的各位温度ucharTemp_Value[]={0x00,0x00};//DS0832读取的温度值uintTime0_Count=0;//定时器中断计数charAlarm_Temp_HL[2]={70,-20};/***********延时程序*************/voidDelay(uintx){while(--x);}/**********DS18B20初始化********/ucharInit_DS18B20(){ucharstatus;DQ=1;Delay(8);DQ=0;Delay(90);DQ=1;Delay(8);status=DQ;Delay(100);DQ=1;returnstatus;}/**********DS18B20读字节********/ucharReadOneByte(){uchari,dat=0;DQ=1;_nop_();for(i=0;i8;i++){DQ=0;dat=1;DQ=1;_nop_();_nop_();if(DQ)dat|=0x80;Delay(30);DQ=1;}returndat;}/**********DS18B20写字节********/voidWriteOneByte(uchardat){uchari;for(i=0;i8;i++){DQ=0;DQ=dat&0x01;Delay(5);DQ=1;dat=1;}}/**********从DS18B20读取温度********/voidRead_Temperature(){if(Init_DS18B20()==1)DS18B20_IS_OK=0;else{WriteOneByte(0xCC);WriteOneByte(0x44);Init_DS18B20();WriteOneByte(0xCC);WriteOneByte(0xBE);Temp_Value[0]=ReadOneByte();Temp_Value[1]=ReadOneByte();DS18B20_IS_OK=1;}}/**********给DS18B20设置报警值********/voidSet_Alarm_Temp_Value(){Init_DS18B20();WriteOneByte(0xcc);//跳过序列号WriteOneByte(0x4e);//将设定的温度报警值写入DS18B20WriteOneByte(Alarm_Temp_HL[0]);//写THWriteOneByte(Alarm_Temp_HL[1]);//写TLWriteOneByte(0x7f);//12位精度Init_DS18B20();WriteOneByte(0xcc);//跳过序列号WriteOneByte(0x48);//将温度报警值存入DS18B20}/**********温度值显示******************/voidDisplay_Temperature()//显示温度{uchari;uchart=150;//延时值ucharng=0,np=0;//负数标示,及负号显示位置charSigned_Current_Temp;if((Temp_Value[1]&0xf8)==0xf8)//如果为负数,取反加1;设置负数标示及负号显示位置{Temp_Value[1]=~Temp_Value[1];Temp_Value[0]=~Temp_Value[0]+1;if(Temp_Value[0]==0x00)Temp_Value[1]++;ng=1;np=0xfd;}Display_Digit[0]=df_Table[Temp_Value[0]&0x0f];//查表显示温度小数位CurrentT=((Temp_Value[0]&0xf0)4)|((Temp_Value[1]&0x07)4);Signed_Current_Temp=ng?-CurrentT:CurrentT;HI_Alarm=Signed_Current_Temp=Alarm_Temp_HL[0]?1:0;LO_Alarm=Signed_Current_Temp=Alarm_Temp_HL[1]?1:0;Display_Digit[3]=CurrentT/100;Display_Digit[2]=CurrentT%100/10;Display_Digit[1]=CurrentT%10;if(Display_Digit[3]==0)//高位为0,不现实,负号后移{Display_Digit[3]=10;np=0xfb;if(Display_Digit[2]==0){Display_Digit[2]=10;np=0xf7;}}for(i=0;i30;i++)//刷新数码管显示温度值{P0=0X39;P2=0X7F;Delay(t);P2=0XFF;P0=0X63;P2=0XBF;Delay(t);P2=0XFF;P0=DSY_CODE[Display_Digit[0]];P2=0XDF;Delay(t);P2=0XFF;P0=(DSY_CODE[Display_Digit[1]])|0X80;P2=0XEF;Delay(t);P2=0XFF;P0=DSY_CODE[Display_Digit[2]];P2=0XF7;Delay(t);P2=0XFF;P0=DSY_CODE[Display_Digit[3]];P2=0XFB;Delay(t);P2=0XFF;if(ng){P0=0X40;P2=np;Delay(t);P2=0XFF;}}}/**********定时器0中断,控制报警声音********/voidT0_INT()interrupt1{TH0=-1000/256;TL0=-1000%256;BEEP=!BEEP;if(++Time0_Count==400){Time0_Count=0;if(HI_Alarm)HI_LED=~HI_LED;elseHI_LED=1;if(LO_Alarm)LO_LED=~LO_LED;elseLO_LED=1;}}voidmain(){IE=0X82;TMOD=0X01;//方式1计数TH0=-1000/256;TL0=-1000%256;TR0=0;HI_LED=1;LO_LED=1;Set_Alarm_Temp_Value();Read_Temperature();Delay(50000);Delay(50000);while(1){Read_Temperature();if(DS18B20_IS_OK){if(HI_Alarm==1||LO_Alarm==1)TR0=1;elseTR0=0;Display_Temperature();}else{P0=P2=0X00;}}}

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功