基于51单片机定时器的1602液晶显示器时钟显示(带年月日)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includereg52.h#defineucharunsignedchar#defineuintunsignedintuchara,count,S1num,xqnum;charyue,ri,shi,fen,miao;intnian;sbitlcdrs=P2^5;sbitlcdrw=P2^4;sbitlcden=P2^3;sbitS1=P3^0;//定义键——进入设置sbitS2=P3^1;//定义键——增大sbitS3=P3^2;//定义键——减小sbitS4=P3^3;//定义键——退出设置ucharcodetable0[]=2014-08-13WED;ucharcodetable1[]=00:00:00;ucharcodexingqi[][3]={MON,TUE,WED,THU,FRI,SAT,SUN};voiddelay(uintz){uintx,y;for(x=z;x0;x--)for(y=110;y0;y--);}/**************************************************1602液晶显示器模块sbitlcdrs=P2^5;sbitlcdrw=P2^4;sbitlcden=P2^3;*************************************************/voidwrite_com(ucharcom)//液晶写指令{lcdrw=0;lcdrs=0;P0=com;delay(5);lcden=1;delay(5);lcden=0;}voidwrite_data(uchardat)//液晶写数据{lcdrw=0;lcdrs=1;P0=dat;delay(5);lcden=1;delay(5);lcden=0;}/********************************************年月日写入1602函数********************************************/voidwrite_sfm(ucharadd,uchardat)//写时分秒函数{ucharshi,ge;shi=dat/10;ge=dat%10;write_com(0x80+0x40+add);write_data(0x30+shi);write_data(0x30+ge);}voidwrite_yr(ucharadd,uchardat)//写月日函数{ucharshi,ge;shi=dat/10;ge=dat%10;write_com(0x80+add);write_data(0x30+shi);write_data(0x30+ge);}voidwrite_nian(ucharadd,uintdat)//写年函数{uintqian,bai,shi,ge;qian=dat/1000;bai=dat%1000/100;shi=dat%100/10;ge=dat%10;write_com(0x80+add);write_data(0x30+qian);write_data(0x30+bai);write_data(0x30+shi);write_data(0x30+ge);}/***************************************************初始化***************************************************/voidinit_lcd()//液晶初始化{lcden=0;nian=2014;yue=8;ri=13;shi=0;//初始shi、fen、miaofen=0;miao=0;write_com(0x38);//设置16x2显示,5x7点阵,8位数据口write_com(0x0c);//设置开显示,不显示光标write_com(0x06);//写一个字符后地址指针加1write_com(0x01);//显示清0,数据指针清0}voidinit()//初始化函数{init_lcd();//液晶初始化write_com(0x80);//设置显示初始坐标for(a=0;a14;a++)//显示年月日初始值{write_data(table0[a]);delay(5);}write_com(0x80+0x40);//设置显示初始坐标for(a=0;a8;a++)//显示时分秒初始值{write_data(table1[a]);delay(5);}write_nian(0,nian);write_sfm(6,miao);//分别将shi、fen、miao送去液晶显示write_sfm(3,fen);write_sfm(0,shi);count=0;xqnum=0;S1num=0;//初始化全局变量countTMOD=0x01;//设置定时器0工作模式1TH0=(65535-50000)/256;//定时器装初始值TL0=(65535-50000)%256;EA=1;//开总中断ET0=1;//开定时器0中断TR0=1;//启动定时器0}/**************************************************独立键盘sbitS1=P3^0;//定义键——进入设置sbitS2=P3^1;//定义键——增大sbitS3=P3^2;//定义键——减小sbitS4=P3^3;//定义键——退出设置独立键盘已接地*************************************************/voidkeyscan(){if(S1==0){delay(5);//确认定义键被按下if(S1==0){S1num++;//定义键S1按下次数记录while(!S1);//释放按键确认if(S1num==1)//S1按下一次时{TR0=0;//关闭定时器write_com(0xc0+7);//光标定位到秒位置write_com(0x0f);//光标闪烁}if(S1num==2)//S1按下两次时{write_com(0xc0+4);//光标定位到分位置}if(S1num==3)//S1按下三次时{write_com(0xc0+1);//光标定位到时位置}if(S1num==4)//S1按下四次时{write_com(0x80+13);//光标定位到星期位置}if(S1num==5)//S1按下五次时{write_com(0x80+9);//光标定位到日位置}if(S1num==6)//S1按下六次时{write_com(0x80+6);//光标定位到月位置}if(S1num==7)//S1按下七次时{write_com(0x80+3);//光标定位到年位置}if(S1num==8)//S1按下八次时{S1num=0;//S1记录按键次数清零TR0=1;//开启定时器write_com(0x0c);//取消光标闪烁}}}if(S1num!=0)//只有定义键按下后S2、S3、S4才有效{if(S2==0){delay(5);//防抖if(S2==0)//确认按键被按下{while(!S2);//释放按键确认if(S1num==1)//S1按下一次时{miao++;//调整秒加1if(miao==60)//满60清零miao=0;write_sfm(6,miao);//每调节一次送液晶显示一次write_com(0x80+0x40+6);//显示位置重新回到调节处}if(S1num==2)//S1按下两次时{fen++;//调整分加1if(fen==60)fen=0;write_sfm(3,fen);write_com(0x80+0x40+3);}if(S1num==3)//S1按下三次时{shi++;//调整时加1if(shi==24)shi=0;write_com(0x80+0x40);write_sfm(0,shi);}if(S1num==4)//星期加调整{xqnum++;if(xqnum==7)xqnum=0;write_com(0x80+0x0b);for(a=0;a3;a++){write_data(xingqi[xqnum][a]);delay(5);}}if(S1num==5)//日加调整{ri++;if(yue==2){if(nian%400==0){if(ri==30){ri=1;}}if(nian%400!=0){if(ri==29){ri=1;}}}elseif(yue=7){if(yue%2==0&yue!=2){if(ri==31){ri=1;}}elseif(yue%2!=0&yue!=2){if(ri==32){ri=1;}}}elseif(yue=8){if(yue%2==0){if(ri==32){ri=1;}}elseif(yue%2!=0){if(ri==31){ri=1;}}}write_yr(8,ri);}if(S1num==6)//月加调整{yue++;if(yue==13)yue=1;write_yr(5,yue);}if(S1num==7)//年加调整{nian++;if(nian==2019)nian=2014;write_nian(0,nian);}}}if(S3==0){delay(5);if(S3==0)//确认按键被按下{while(!S3);if(S1num==1){miao--;//调整秒减1if(miao==-1)//减到00后再减重新设置为59miao=59;write_sfm(6,miao);write_com(0x80+0x40+6);}if(S1num==2){fen--;//调整分减1if(fen==-1)fen=59;write_sfm(3,fen);write_com(0x80+0x40+3);}if(S1num==3){shi--;//调整时减1if(shi==-1)shi=23;write_sfm(0,shi);write_com(0x80+0x40);}if(S1num==4){xqnum--;//调整星期减一if(xqnum==-1)xqnum=6;write_com(0x80+0x0b);for(a=0;a3;a++){write_data(xingqi[xqnum][a]);delay(5);}}if(S1num==5)//调整日{ri--;if(yue==2){if(nian%400==0){if(ri==0){ri=29;}}if(nian%400!=0){if(ri==0){ri=28;}}}elseif(yue=7){if(yue%2==0&yue!=2){if(ri==0){ri=30;}}elseif(yue%2!=0&yue!=2){if(ri==0){ri=31;}}}elseif(yue=8){if(yue%2==0){if(ri==0){ri=31;}}elseif(yue%2!=0){if(ri==0){ri=30;}}}write_yr(8,ri);}if(S1num==6)//调整月{yue--;if(yue==0)yue=12;write_yr(5,yue);}if(S1num==7)//调整年{nian--;if(nian==2013)nian=2018;write_nian(0,nian);}}}if(S4==0)//退出设置、开启中断{delay(5);if(S4==0){S1num=0;TR0=1;write_com(0x0c);}}}}/**************************************************定时器0*************************************************/voidtimer0()interrupt1//定时器0中断服务程序{TH0=(65535-50000)/256;//重装定时器初始值TL0=(6

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功