给个我以前写的程序,你参考参考,我这个使用1602液晶写的,显示方式需要改改 #include "1602yejing.h" sbit key1=P3^2; //选择调整位置按钮 sbit key2=P3^3; //加一按钮 sbit key3=P3^4; //减一按钮 sbit key4=P3^5; //定时按钮 char shi,fen,miao,T_shi,T_fen,T_miao; uchar count,keynum,flag; uchar code table1[]=" 2011-12-26 1"; uchar code table2[]=" 10:30:30"; uchar code table3[]="input the timing"; uchar code table4[]=" 00:00:00"; void write_sfm(uchar add,uchar date) { uchar shi,ge; shi=date/10; ge=date%10; write_com(0xc0+add); write_date(shi+'0'); write_date(ge+'0'); } void init() { uchar num; shi=10; fen=30; miao=30; P1=0xfe; write_com(0x38);//显示模式 write_com(0x0c);//不显示光标,光标不闪烁 write_com(0x06);//地址指针加一,光标加一 write_com(0x01);//清屏 write_com(0x80); for(num=0;num<16;num++) { write_date(table1[num]); delay(5); } write_com(0xc0); for(num=0;num<12;num++) { write_date(table2[num]); delay(5); } TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; ET0=1; TR0=1; } void keyscan() { uchar i; if(key4==0) { delay(5); if(key4==0) { while(!key4); flag=1; write_com(0x80); for(i=0;i<16;i++) { write_date(table3[i]); delay(5); } write_com(0xc0); for(i=0;i<12;i++) { write_date(table4[i]); delay(5); } } } if(key1==0) { delay(5); if(key1==0) { while(!key1); keynum++; if(keynum==1) { if(flag==0) TR0=0; write_com(0xc0+11); write_com(0x0f); } if(keynum==2) { write_com(0xc0+8); write_com(0x0f); } if(keynum==3) { write_com(0xc0+5); write_com(0x0f); } if(keynum==4) { write_com(0x0c); TR0=1; if(flag==0) { keynum=0; } if(flag==1) { flag=0; write_com(0x80); for(i=0;i<16;i++) { write_date(table1[i]); delay(5); } write_sfm(4,shi); write_sfm(7,fen); write_sfm(10,miao); } } } } if(keynum!=0) { if(key2==0) { delay(5); if(key2==0) { // while(!key2); if(keynum==1) { if(flag==0) { miao++; if(miao==60) miao=0; write_sfm(10,miao); write_com(0xc0+10); delay(300); } if(flag==1) { T_miao++; if(T_miao==60) T_miao=0; write_sfm(10,T_miao); write_com(0xc0+10); delay(300); } } if(keynum==2) { if(flag==0) { fen++; if(fen==60) fen=0; write_sfm(7,fen); write_com(0xc0+7); delay(300); } if(flag==1) { T_fen++; if(T_fen==60) T_fen=0; write_sfm(7,T_fen); write_com(0xc0+7); delay(300); } } if(keynum==3) { if(flag==0) { shi++; if(shi==24) shi=0; write_sfm(4,shi); write_com(0xc0+4); delay(300); } if(flag==1) { T_shi++; if(T_shi==24) T_shi=0; write_sfm(4,T_shi); write_com(0xc0+4); delay(300); } } } } if(key3==0) { delay(5); if(key3==0) { // while(!key3); if(keynum==1) { if(flag==0) { miao--; if(miao==-1) miao=59; write_sfm(10,miao); write_com(0xc0+10); delay(300); } if(flag==1) { T_miao--; if(T_miao==-1) T_miao=59; write_sfm(10,T_miao); write_com(0xc0+10); delay(300); } } if(keynum==2) { if(flag==0) { fen--; if(fen==-1) fen=59; write_sfm(7,fen); write_com(0xc0+7); delay(300); } if(flag==1) { T_fen--; if(T_fen==-1) T_fen=59; write_sfm(7,T_fen); write_com(0xc0+7); delay(300); } } if(keynum==3) { if(flag==0) { shi--; if(shi==-1) shi=23; write_sfm(4,shi); write_com(0xc0+4); delay(300); } if(flag==1) { T_shi--; if(T_shi==-1) T_shi=23; write_sfm(4,T_shi); write_com(0xc0+4); delay(300); } } } } } } void main() { init(); while(1) { keyscan(); if(miao==T_miao&&fen==T_fen&&shi==T_shi) { P1=0xff; } } } void timer0() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; count++; if(count==16) { count=0; P1=0xfe; miao++; if(miao==60) { miao=0; fen++; if(fen==60) { fen=0; shi++; if(shi==24) { shi=0; } if(flag==0) write_sfm(4,shi); } if(flag==0) write_sfm(7,fen); } if(flag==0) write_sfm(10,miao); } } 头文件 #ifndef _1602yejing_H_ #define _1602yejing_H_ #include<reg52.h> #define uchar unsigned char #define uint unsigned int sbit E=P2^7; sbit RW=P2^6; sbit RS=P2^5; void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=148;y>0;y--); } void write_com(uchar com)//写命令函数 { P0=com; RS=0; RW=0; E=0; delay(5); E=1; delay(5); } void write_date(uchar date)//写数据函数 { P0=date; RS=1; RW=0; E=0; delay(5); E=1; delay(5); } #endif |