您好,欢迎访问三七文档
当前位置:首页 > 机械/制造/汽车 > 制造加工工艺 > 51单片机c语言编写的1602液晶显示闹钟
#includereg52.h#defineucharunsignedchar#defineuintunsignedintucharcodetable[]=2012-08-17MON;//設定開始顯示の時間ucharcodetable1[]=12:30:00;ucharcodetable2[7][3]={{MON},{TUE},{WED},{THU},{FRI},{SAT},{SUN}};//定義二維字符數組sbitwr=P3^6;//1602液晶の讀/寫端sbitdula=P2^6;//數碼管段選sbitwela=P2^7;//數碼管位選sbitlcden=P3^4;//1602液晶使能端sbitlcdrs=P3^5;//1602液晶命令/數據端sbitkey1=P3^0;//聲明s4按鍵sbitkey2=P3^1;//聲明s8按鍵sbitkey3=P3^2;//聲明s12按鍵sbitkey4=P3^3;//聲明s16按鍵sbitbeep=P2^3;//聲明蜂鳴器sbitrd=P3^7;//聲明4個按鍵の公共端ucharnum1,shi=12,fen=30,miao,yue=8,ri=17,shi1,fen1,miao1,keynum1,week;//定義時分秒變量uintnian=2012;//定義年變量voiddelayms(uintx){uinti,j;for(i=x;i0;i--)//定義帶參數延時函數for(j=110;j0;j--);}voidwrite_com(ucharcom)//1602液晶寫指令函數{wr=0;lcdrs=0;P0=com;delayms(1);lcden=1;delayms(1);lcden=0;}voidwrite_date(uchardate)//1602液晶寫數據函數{wr=0;lcdrs=1;P0=date;delayms(1);lcden=1;delayms(1);lcden=0;}voidwrite_addate1(ucharadd,uintdate)//定義顯示年の函數{uintqian;ucharbai,shi,ge;qian=date/1000;bai=date%1000/100;shi=date%1000%100/10;ge=date%10;write_com(add+0x80);write_date(0x30+qian);write_date(0x30+bai);write_date(0x30+shi);write_date(0x30+ge);}voidwrite_addate2(ucharadd,uchardate)//定義顯示月、日、時、分、秒函數{ucharshi,ge;shi=date/10;ge=date%10;write_com(add+0x80);write_date(0x30+shi);write_date(0x30+ge);}voidwrite_rq(uchari)//定義顯示星期の函數{ucharx;for(x=0;x3;x++){write_date(table2[i][x]);delayms(1);}}voidinit()//定義初始化函數{dula=0;//關閉數碼管段、位選端wela=0;TMOD=0x01;//定時器0工作方式為16位TH0=(65536-45872)/256;//裝初值TL0=(65536-45872)%256;EA=1;//開總中斷ET0=1;//開定時器0中斷write_com(0x38);//設置16*2顯示,5*7點陣,8位數據口write_com(0x06);//寫一個字符後地址指針加1write_com(0x0c);//設置開顯示,不顯示光標write_com(0x01);//顯示清0,數據清0write_com(0x80);//使指針指向第1行第1列}voidkeyscan()//定義按鍵函數{ucharkeynum;rd=0;//4個按鍵公共端為低電平if(key1==0)//s4按鍵被按下{delayms(3);//去抖延時if(key1==0)//確認s4按鍵被按下,進入調時間模式{keynum++;while(!key1);//松手檢測}if(keynum==1)//s4按下一次{TR0=0;//關閉定時器0write_com(0x81);//指針指向第1行2列write_com(0x0f);//開啟光標顯示,光標閃爍}if(keynum==2){write_com(0x86);//光標跳到月份}if(keynum==3){write_com(0x89);//光標跳到日}if(keynum==4){write_com(0x8c);//光標跳到星期}if(keynum==5){write_com(0xc4);//光標跳到時}if(keynum==6){write_com(0xc7);//光標跳到分}if(keynum==7){write_com(0xca);//光標跳到秒}if(keynum==8){keynum=0;//退出調時間模式TR0=1;//開啟定時器0write_com(0x0c);//關閉光標顯示}}if(keynum)//當進入調時間模式時{if(key2==0)//s8按鍵按下{delayms(3);//去抖延時if(key2==0){if(keynum==1){nian++;//s8每按下一次年份加1if(nian==10000)//加滿清0nian=0;write_addate1(1,nian);//顯示年份write_com(0x81);//寫完數據指針指向年份原來位置}if(keynum==2){yue++;//s8每按下一次月份加1if(yue==13)yue=0;//加滿清0write_addate2(6,yue);//顯示月份write_com(0x86);//寫完數據指針指向月份原來位置}if(keynum==3){ri++;if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)//1,3,5,7,8,10,12月有31天if(ri==32)//加滿還原ri=1;if(yue==4||yue==6||yue==9||yue==11)//4,6,9,11月有30天if(ri==31)ri=1;if(yue==2&&((nian%4==0&&nian%100!=0)||nian%400==0))//閏年2月有30天if(ri==30)ri=1;if(yue==2&&(nian%4!=0)||(nian%100==0&&nian%400!=0))//非閏年2月有31天if(ri==29)ri=1;write_addate2(9,ri);//顯示日write_com(0x89);//寫完數據指針指向日原來位置}if(keynum==4){week++;//s8每按下一次星期加1if(week==7)//加滿還原week=0;write_rq(week);//顯示星期write_com(0x8c);//寫完數據指針指向星期原來位置}if(keynum==5){shi++;//s8每按下一次時加1if(shi==24)shi=0;//加滿還原write_addate2(0x44,shi);//顯示時write_com(0xc4);//寫完數據指針指向時原來位置}if(keynum==6){fen++;//s8每按下一次分加1if(fen==60)fen=0;//加滿還原write_addate2(0x47,fen);//顯示分write_com(0xc7);//寫完數據指針指向分原來位置}if(keynum==7){miao++;//s8每按下一次秒加1if(miao==60)miao=0;//加滿還原write_addate2(0x4a,miao);//顯示秒write_com(0xca);//寫完數據指針指向秒原來位置}while(!key2);//松手檢測}}if(key3==0){delayms(3);if(key3==0){//s12按下時減1if(keynum==1){nian--;if(nian==-1)nian=9999;write_addate1(1,nian);write_com(0x81);}if(keynum==2){yue--;if(yue==-1)yue=12;write_addate2(6,yue);write_com(0x86);}if(keynum==3){ri--;if(ri==0){if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)ri=31;if(yue==4||yue==6||yue==9||yue==11)ri=30;if(yue==2&&((nian%4==0&&nian%100!=0)||nian%400==0))ri=29;if(yue==2&&(nian%4!=0)||(nian%100==0&&nian%400!=0))ri=28;}write_addate2(9,ri);write_com(0x89);}if(keynum==4){week--;if(week==-1)week=6;write_rq(week);write_com(0x8c);}if(keynum==5){shi--;if(shi==-1)shi=23;write_addate2(0x44,shi);write_com(0xc4);}if(keynum==6){fen--;if(fen==-1)fen=59;write_addate2(0x47,fen);write_com(0xc7);}if(keynum==7){miao--;if(miao==-1)miao=59;write_addate2(0x4a,miao);write_com(0xca);}while(!key3);//松手檢測}}}if(key4==0)//s16被按下進入調鬧鐘模式{delayms(3);if(key4==0){keynum1++;while(!key4);}if(keynum1==1){write_com(0x01);//清屏,指針清0write_com(0xc6);//在時分之間顯示“:”write_date(':');write_com(0xc9);//在分秒之間顯示“:”write_date(':');write_com(0x0f);//開啟光標write_com(0xc4);//光標指向時}if(keynum1==2){write_com(0xc7);//光標指向分}if(keynum1==3){write_com(0xca);//光標指向秒}if(keynum1==4){keynum1=0;write_com(0x0c);//關閉光標}}if(keynum1){if(key2==0)//s8按下加1{delayms(3);if(key2==0){if(keynum1==1){shi1++;if(shi1==24)shi1=0;write_addate2(0x44,shi1);write_com(0xc4);}if(keynum1==2){fen1++;if(fen1==60)fen1=0;write_addate2(0x47,fen1);write_com(0xc7);}if(keynum1==3){miao1++;if(miao1==60)miao1=0;write_addate2(0x4a,miao1);write_com(0xca);}while(!key2);}}if(key3==0)//s12每按一次減1{delayms(3);if(key3==0){if(keynum1==1){shi1--;if(shi1==-1)shi1=23;write_addate2(0x44,shi1);write
本文标题:51单片机c语言编写的1602液晶显示闹钟
链接地址:https://www.777doc.com/doc-5744880 .html