资源描述:
《用数码管与DS18B20设计温度报警器.docx》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#include#include#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
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初始化********/uchar
3、Init_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;i<8;i++){DQ=0;dat>>=1;DQ=1;_nop_();_nop_();if(DQ)dat
4、=0x80;Delay(30);DQ=1;}returndat;}/**********DS18B20写字
5、节********/voidWriteOneByte(uchardat){uchari;for(i=0;i<8;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_Valu
6、e[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);//1
7、2位精度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]=~Te
8、mp_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)
9、((Temp_Value[1]&0x