- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit LCD_RS=P2^6;
- sbit LCD_RW=P2^5;
- sbit LCD_EN=P2^7;
- sbit LCD_PSB=P3^2;
- sbit s1=P1^4; //独立按键
- sbit s2=P1^5;
- sbit s3=P1^6;
- sbit rd=P1^7;
- sbit beep=P2^3; //定义蜂鸣器端口
- uchar miao,shi,fen,mon,day,week,year1,year2,i, s1num,count;
- uint a,b,year;
- uchar code dis2[]={"星期 "};
- uchar code dis3[]={ "风雨恋情"};
- void delay_1ms(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void di() //蜂鸣器
- {
- beep=0;
- delay_1ms(100);
- beep=1;
- }
- void write_cmd(uchar cmd) //写命令
- {
- LCD_RS=0;
- LCD_RW=0;
- LCD_EN=0;
- P0=cmd;
- delay_1ms(5);
- LCD_EN=1;
- delay_1ms(5);
- LCD_EN=0;
- }
- void write_dat(uchar dat) //写数据
- {
- LCD_RS=1;
- LCD_RW=0;
- LCD_EN=0;
- P0=dat;
- delay_1ms(5);
- LCD_EN=1;
- delay_1ms(5);
- LCD_EN=0;
- }
- void write_sfm(uchar add,char dat) //写时分秒
- {
- uchar shi1,ge1;
-
- shi1=dat/10;
- ge1=dat%10;
- write_cmd(0x90+add); //写显示地址的指令
- write_dat(0x30+shi1);
- write_dat(0x30+ge1);
- //ge2=dat%10;
- //write_cmd(0x88+add);
- // write_dat(0x30+ge2);
- }
- void write_nyr(uchar add,char dat) //写年月
- {
- char shi,ge;
- shi=dat/10;
- ge=dat%10;
- write_cmd(0x80+add);
- write_dat(0x30+shi);
- write_dat(0x30+ge);
- }
- void write_week(uchar add,char dat) //写星期
- {
- char ge2;
- ge2=dat%10;
- write_cmd(0x88+add);
- write_dat(0x30+ge2);
- }
- void init()
- { //初始化 2014年11月6日星期1 23:59:59
- LCD_PSB=1;
- rd=0;
- LCD_EN=0;
- fen=59;
- miao=59;
- shi=23;
- week=1;
- year1=20;
- year2=14;
- mon=11;
- day=6;
- count=0;
- s1num=0;
- TMOD=0x01; //中断0,方式1
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- EA=1;
- ET0=1;
- TR0=1;
- write_cmd(0x30);
- delay_1ms(5);
- write_cmd(0x0c);
- delay_1ms(5);
- write_cmd(0x01);
- delay_1ms(5);
- write_cmd(0x91);
- write_dat(':');
- delay_1ms(5);
- write_cmd(0x93);
- write_dat(':');
- delay_1ms(5);
- write_sfm(4,miao);
- write_sfm(2,fen);
- write_sfm(0,shi) ;
- write_cmd(0x82);
- write_dat('-');
- delay_1ms(5);
- write_cmd(0x84);
- write_dat('-');
- delay_1ms(5);
- write_nyr(5,day);
- write_nyr(3,mon);
- write_nyr(0,year1);
- write_nyr(1,year2);
- write_week(2,week);
- }
- void keyscan() //独立键盘检测
- {
- if(s1==0)
- {
- delay_1ms(5);
- if(s1==0)
- {
- s1num++;
- while(!s1);
- di();
- if(s1num==1)
- {
- TR0=0;
- write_cmd(0x80); //光标定位到年的位置
- write_cmd(0x0f); //光标开始闪数
- }
- if(s1num==2)
- {
- write_cmd(0x81); //光标定位到年的位置
- }
- if(s1num==3)
- {
- write_cmd(0x83); //光标定位到月的位置
- }
- if(s1num==4)
- {
- write_cmd(0x85); //光标定位到天的位置
- }
- if(s1num==5)
- {
- write_cmd(0x90); //光标定位到时的位置
- }
- if(s1num==6)
- {
- write_cmd(0x92); //光标定位到分的位置
- }
- if(s1num==7)
- {
- write_cmd(0x94); //光标定位到秒的位置
- }
- if(s1num==8)
- {
- write_cmd(0x8a); //光标定位到星期的位置
- }
- if(s1num==9)
- {
- s1num=0;
- write_cmd(0x0c); //取消光标闪数
- TR0=1; //启动定时器
- }
- }
- }
- if(s1num!=0) //只有功能键被按下后,增大和减小键才有效
- {
- if(s2==0)
- {
- delay_1ms(5);
- if(s2==0)
- {
- while(!s2);
- di();
- if(s1num==1)
- {
- year1++;
- if(year1==99)
- year1=0;
- write_nyr(0,year1);
- write_cmd(0x80);
- }
- if(s1num==2)
- {
- year2++;
- if(year2==99)
- year2=0;
- write_nyr(1,year2);
- write_cmd(0x81);
- }
- if(s1num==3)
- {
- mon++;
- if(mon==13)
- mon=1;
- write_nyr(3,mon);
- write_cmd(0x83);
- }
- if(s1num==4)
- {
- day++;
- if(day==a+1)
- day=1;
- write_nyr(5,day);
- write_cmd(0x85);
- }
- if(s1num==5)
- {
- shi++;
- if(shi==23)
- shi=0;
- write_sfm(0,shi);
- write_cmd(0x90);
- }
- if(s1num==6)
- {
- fen++;
- if(fen==59)
- fen=0;
- write_sfm(2 ,fen);
- write_cmd(0x92);
- }
- if(s1num==7)
- {
- miao++;
- if(miao==59)
- miao=0;
- write_sfm(4,miao);
- write_cmd(0x94);
- }
- if(s1num==8)
- {
- week++;
- if(week==8)
- week=0;
- write_week(2,week);
- write_cmd(0x8a);
- }
- }
- }
- if(s3==0)
- {
- delay_1ms(5);
- if(s3==0)
- {
- while(!s3);
- di();
- if(s1num==1)
- {
- year1--;
- if(year1==-1)
- year1=99;
- write_nyr(0,year1);
- write_cmd(0x80);
- }
- if(s1num==2)
- {
- year2--;
- if(year2==-1)
- year2=99;
- write_nyr(1,year2);
- write_cmd(0x81);
- }
- if(s1num==3)
- {
- mon--;
- if(mon==0)
- mon=12;
- write_nyr(3,mon);
- write_cmd(0x83);
- }
- if(s1num==4)
- {
- day--;
- if(day==0)
- day=a;
- write_nyr(5,day);
- write_cmd(0x85);
- }
- if(s1num==5)
- {
- shi--;
- if(shi==-1)
- shi=23;
- write_sfm(0,shi);
- write_cmd(0x90);
- }
- if(s1num==6)
- {
-
- fen--;
- if(fen==-1)
- fen=59;
- write_sfm(2 ,fen);
- write_cmd(0x92);
- }
- if(s1num==7)
- {
- miao--;
- if(miao==-1)
- miao=59;
- write_sfm(4,miao);
- write_cmd(0x94);
- }
- if(s1num==8)
- {
- week--;
- if(week==0)
- week=7;
- write_week(2,week);
- write_cmd(0x8a);
- }
- }
- }
- }
- }
- void display()//判断月份年份天数
- {
- year=year2+year1*100;
- if(mon==1||3||5||7||8||10||12)
- a=31;
- if(mon==4||6||9||11)
- a=30;
- if(mon==2)
- {
- if(year%4!=0)
- a=28;
- else
- a=29;
- }
- }
- void main()
- {
- init();
- write_cmd(0x98); //地址
- for(i=0;i<8;i++)
- {
- write_dat(dis3[i]);
- delay_1ms(5);
- }
- write_cmd(0x88); //地址
- for(i=0;i<4;i++)
- {
- write_dat(dis2[i]);
- delay_1ms(5);
- }
- while(1)
- {
- keyscan();
- display();
- }
- }
- void timer0()interrupt 1
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- count++;
- if(count==20)
- {
- count=0;
- miao++;
- if(miao==60)
- {
- miao=0;
- fen++;
- if(fen==60)
- {
- fen=0;
- shi++;
- if(shi==24)
- {
- shi=0;
- day++;
- week++;
- if(week==7)
- {
- week=0;
- }
- if(day==a)
- {
- day=0;
- mon++;
- if(mon==12)
- {
- mon=0;
- year2++;
- if(year2==99)
- {
- year2=-1;
- year1++;
- }
- write_nyr(0,year1);
- write_nyr(1,year2);
- }
- write_nyr(3,mon);
- }
- write_nyr(5,day);
- }
- write_week(2,week);
- write_sfm(0,shi);
- }
- write_sfm(2,fen);
- }
- write_sfm(4,miao);
- }
- }
复制代码
|