#include<reg52.h> #define uchar unsigned char #define uint unsigned int sbit lcd_rs=P2^0; sbit lcd_rw=P2^1; sbit lcd_en=P2^2; sbit key1=P3^0; sbit key2=P3^1; sbit key3=P3^2; uchar code table[]="Date:2009-12-31"; uchar code table1[]="Time: 23:59:52"; uchar code table3[]={31,29,31,30,31,30,31,31,30,31,30,31}; void write_com(uchar); void write_data(uchar); void write_dat1(uchar,uint); void write_dat2(uchar,uchar); void write_dat3(uchar,uchar); void delay(uint); void state(); void keyscan(); void clock(); uint hou,min,sec,tt,day,mon,year,keynum; void main() {sec=52; min=59; hou=23; day=31; mon=12; year=2009; state(); while(1) {clock(); keyscan();} } void mc() interrupt 1 {TH0=(65536-50000)/256; TL0=(65536-50000)%256; tt++; } void write_com(uchar com) {lcd_rs=0; lcd_rw=0; lcd_en=0; P1=com; delay(5); lcd_en=1; delay(5); lcd_en=0; } void write_data(uchar date) {lcd_rs=1; lcd_rw=0; lcd_en=0; P1=date; delay(5); lcd_en=1; delay(5); lcd_en=0; } void write_dat1(uchar add1,uint dat1) {uint qian,bai,shi,ge; qian=2; bai=0; shi=(dat1%100)/10; ge=dat1/10; write_com(0x80+add1); write_data(0x30+qian); write_data(0x30+bai); write_data(0x30+shi); write_data(0x30+ge); } void write_dat2(uchar add2,uchar dat2) {uchar shi,ge; shi=dat2/10; ge=dat2%10; write_com(0x80+0x40+add2); write_data(0x30+shi); write_data(0x30+ge); } void write_dat3(uchar add3,uchar dat3) {uchar shi,ge; shi=dat3/10; ge=dat3%10; write_com(0x80+add3); write_data(0x30+shi); write_data(0x30+ge); } void state() {uchar num; lcd_en=0; write_com(0x38); write_com(0x0c); write_com(0x06); write_com(0x01); write_com(0x80); for(num=0;num<15;num++) {write_data(table[num]); delay(5); } write_com(0x80+0x40); for(num=0;num<14;num++) {write_data(table1[num]); delay(5); } TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; ET0=1; TR0=1; } void clock() {if(tt==20)//定时1秒 {tt=0; sec++; if(sec==60) {sec=0; min++; if(min==60) { min=0; hou++; if(hou==24) {hou=0; day++; if(day>table3[mon-1]) {day=1; mon++; if(mon==13) mon=1; year++; if(year==2100) { year=2000; } write_dat1(5,year); } write_dat3(10,mon); } write_dat3(13,day); } write_dat2(6,hou); } write_dat2(9,min); } write_dat2(12,sec); } } void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void keyscan() { if(key1==0) { delay(5); if(key1==0) { keynum++; while(!key1); if(keynum==1) { TR0=0; write_com(0x80+0x40+12); write_com(0x0f); } if(keynum==2) { write_com(0x80+0x40+9); } if(keynum==3) { write_com(0x80+0x40+6); } if(keynum==4) { write_com(0x80+13); } if(keynum==5) { write_com(0x80+10); } if(keynum==6) { write_com(0x80+5); } if(keynum==7) { keynum=0; write_com(0x0c); TR0=1; } } } if(keynum!=0) { if(key2==0) { delay(5); if(key2==0) { while(!key2); switch(keynum) { case 1:sec++; if(sec==60)sec=0; write_dat2(12,sec); write_com(0x80+0x40+12);break; case 2:min++; if(min==60)min=0; write_dat2(9,min); write_com(0x80+0x40+9);break; case 3:hou++; if(hou==24)hou=0; write_dat2(6,hou); write_com(0x80+0x40+6);break; case 4:day++; if(day==table3[mon-1])day=1; write_dat3(13,day); write_com(0x80+13); break; case 5:mon++; if(mon==13)mon=1; write_dat3(10,mon); write_com(0x80+10);break; case 6:year++; if(year==2100)year=2000; write_dat1(5,year); write_com(0x80+5);break; } } } if(key3==0) { delay(5); if(key3==0) { while(!key3); switch(keynum) { case 1:sec--; if(sec==-1)sec=59; write_dat2(12,sec); write_com(0x80+0x40+12);break; case 2:min--; if(min==-1)min=59; write_dat2(9,min); write_com(0x80+0x40+9);break; case 3:hou--; if(hou==-1)hou=23; write_dat2(6,hou); write_com(0x80+0x40+6);break; case 4:day--; if(day==0)day=table3[mon-1]; write_dat3(13,day); write_com(0x80+13); break; case 5:mon--; if(mon==0)mon=12; write_dat3(10,sec); write_com(0x80+10);break; case 6:year--; if(year==1999)year=2099; write_dat1(5,sec); write_com(0x80+5);break; } } } } } 自己编写的一个时钟程序,在仿真的时候,在调节日的时候递加到最大只有30,但是从1递减的时候却又会出现31;在调节月份的时候递减,就会出现错误的月份;在调节年的时候,无论是递加还是递减都出现错误的年数。其他都是好的,请高手帮忙解决一下! |