微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 单片机控制1602液晶显示万年历

单片机控制1602液晶显示万年历

时间:08-22 来源:互联网 点击:

/*跳线设置:默认利用三个按键来调节时间---希望带给你帮助 把1602液晶插入51hei-5型单片机开发板后,即可显示时间调节时间使用int0,s4和s5这3个按键*/#includereg52.h>#include51hei.h>sbit dula=P2^6;sbit wela=P2^7;sbit lcdwr=P2^7;sbit lcdrs=P3^5;sbit lcden=P3^4;sbit key1=P3^2;sbit key2=P3^6;sbit key3=P3^7;#define uchar unsigned char#define uint unsigned intuchar code table0[]=" 2011-04-03 SUN";//16uchar code table1[]="    00:00:00";//12uchar code table2[]="MON";uchar code table3[]="TUE";uchar code table4[]="WED";uchar code table5[]="THU";uchar code table6[]="FRI";uchar code table7[]="SAT";uchar code table8[]="SUN";uchar num,aa,bb,cc,dd,flag,flag1,flag2;char shi,fen,miao,ee,ff,gg;void delay(uchar z){uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void write_com(uchar com){lcdrs=0;delay(5);P0=com;delay(5);lcden=1;delay(5);lcden=0; }void write_data(uchar date){lcdrs=1;delay(5);P0=date;delay(5);lcden=1;delay(5);lcden=0;}void init(){dula=0;wela=0;lcdwr=0;lcdrs=1;lcden=0;shi=00;fen=00;miao=00;bb=0;cc=0;dd=0;ee=03;ff=04;gg=11;write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x80);write_com(0x01);for(num=0;num15;num++){ write_data(table0[num]);delay(5);}write_com(0x80+0x40);for(num=0;num12;num++){ write_data(table1[num]);delay(5);}TMOD=0x01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1; }void sfm_date(uchar add,uchar date1){ uchar shi,ge;shi=date1/10;ge=date1%10;write_com(0x80+0x40+add);write_data(0x30+shi);write_data(0x30+ge); }void nyr_date1(uchar add,uchar date1){ uchar shi,ge;shi=date1/10;ge=date1%10;write_com(0x80+add);write_data(0x30+shi);write_data(0x30+ge); }void main(){guanled();guandz();init();while(1){ if(key1==0){bb++;delay(5);if(key1==0){if(bb==1){while(!key1);TR0=0;write_com(0x80+0x40+10);write_com(0x0f);}if(bb==2){while(!key1);write_com(0x80+0x40+7);}if(bb==3){while(!key1);write_com(0x80+0x40+4);}if(bb==4){while(!key1);write_com(0x80+12);}if(bb==5){while(!key1);write_com(0x80+9);}if(bb==6){while(!key1);write_com(0x80+6);}if(bb==7){while(!key1);write_com(0x80+3);}if(bb==8){bb=0;while(!key1);TR0=1;write_com(0x0c);}}}if(bb==1){if(key2==0){delay(5);while(!key2);miao++;if(miao==60){miao=0;}sfm_date(10,miao);write_com(0x80+0x40+10); }if(key3==0){delay(5);while(!key3);miao--;if(miao==-1){miao=59;}sfm_date(10,miao);write_com(0x80+0x40+10); }}if(bb==2){if(key2==0){delay(5);while(!key2);fen++;if(fen==60){fen=0;}sfm_date(7,fen);write_com(0x80+0x40+7); }if(key3==0){delay(5);while(!key3);fen--;if(fen==-1){fen=59;}sfm_date(7,fen);write_com(0x80+0x40+7); }}if(bb==3){if(key2==0){delay(5);while(!key2);shi++;if(shi==24){shi=0;}sfm_date(4,shi);write_com(0x80+0x40+4); }if(key3==0){delay(5);while(!key3);shi--;if(shi==-1){shi=23;}sfm_date(4,shi);write_com(0x80+0x40+4); }}if(bb==4){if(key2==0){delay(5);dd++;while(!key2);if(dd==1){for(cc=0;cc3;cc++){write_data(table2[cc]);}}write_com(0x80+12);if(dd==2){for(cc=0;cc3;cc++){write_data(table3[cc]);}}write_com(0x80+12);if(dd==3){for(cc=0;cc3;cc++){write_data(table4[cc]);}}write_com(0x80+12);if(dd==4){for(cc=0;cc3;cc++){write_data(table5[cc]);}}write_com(0x80+12);if(dd==5){for(cc=0;cc3;cc++){write_data(table6[cc]);}}write_com(0x80+12);if(dd==6){for(cc=0;cc3;cc++){write_data(table7[cc]);}}write_com(0x80+12);if(dd==7){dd=0;for(cc=0;cc3;cc++){write_data(table8[cc]);}}write_com(0x80+12);}if(key3==0){dd++;delay(5);while(!key3);if(dd==1){for(cc=0;cc3;cc++){write_data(table8[cc]);}}write_com(0x80+12);if(dd==2){for(cc=0;cc3;cc++){write_data(table7[cc]);}}write_com(0x80+12);if(dd==3){for(cc=0;cc3;cc++){write_data(table6[cc]);}}write_com(0x80+12);if(dd==4){for(cc=0;cc3;cc++){write_data(table5[cc]);}}write_com(0x80+12);if(dd==5){for(cc=0;cc3;cc++){write_data(table4[cc]);}}write_com(0x80+12);if(dd==6){for(cc=0;cc3;cc++){write_data(table3[cc]);}}write_com(0x80+12);if(dd==7){dd=0;for(cc=0;cc3;cc++){write_data(table2[cc]);}}write_com(0x80+12);}}if(bb==5){if(key2==0){delay(5);while(!key2);ee++;if(ee==32){ee=1;}nyr_date1(9,ee);write_com(0x80+9); }if(key3==0){delay(5);while(!key3);ee--;if(ee==0){ee=31;}nyr_date1(9,ee);write_com(0x80+9); } }if(bb==6){if(key2==0){delay(5);while(!key2);ff++;if(ff==13){ff=1;}nyr_date1(6,ff);write_com(0x80+6); }if(key3==0){delay(5);while(!key3);ff--;if(ff==0){ff=12;}nyr_date1(6,ff);write_com(0x80+6); } }if(bb==7){if(key2==0){delay(5);while(!key2);gg++;if(gg==41){gg=11;}nyr_date1(3,gg);write_com(0x80+3); }if(key3==0){delay(5);while(!key3);gg--;if(gg==10){gg=41;}nyr_date1(3,gg);write_com(0x80+3); } }if(aa==20){aa=0;miao++;if(miao==60){miao=0;fen++;if(fen==60){fen=0;shi++;if(shi==24){shi=0;flag=1;}sfm_date(4,shi);}sfm_date(7,fen);}sfm_date(10,miao); }if(flag==1){flag=0;ee++;if(ee==32){ee=1;flag1=1;}nyr_date1(9,ee);//write_com(0x80+9);dd++;if(dd==8){dd=0;}if(dd==1){write_com(0x80+12);for(cc=0;cc3;cc++){write_data(table2[cc]);}}write_com(0x80+12);if(dd==2){write_com(0x80+12);for(cc=0;cc3;cc++){write_data(table3[cc]);}}//write_com(0x80+12);if(dd==3){write_com(0x80+12);for(cc=0;cc3;cc++){write_data(table4[cc]);}}//write_com(0x80+12);if(dd==4){write_com(0x80+12);for(cc=0;cc3;cc++){write_data(table5[cc]);}}//write_com(0x80+12);if(dd==5){write_com(0x80+12);for(cc=0;cc3;cc++){write_data(table6[cc]);}}//write_com(0x80+12);if(dd==6){write_com(0x80+12);for(cc=0;cc3;cc++){write_data(table7[cc]);}}//write_com(0x80+12);if(dd==7){dd=0;write_com(0x80+12);for(cc=0;cc3;cc++){write_data(table8[cc]);}}// write_com(0x80+12);}if(flag1==1){flag1=0;ff++;if(ff==13){ff=1;flag2=1;}nyr_date1(6,ff);write_com(0x80+6); }if(flag2==1){flag2=0;gg++;if(gg==41){gg=11;}nyr_date1(3,gg);write_com(0x80+3);}}}void timer0() interrupt 1{TH0=(65536-50000)/256;TL0=(65536-50000)%256;aa++;}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top