#includereg52.H#includeintrins.h#includestdio.h#defineucharunsignedchar#defineuintunsignedint#defineTIME_PER_SEC100//定义系统节拍在晶振为12Mhz是10ms进行一次定时中断#defineCLOCK12000000//定义晶平率#defineMAK_TASK4//最大任务数voidtask0_temperature();voidtask1_timedis();uchartask_delay[MAK_TASK];sbitRS=P1^0;//液晶定义端口sbitRW=P1^1;sbitEN=P2^5;sbitNLED=P1^1;sbitLED=P1^0;#defineRS_CLRRS=0#defineRS_SETRS=1#defineRW_CLRRW=0#defineRW_SETRW=1#defineEN_CLREN=0#defineEN_SETEN=1#defineDataPortP0sbitDQ=P2^2;//ds18b20端口sbitSCK=P3^1;//DS1302管脚定义sbitSDA=P3^0;sbitRST=P1^3;//复位脚#defineRST_CLRRST=0//电平置低#defineRST_SETRST=1//电平置高//双向数据#defineIO_CLRSDA=0//电平置低#defineIO_SETSDA=1//电平置高#defineIO_RSDA//电平读取//时钟信号#defineSCK_CLRSCK=0//时钟信号#defineSCK_SETSCK=1//电平置高#defineds1302_sec_add0x80//秒数据地址#defineds1302_min_add0x82//分数据地址#defineds1302_hr_add0x84//时数据地址#defineds1302_date_add0x86//日数据地址#defineds1302_month_add0x88//月数据地址#defineds1302_day_add0x8a//星期数据地址#defineds1302_year_add0x8c//年数据地址#defineds1302_control_add0x8e//控制数据地址#defineds1302_charger_add0x90#defineds1302_clkburst_add0xbechartime_buf1[8]={20,10,6,5,12,55,00,6};//空年月日时分秒周chartime_buf[8];//空年月日时分秒周ucharcodeshezhi[7][4]={year,mont,date,hour,mint,sec,week};ucharff,miao,fen,shi;//ucharbdataruning=0x00;sbitruning0=runing^0;sbitruning1=runing^1;//菜单sbitruning2=runing^2;sbitruning3=runing^3;sbitruning4=runing^4;sbitruning5=runing^5;//helpsbitruning6=runing^6;//闹铃sbitwenduH=runing^7;//温度上限标志charpress,Hwendu=-22;//bitNnaoLing=1;//闹铃标志ucharnfen,nshi;floattemperature;/*------------------------------------------------这里使用晶振12M,大致延时长度如下T=tx2+5uS------------------------------------------------*/voidDelayUs2x(uchart){while(--t);}/*------------------------------------------------mS延时函数,大致延时1mS------------------------------------------------*/voidDelayMs(uinttt){while(tt--){DelayUs2x(245);DelayUs2x(245);}}/*------------------------------------------------18b20初始化------------------------------------------------*/bitInit_DS18B20(void){bitdat=0;DQ=1;//DQ复位DelayUs2x(5);//稍做延时DQ=0;//单片机将DQ拉低DelayUs2x(200);//精确延时大于480us小于960usDelayUs2x(200);DQ=1;//拉高总线DelayUs2x(50);//15~60us后接收60-240us的存在脉冲dat=DQ;//如果x=0则初始化成功,x=1则初始化失败DelayUs2x(25);//稍作延时返回returndat;}/*------------------------------------------------读取一个字节------------------------------------------------*/ucharReadOneChar(void){uchari=0;uchardat=0;for(i=8;i0;i--){DQ=0;//给脉冲信号dat=1;DQ=1;//给脉冲信号if(DQ)dat|=0x80;DelayUs2x(25);}return(dat);}/*------------------------------------------------写入一个字节------------------------------------------------*/voidWriteOneChar(uchardat){uchari=0;for(i=8;i0;i--){DQ=0;DQ=dat&0x01;DelayUs2x(25);DQ=1;dat=1;}DelayUs2x(25);}/*------------------------------------------------读取温度------------------------------------------------*/uintReadTemperature(void){uchara=0;uintb=0;uintt=0;Init_DS18B20();WriteOneChar(0xCC);//跳过读序号列号的操作WriteOneChar(0x44);//启动温度转换DelayMs(10);Init_DS18B20();WriteOneChar(0xCC);//跳过读序号列号的操作WriteOneChar(0xBE);//读取温度寄存器等(共可读9个寄存器)前两个就是温度a=ReadOneChar();//低位b=ReadOneChar();//高位b=8;t=a+b;return(t);}/*------------------------------------------------写入命令函数------------------------------------------------*/voidLCD_Write_Com(unsignedcharcom){RS_CLR;RW_CLR;EN_SET;DataPort=com;DelayUs2x(15);EN_CLR;}/*------------------------------------------------写入数据函数------------------------------------------------*/voidLCD_Write_Data(unsignedcharData){RS_SET;RW_CLR;EN_SET;DataPort=Data;DelayUs2x(15);EN_CLR;}/*------------------------------------------------清屏函数------------------------------------------------*/voidLCD_Clear(void){LCD_Write_Com(0x01);DelayMs(5);}/*------------------------------------------------写入字符串函数------------------------------------------------*/voidLCD_Write_String(unsignedcharx,unsignedchary,unsignedchar*s){if(y==0){LCD_Write_Com(0x80+x);//表示第一行}else{LCD_Write_Com(0xC0+x);//表示第二行}while(*s){LCD_Write_Data(*s);s++;}}/*------------------------------------------------写入字符函数------------------------------------------------voidLCD_Write_Char(unsignedcharx,unsignedchary,unsignedcharData){if(y==0){LCD_Write_Com(0x80+x);}if(y==1){LCD_Write_Com(0xC0+x);}LCD_Write_Data(Data);}------------------------------------------------初始化函数------------------------------------------------*/voidLCD_Init(void){LCD_Write_Com(0x38);/*显示模式设置*/DelayMs(5);LCD_Write_Com(0x38);DelayMs(5);LCD_Write_Com(0x38);DelayMs(5);LCD_Write_Com(0x38);LCD_Write_Com(0x08);/*显示关闭*/LCD_Write_Com(0x01);/*显示清屏*/LCD_Write_Com(0x06);/*显示光标移动设置*/DelayMs(5);LCD_Write_Com(0x0C);/*显示开及光标设置*/}/*------------------------------------------------定时器0初始化子程序------------------------------------------------*/voidInit_Timer0(void){uchark;for(k=0;kMAK_TASK;k++)task_delay[k]=0;//任务延时清零TMOD|=0x01;//使用模式1,16位定时器,使用|符号可以在使用多个定时器时不受影响TH0=(65536-CLOCK/TIME_PER_SEC/12)/256;//给定初值TL0=(65536-CLOCK/TIME_PER_SEC/12)%256;EA=1;//总中断打开ET0=1;//定时器中断打开TR0=1;//定时器开关打开}/*------------------------------------------------定时器1初始化子程序------------------------------------------------*/voidInit_Timer1(void){TMOD|=0x10;TH1=(