#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedintsbitDQ=P3^3;//定义DS18B20端口DQsbitBEEP=P3^7;//蜂鸣器驱动线bitpresence,flag;bitcompare_th,compare_tl,alarm_on_off=0,temp_th,temp_tl;sbitLCD_RS=P2^0;sbitLCD_RW=P2^1;sbitLCD_EN=P2^2;sbitK1=P1^4;sbitK2=P1^5;sbitK3=P1^6;sbitK4=P1^7;ucharcodecdis1[]={READ_ROMCORD};ucharcodecdis2[]={};ucharcodecdis3[]={DS18B20ERROR};ucharcodecdis4[]={PLEASECHECK};ucharcodecdis5[]={TEMP:};ucharcodecdis6[]={TH:TL:};unsignedchardatatemp_data[2]={0x00,0x00};unsignedchardatatemp_alarm[2]={0x00,0x00};unsignedchardatadisplay[5]={0x00,0x00,0x00,0x00,0x00};//温度值显示unsignedchardatadisplay1[3]={0x00,0x00,0x00};//温度报警值显示//unsignedcharcodeditab[16]={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,//0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};unsignedchardataRomCode[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};unsignedcharcodemytab1[8]={0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00};unsignedcharcodemytab2[8]={0x01,0x1b,0x1d,0x19,0x1d,0x1b,0x01,0x00};//小喇叭#definedelayNOP();{_nop_();_nop_();_nop_();_nop_();};unsignedcharTemp,temp_comp,timecount,count;unsignedcharcrc;voidDisp_Temp_alarm(ucharaddr,ucharnum);voidspk(ucharaddr);voidset_temp_alarm();voidtemp_compare();voidbeep();/*******************************************************************/voiddelay1(intms){unsignedchary;while(ms--){for(y=0;y250;y++){_nop_();_nop_();_nop_();_nop_();}}}/******************************************************************//**//*检查LCD忙状态*//*lcd_busy为1时,忙,等待。lcd-busy为0时,闲,可写指令与数据。*//**//******************************************************************/bitlcd_busy(){bitresult;LCD_RS=0;LCD_RW=1;LCD_EN=1;delayNOP();result=(bit)(P0&0x80);LCD_EN=0;return(result);}/*******************************************************************//**//*写指令数据到LCD*//*RS=L,RW=L,E=高脉冲,D0-D7=指令码。*//**//*******************************************************************/voidlcd_wcmd(ucharcmd){while(lcd_busy());LCD_RS=0;LCD_RW=0;LCD_EN=0;_nop_();_nop_();P0=cmd;delayNOP();LCD_EN=1;delayNOP();LCD_EN=0;}/*******************************************************************//**//*写显示数据到LCD*//*RS=H,RW=L,E=高脉冲,D0-D7=数据。*//**//*******************************************************************/voidlcd_wdat(uchardat){while(lcd_busy());LCD_RS=1;LCD_RW=0;LCD_EN=0;P0=dat;delayNOP();LCD_EN=1;delayNOP();LCD_EN=0;}/*******************************************************************//**//*自定义字符写入CGRAM*//**//*******************************************************************/voidwritetab(){unsignedchari;lcd_wcmd(0x40);//写CGRAMfor(i=0;i8;i++)lcd_wdat(mytab1[i]);for(i=0;i8;i++)lcd_wdat(mytab2[i]);}/*******************************************************************//**//*LCD初始化设定*//**//*******************************************************************/voidlcd_init(){delay1(15);lcd_wcmd(0x01);//清除LCD的显示内容lcd_wcmd(0x38);//16*2显示,5*7点阵,8位数据delay1(5);lcd_wcmd(0x38);delay1(5);lcd_wcmd(0x38);delay1(5);lcd_wcmd(0x0c);//显示开,关光标delay1(5);lcd_wcmd(0x06);//移动光标delay1(5);lcd_wcmd(0x01);//清除LCD的显示内容delay1(5);writetab();//自定义字符写入CGRAM}/*******************************************************************//**//*设定显示位置*//**//*******************************************************************/voidlcd_pos(ucharpos){lcd_wcmd(pos|0x80);//数据指针=80+地址变量}/*******************************************************************//**//*us级延时函数*//**//*******************************************************************/voidDelay(unsignedintnum){while(--num);}/*******************************************************************//**//*初始化ds1820*//**//*******************************************************************/Init_DS18B20(void){DQ=1;//DQ复位Delay(8);//稍做延时DQ=0;//单片机将DQ拉低Delay(90);//精确延时大于480usDQ=1;//拉高总线Delay(8);presence=DQ;//如果=0则初始化成功=1则初始化失败Delay(100);DQ=1;return(presence);//返回信号,0=presence,1=nopresence}/*******************************************************************//**//*读一个字节*//**//*******************************************************************/ReadOneChar(void){unsignedchari=0;unsignedchardat=0;for(i=8;i0;i--){DQ=0;//给脉冲信号dat=1;DQ=1;//给脉冲信号if(DQ)dat|=0x80;Delay(4);}return(dat);}/*******************************************************************//**//*写一个字节*//**//*******************************************************************/WriteOneChar(unsignedchardat){unsignedchari=0;for(i=8;i0;i--){DQ=0;DQ=dat&0x01;Delay(5);DQ=1;dat=1;}}/*******************************************************************//**//*温度报警值写入DS18B20*//**//*******************************************************************/Write_Temperature_alarm(void){Init_DS18B20();WriteOneChar(0xCC);//跳过读序号列号的操作WriteOneChar(0x4e);//将设定的温度报警值写入DS18B20WriteOneChar(temp_alarm[0]);//写THWriteOneChar(temp_alarm[1]);//写TLWriteOneChar(0x7f);//12位精确度Init_DS18B20();WriteOneChar(0xCC);//跳过读序号列号的操作WriteOneChar(0x48);//把暂存器里的温度报警值拷贝到EEROM}/*******************************************************************//**//*读取64位序列码*//**//*******************************************************************/Read_RomCord(void){unsignedcharj;I