您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > DS1302时钟+LCD1602显示+键盘调时+蜂鸣器
-1-/****************************************************************************标题:可调的DS1302时钟在LCD1602上显示**********************************************************************************接线:JP8接JP4、蜂鸣器短路帽的左端接到P3^2(用一根杜邦线)**说明:本实验是在普中科技单片机开发板HC6800EM3上实现的******************************************************************************/#includereg52.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedintsbitSCK=P3^6;//时钟sbitSDA=P3^4;//数据sbitRST=P3^5;//DS1302复位sbitlcdrs=P2^6;sbitlcdrw=P2^5;sbitlcden=P2^7;sbitkey1=P1^4;sbitkey2=P1^5;sbitkey3=P1^6;sbitkey4=P1^7;sbitH=P1^1;//矩阵键盘第一行四个键sbitbeep=P3^2;bitReadRTC_Flag;//定义读DS1302标志ucharmiao,fen,shi,ri,yue,week,nian,a,T,n=0;charl_tmpdate[7]={50,8,1,7,10,1,12};//设置初始时间,秒分时日月周年12-10-0701:08:50ucharcodewrite_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};//秒分时日月周年最低位写位ucharcoderead_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};//秒分时日月周年最低位读位ucharcodetable1[]=SUN;//星期字表ucharcodetable2[]=MON;ucharcodetable3[]=TUE;ucharcodetable4[]=WED;ucharcodetable5[]=THU;-2-ucharcodetable6[]=FRI;ucharcodetable7[]=SAT;ucharcodetab1[]=20--;ucharcodetab2[]=::;/******************************************************************//*函数声明*//******************************************************************/voidWrite_Ds1302_byte(uchartemp);voidWrite_Ds1302(ucharaddress,uchardat);ucharRead_Ds1302(ucharaddress);voidRead_RTC(void);voidSet_RTC(void);voiddelay(uchara);voidini_Timer();voiddi();voidlcd_mang();voidwrite_lcd1602(ucharcmd,uchari);voidini_lcd1602();voidwrite_week(ucharxq);voidwrite_nyr(ucharadd,uchardat);voidwrite_sfm(ucharadd,uchardat);voiddisplay();voidkeyscan();/******************************************************************//*主函数*//******************************************************************/voidmain(void){ini_Timer();ini_lcd1602();//Set_RTC();//写入时钟值,如果使用备用电池时候,不需要没每次上电写入,此程序应该屏蔽while(1){keyscan();if(ReadRTC_Flag==0){Read_RTC();display();-3-}}}/******************************************************************//*BCD转换成LCD1602能显示的16进制数,秒、分、时*//******************************************************************/voidwrite_sfm(ucharadd,uchardat){uchargw,sw;gw=dat%16;sw=dat/16;write_lcd1602(0x80+0x40+add,0);write_lcd1602(sw+0x30,1);write_lcd1602(gw+0x30,1);}/******************************************************************//*BCD转换成LCD1602能显示的16进制数,日、月、年*//******************************************************************/voidwrite_nyr(ucharadd,uchardat){uchargw,sw;gw=dat%16;sw=dat/16;write_lcd1602(0x80+add,0);write_lcd1602(sw+0x30,1);write_lcd1602(gw+0x30,1);}/******************************************************************//*显示星期*//******************************************************************/voidwrite_week(ucharxq){ucharx;write_lcd1602(0x80+12,0);switch(xq){case1:for(x=0;x3;x++){write_lcd1602(table1[x],1);}break;case2:-4-for(x=0;x3;x++){write_lcd1602(table2[x],1);}break;case3:for(x=0;x3;x++){write_lcd1602(table3[x],1);}break;case4:for(x=0;x3;x++){write_lcd1602(table4[x],1);}break;case5:for(x=0;x3;x++){write_lcd1602(table5[x],1);}break;case6:for(x=0;x3;x++){write_lcd1602(table6[x],1);}break;case7:for(x=0;x3;x++){write_lcd1602(table7[x],1);}break;}write_lcd1602(0x80+12,0);}/******************************************************************//*显示函数*//******************************************************************/voiddisplay(){write_sfm(10,miao);-5-write_sfm(7,fen);write_sfm(4,shi);write_nyr(3,nian);write_nyr(6,yue);write_nyr(9,ri);write_week(week);}/******************************************************************//*毫秒级延时函数*//******************************************************************/voiddelay(uchara){uchari;while(a--){for(i=0;i250;i++){_nop_();_nop_();_nop_();_nop_();}}}/******************************************************************//*定时器0使蜂鸣器发声函数*//******************************************************************/voidini_Timer(){a=0;TMOD=0X01;TH0=(65536-250)/256;TL0=(65536-250)%256;EA=1;ET0=1;TR0=1;}voidtimer0()interrupt1{TH0=(65536-250)/256;TL0=(65536-250)%256;a++;}voiddi()-6-{while(key1==0||key2==0||key3==0){if(a==2){a=0;beep=~beep;}}}/******************************************************************//*LCD1602判忙函数*//******************************************************************/voidlcd_mang(){lcdrs=0;lcdrw=1;lcden=1;_nop_();_nop_();_nop_();_nop_();while(P0&0x80);lcden=0;}/******************************************************************//*LCD1602写指令(i=0)、数据(i=1)函数*//******************************************************************/voidwrite_lcd1602(ucharcmd,uchari){lcd_mang();lcdrs=i;lcdrw=0;lcden=0;_nop_();_nop_();lcden=1;_nop_();_nop_();P0=cmd;_nop_();_nop_();_nop_();-7-_nop_();lcden=0;}/******************************************************************//*LCD1602初始化函数*//******************************************************************/voidini_lcd1602(){uchari;write_lcd1602(0x38,0);delay(1);write_lcd1602(0x0c,0);delay(1);write_lcd1602(0x06,0);delay(1);write_lcd1602(0x01,0);delay(1);write_lcd1602(0x80+1,0);for(i=0;i8;i
本文标题:DS1302时钟+LCD1602显示+键盘调时+蜂鸣器
链接地址:https://www.777doc.com/doc-6573836 .html