您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 单片机实现基于DS,,B的多功能电子时钟,带测温功能
#includereg51.h#defineucharunsignedchar#defineuintunsignedint/*------------------寄存器地址定义------------*/#defineWRITE_SECOND0x80#defineWRITE_MINUTE0x82#defineWRITE_HOUR0x84#defineWRITE_DAY0x86#defineWRITE_MONTH0x88#defineWRITE_YEAR0x8C#defineWRITE_PROTECT0x8E#defineWRITE_WEEK0x8A#defineWRITE_CURRENT0x90#defineREAD_SECOND0x81#defineREAD_MINUTE0x83#defineREAD_HOUR0x85#defineREAD_DAY0x87#defineREAD_MONTH0x89#defineREAD_WEEK0x8B#defineREAD_YEAR0x8DsbitRST=P3^5;//DS1302片选sbitDIO=P3^4;//DS1302数据信号sbitSCLK=P3^6;//DS1302时钟信号sbitACC7=ACC^7;sbitACC0=ACC^0;unsignedcharcount;sbitbeep=P1^4;voidINITIAL_1302();unsignedcharREAD_1302(unsignedcharaddr);voidWRITE_1302(unsignedcharaddr,unsignedchardate);sbitLCD_E=P2^7;//液晶使能端sbitLCD_RS=P2^6;//液晶数据命令选择端sbitLCD_RW=P2^5;//液晶读写选择端sbitkey0=P3^2;//功能键sbitkey1=P3^3;//增大键sbitkey2=P3^4;//减小键sbitDQ=P3^7;//18b20温度输入输出端charhour,minute,second,week,day,month,year,tem;unsignedcharnum,flag;unsignedcharstr1[]=20--;unsignedcharstr2[]=::C;voiddelay(unsignedcharz);voidLCD_WRITE_COM(unsignedcharcom);voidLCD_WRITE_DATA(unsignedchardate);voidLCD_WRITE_SFM(unsignedcharaddr,unsignedchardate);voidLCD_WRITE_NYR(unsignedcharaddr,unsignedchardate);voidLCD_WRITE_WEEK(unsignedcharweek);voidLCD_INITIAL();voidLCD_WRITE_TEMP(ucharaddr,uchartemp);voidkeyscan();voidINITIAL_TEMER0();ucharTMPH,TMPL;voidw1602c(ucharcom);voidw1602d(uchardata01);voiddelay2(uintms);voiddelay3(uintus);ucharresert(void);ucharread_byte();voidwrite_byte(ucharval);uintget_temp();voiddisplay();voiddelay3(uintus);//18b20ucharresert(void);//18b20ucharread_byte();//18b20voidwrite_byte(ucharval);//18b20uintget_temp();//18b20voidmain(){LCD_INITIAL();//液晶初始化INITIAL_1302();//DS1302初始化INITIAL_TEMER0();//定时器初始化while(1){keyscan();//按键扫描if(flag==0)//当标志位为0,允许读取DS1302数据,将数据送入液晶显示{second=READ_1302(0x81);LCD_WRITE_SFM(10,second);minute=READ_1302(0x83);LCD_WRITE_SFM(7,minute);hour=READ_1302(0x85);LCD_WRITE_SFM(4,hour);week=READ_1302(0x8b);LCD_WRITE_WEEK(week);day=READ_1302(0x87);LCD_WRITE_NYR(9,day);month=READ_1302(0x89);LCD_WRITE_NYR(6,month);year=READ_1302(0x8d);LCD_WRITE_NYR(3,year);tem=get_temp();LCD_WRITE_TEMP(13,tem);}}}/**********************1602BU份******************************************************/voiddelay(unsignedcharz)//1602de延时函数{unsignedcharx,y;for(x=z;x0;x--)for(y=110;y0;y--);}voidLCD_WRITE_COM(unsignedcharcom){LCD_RS=0;LCD_RW=0;LCD_E=1;P0=com;delay(5);LCD_E=0;}voidLCD_WRITE_DATA(unsignedchardate){LCD_RS=1;LCD_RW=0;LCD_E=1;P0=date;delay(5);LCD_E=0;}voidLCD_WRITE_SFM(unsignedcharaddr,unsignedchardate){unsignedcharshi,ge;shi=date/10;ge=date%10;LCD_WRITE_COM(0x80+0x40+addr);LCD_WRITE_DATA(0x30+shi);LCD_WRITE_DATA(0x30+ge);}voidLCD_WRITE_NYR(unsignedcharaddr,unsignedchardate){charshi,ge;shi=date/10;ge=date%10;LCD_WRITE_COM(0x80+addr);LCD_WRITE_DATA(0x30+shi);LCD_WRITE_DATA(0x30+ge);}voidLCD_WRITE_WEEK(unsignedcharweek){LCD_WRITE_COM(0x80+12);switch(week){case1:LCD_WRITE_DATA('M');delay(5);LCD_WRITE_DATA('O');delay(5);;LCD_WRITE_DATA('N');break;case2:LCD_WRITE_DATA('T');delay(5);LCD_WRITE_DATA('U');delay(5);LCD_WRITE_DATA('E');break;case3:LCD_WRITE_DATA('W');delay(5);LCD_WRITE_DATA('E');delay(5);LCD_WRITE_DATA('D');break;case4:LCD_WRITE_DATA('T');delay(5);LCD_WRITE_DATA('H');delay(5);LCD_WRITE_DATA('U');break;case5:LCD_WRITE_DATA('F');delay(5);LCD_WRITE_DATA('R');delay(5);LCD_WRITE_DATA('I');break;case6:LCD_WRITE_DATA('S');delay(5);LCD_WRITE_DATA('A');delay(5);LCD_WRITE_DATA('T');break;case7:LCD_WRITE_DATA('S');delay(5);LCD_WRITE_DATA('U');delay(5);LCD_WRITE_DATA('N');break;}}voidLCD_WRITE_TEMP(ucharaddr,uchartemp){charshi,ge;shi=temp/10;ge=temp%10;LCD_WRITE_COM(0x80+0x40+addr);LCD_WRITE_DATA(0x30+shi);LCD_WRITE_DATA(0x30+ge);}voidLCD_INITIAL(){unsignedchari;LCD_WRITE_COM(0x38);LCD_WRITE_COM(0x0c);LCD_WRITE_COM(0x06);//经本人试验证明1602初始化不清屏也可实现LCD_WRITE_COM(0x80);for(i=0;i16;i++){LCD_WRITE_DATA(str1[i]);delay(5);}LCD_WRITE_COM(0x80+0x40);//液晶第二行显示for(i=0;i16;i++){LCD_WRITE_DATA(str2[i]);delay(5);}}/***************************1302部分***************************************************/voidWRITE_1302(unsignedcharaddr,unsignedchardate){unsignedchari;RST=0;SCLK=0;RST=1;for(i=8;i0;i--){SCLK=0;ACC=addr;DIO=ACC0;addr=1;SCLK=1;}for(i=8;i0;i--){SCLK=0;ACC=date;DIO=ACC0;date=1;SCLK=1;}RST=0;}unsignedcharREAD_1302(unsignedcharaddr){unsignedchari,date1,date2;RST=0;SCLK=0;RST=1;for(i=8;i0;i--){SCLK=0;ACC=addr;DIO=ACC0;addr=1;SCLK=1;}for(i=8;i0;i--){ACC7=DIO;SCLK=1;ACC=1;SCLK=0;}RST=0;date1=ACC;date2=date1/16;date1=date1%16;date1=date1+date2*10;return(date1);}voidINITIAL_1302(){WRITE_1302(WRITE_SECOND,READ_1302(READ_SECOND)&0x7f);//启动DS1302}/**************************键盘扫描部分***************************************************/voidkeyscan(){if(key0==0)//确认功能键按下{delay(20);if(key0==0)//去抖{while(!key0);//释放num++;switch(num){case1:flag=1;//修改时间,禁止从DS1302读数据TR0=0;//定时器关LCD_WRITE_COM(0x0f);//光标开始闪烁LCD_WRITE_COM(0x80+3);//第一次按下,光标定位到年位置break;case2:LCD_WRITE_COM(0x80+6);//第二次按下,光标定位到月位置break;case3:LCD_WRITE_COM(0x80+9);//第三次按下,光标定位到日位置break;case4:LCD_WRITE_C
本文标题:单片机实现基于DS,,B的多功能电子时钟,带测温功能
链接地址:https://www.777doc.com/doc-2606447 .html