元件名称 | 型号 | 数量 | 单片机芯片 | AT89C51 | 1 | 温度传感器 | DS18B20 | 1 | 晶振 | 12MHz | 1 | 电源 | 5V | 1 | 液晶屏 | lcd1602 | 1 | 电容 | 30pF | 2 | 10uF(极性) | 1 | 排阻 | 1K | 1 | 万能板 | —— | 1 | 开关 | —— | 3 | 导线 | —— | 若干 | 电阻 | 1K | 4 | 发光二极管 | 绿色 | 2 | 蜂鸣器 | —— | 1 |
全部程序清单程序代码: #include <reg51.h> #include <intrins.h> #define uchar unsigned char #define uint unsigned int #define delayNOP(){_nop_();_nop_();_nop_();_nop_();} sbit HI_LED = P2^3; sbit LO_LED = P2^6; sbit DQ = P3^3; sbit BEEP = P3^7; sbit LCD_RS = P2^0; sbit LCD_RW = P2^1; sbit LCD_EN = P2^2; sbit K1 = P1^7; sbit K2 = P1^4; sbit K3 = P1^1; uchar code RomCodeStr[]={"--ROMCODE--"}; ucharRomCode[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; uchar code Temp_Disp_Title[] ={"Current Temp : "}; ucharCurrent_Temp_Display_Buffer[]={"TEMP: "}; uchar codeTemperature_Char[8]={0x0c,0x12,0x12,0x0c,0x00,0x00,0x00,0x00}; uchar code Alarm_Temp[]={"ALARM TEMP HiLo"}; uchar Alarm_HI_LO_STR[]={"Hi: Lo: "}; uchar temp_data[2]={0x00,0x00}; uchar temp_alarm[2]={0x00,0x00}; uchar display[5]={0x00,0x00,0x00,0x00,0x00}; uchar display1[3]={0x00,0x00,0x00}; uchar codedf_Table[]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9}; char Alarm_Temp_HL[2]={70,-20}; uchar CurrentT=0; uchar Temp_Value[]={0x00,0x00}; uchar Display_Digit[]={0,0,0,0}; bit HI_Alarm=0,LO_Alarm=0; bit DS18B20_IS_OK=1; uint Time0_Count=0; void DelayXus(int x) { uchari; while(x--) for(i=0;i<200;i++); } bit LCD_Busy_Check() { bitresult; LCD_RS=0;LCD_RW=1;LCD_EN=1;delayNOP(); result=(bit)(P0 & 0x80); LCD_EN=0; returnresult; } void Write_LCD_Command(uchar cmd) { while(LCD_Busy_Check()); LCD_RS=0;LCD_RW=0;LCD_EN=0;_nop_();_nop_(); P0=cmd; delayNOP(); LCD_EN=1;delayNOP();LCD_EN=0; } void Write_LCD_Data(uchar dat) { while(LCD_Busy_Check()); LCD_RS=1;LCD_RW=0;LCD_EN=0; P0=dat;delayNOP(); LCD_EN=1;delayNOP();LCD_EN=0; } void LCD_Initialise() { Write_LCD_Command(0x01); DelayXus(5); Write_LCD_Command(0x38); DelayXus(5); Write_LCD_Command(0x0c); DelayXus(5); Write_LCD_Command(0x06); DelayXus(5); } void Set_LCD_POS(uchar pos) { Write_LCD_Command(pos | 0x80); } //自定义字符写CGRAM void Write_NEW_LCD_Char() { uchar i; Write_LCD_Command(0x40); for(i=0;i<8;i++) Write_LCD_Data(Temperature_Char); } //延时 void Delay(uint num) { while(--num); } //在LCD上显示当前的温度 void Display_Temperature() { uchar i; uchar t=150; uchar ng=0; charSigned_Current_Temp; if((Temp_Value[1]&0xF8)==0xF8) { Temp_Value[1]=~Temp_Value[1]; Temp_Value[0]=~Temp_Value[0]+1; if(Temp_Value[0]==0x00) Temp_Value[1]++; ng=1; } 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; Current_Temp_Display_Buffer[11]=Display_Digit[0]+'0'; Current_Temp_Display_Buffer[10]='.'; Current_Temp_Display_Buffer[9]=Display_Digit[1]+'0'; Current_Temp_Display_Buffer[8]=Display_Digit[2]+'0'; Current_Temp_Display_Buffer[7]=Display_Digit[3]+'0'; if(Display_Digit[3]==0) Current_Temp_Display_Buffer[7]=' '; if(Display_Digit[2]==0&& Display_Digit[3]==0) Current_Temp_Display_Buffer[8]=' '; if(ng) { if(Current_Temp_Display_Buffer[8]==' ') Current_Temp_Display_Buffer[8]='-' ; else if(Current_Temp_Display_Buffer[7]==' ') Current_Temp_Display_Buffer[7]='-'; else Current_Temp_Display_Buffer[6]='-'; } Set_LCD_POS(0x00); for(i=0;i<16;i++) Write_LCD_Data(Temp_Disp_Title); Set_LCD_POS(0x40); for(i=0;i<16;i++) Write_LCD_Data(Current_Temp_Display_Buffer); Set_LCD_POS(0x4D);Write_LCD_Data(0x00); Set_LCD_POS(0x4E);Write_LCD_Data('C'); } //定时器中断 void_To_INT() interrupt 1 { TH0=-1000 / 256; TL0=-1000 % 256; BEEP=!BEEP; if(++Time0_Count ==400) { Time0_Count=0; if(HI_Alarm) HI_LED=~HI_LED;else HI_LED=0; if(HI_Alarm) LO_LED=~LO_LED;else LO_LED=0; TR0=0; } } //rom的转换与显示 void Display_Rom_Code() { uchar i,t; Set_LCD_POS(0X40); for(i=0;i<8;i++) { t=((RomCode & 0xF0)>> 4); if(t>9) t+=0x37; else t+='0'; Write_LCD_Data(t); t=RomCode & 0x0F; if(t>9) t+=0x37; else t+='0'; Write_LCD_Data(t); } } uchar Init_DS18B20() { ucharstatus; DQ=1;Delay(8);DQ=0;Delay(90);DQ=1;Delay(8); status=DQ; Delay(100); DQ=1; returnstatus; } uchar ReadOneByte() { uchari,dat=0; DQ=1;_nop_(); for(i=0;i<8;i++) { DQ=0;dat>>=1;DQ=1;_nop_();_nop_(); if(DQ) dat |=0x80; Delay(30); DQ=1; } returndat; } WriteOneByte(uchar dat) { uchari; for(i=0;i<8;i++) { DQ=0;DQ=dat & 0x01;Delay(5);DQ=1;dat>>=1; } } void Read_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; } } //读64位序列码 void Read_Rom_Code() { uchari; Init_DS18B20(); WriteOneByte(0x33); for(i=0;i<8;i++) RomCode=ReadOneByte(); } //显示romcode void Display_RomCode() { uchari; Set_LCD_POS(0X00); for(i=0;i<16;i++) Write_LCD_Data(RomCodeStr); Read_Rom_Code(); Display_Rom_Code(); } //显示报警温度 void Disp_Alarm_Temperature() { uchari,ng; ng=0; if(Alarm_Temp_HL[0]<0) { Alarm_Temp_HL[0]=~Alarm_Temp_HL[0]+1; ng=1; } Alarm_HI_LO_STR[4]=Alarm_Temp_HL[0] / 100+'0'; Alarm_HI_LO_STR[5]=Alarm_Temp_HL[0] / 10 % 10+'0'; Alarm_HI_LO_STR[6]=Alarm_Temp_HL[0] % 10+'0'; if(Alarm_HI_LO_STR[4] == '0') Alarm_HI_LO_STR[4]=' '; if(Alarm_HI_LO_STR[4] == ' ' && Alarm_HI_LO_STR[5]=='0') Alarm_HI_LO_STR[5]=' '; if(ng) { if(Alarm_HI_LO_STR[5] == ' ') Alarm_HI_LO_STR[5]='-'; else if(Alarm_HI_LO_STR[4] == ' ') Alarm_HI_LO_STR[4]='-'; else Alarm_HI_LO_STR[3]='-'; } ng=0; if(Alarm_Temp_HL[1]<0) { Alarm_Temp_HL[1]=~Alarm_Temp_HL[1] +1; ng=1; } Alarm_HI_LO_STR[12]=Alarm_Temp_HL[1] / 100+'0'; Alarm_HI_LO_STR[13]=Alarm_Temp_HL[1] / 10 % 10+'0'; Alarm_HI_LO_STR[14]=Alarm_Temp_HL[1] % 10+'0'; if(Alarm_HI_LO_STR[12] == '0') Alarm_HI_LO_STR[12]=' '; if(Alarm_HI_LO_STR[12] == ' ' && Alarm_HI_LO_STR[13]=='0') Alarm_HI_LO_STR[13]=' '; if(ng) { if(Alarm_HI_LO_STR[13] == ' ') Alarm_HI_LO_STR[13]='-'; else if(Alarm_HI_LO_STR[12] == ' ') Alarm_HI_LO_STR[12]='-'; else Alarm_HI_LO_STR[11]='-'; } Set_LCD_POS(0X00); for(i=0;i<16;i++) Write_LCD_Data(Alarm_Temp); Set_LCD_POS(0X40); for(i=0;i<16;i++) Write_LCD_Data(Alarm_HI_LO_STR); } //主函数 void main() { uchar Current_Operation=1; LCD_Initialise(); IE=0X82; TMOD=0X01; TH0=-1000 / 256; TL0=-1000 % 256; TR0=0; HI_LED=0; LO_LED=0; Set_Alarm_Temp_Value(); Read_Temperature(); Delay(50000); Delay(50000); while(1) { if(K1==0) Current_Operation=1; if(K2==0) Current_Operation=2; if(K3==0) Current_Operation=3; switch (Current_Operation) { case 1: Read_Temperature(); if(DS18B20_IS_OK) { if(HI_Alarm==1 || LO_Alarm ==1) TR0=1; else TR0=0; Display_Temperature(); } DelayXus(100); break; case 2: Read_Temperature(); Disp_Alarm_Temperature(); DelayXus(100); break; case 3: Display_RomCode(); DelayXus(50); break; } } }
|