|
#include<reg51.h>//---重定义关键词---//#define uchar unsigned char#define uint unsigned inttypedef unsigned char uint8;typedef unsigned int uint16;sbit rs=P2^7; // 数据命令选择sbit rw=P2^6; //读写选择sbit e=P2^5; //使能sbit K1=P3^4;sbit K2=P3^5;sbit K3=P3^6;sbit K4=P3^7;sbit beep=P2^3;int Time;char flag;unsigned char set_wei;unsigned char state=0;char sec,min,hour;char year,month,day;unsigned char Jianzhi;unsigned char Anjianzt;void KeyScan();void Delay1ms(unsigned int y);void LCD_Display_DATE();void KeyDown();void LcdWaitReady();void LcdWriteCom(uchar com); void LcdWriteData(uchar dat); void LcdInit(); void LCDWrite_String(uchar x, uchar y, uchar *s);void LCD_set_xy(uchar x, uchar y); void Delay10ms(unsigned int c);void BJ();void main(){ year=23; month=6; day=26; sec=50; min=59; hour=8; LcdInit(); TMOD = 0x21; TH0 = 0x4C; TL0 = 0x00; EA = 1; ET0 = 1; TR0 = 1; while(1) { EA = 1; LcdInit(); LcdWriteCom(0x01); while(state==0) { LCD_Display_DATE(); KeyScan(); if((Anjianzt==1)) { if(Jianzhi==1)//0x40 { state=1; set_wei=0; } Anjianzt=0; } } LcdWriteCom(0x01); LcdWriteCom(0x0f); LCD_Display_DATE(); while(state==1) { KeyScan(); if((Anjianzt==1)) { KeyDown(); Anjianzt=0; } } }}void Timer0Interrupt(void) interrupt 1{ TH0 = 0x4C; TL0 = 0x00; if(state==0) Time++; if(Time>=20) //1s { Time=0; flag=1; sec++; if(sec>=60) //秒满60清零 { sec=0; min++; if(min>=60) //分满60清零 { min=0; hour++; if(hour==24) { hour=0; day++; if(((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12)) &&(day==32) ) { day=1; month++; if(month==13) { month=0; year++; } } if(((month==4)||(month==6)||(month==9)||(month==11)) &&(day==31) ) { day=1; month++; } if((((year%4)==0)&&(month==2)) &&(day==30) ) { day=1; month++; } if((((year%4)!=0)&&(month==2)) &&(day==29) ) { day=1; month++; } } } } }}void Delay10ms(unsigned int c) { unsigned char a,b; for(;c>0;c--) for(b=38;b>0;b--) for(a=130;a>0;a--);}void BJ(){ beep=0; Delay10ms(10); beep=1;} void KeyScan(){ if(K1==0) //按下1键 { Delay10ms(1); if(K1==0) //按下1键 { Anjianzt=1; Jianzhi=1;BJ(); while(K1==0); } } if(K2==0) //按下2键 { Delay10ms(1); if(K2==0) //按下2键 { Anjianzt=1; Jianzhi=2;BJ(); while(K2==0); } } if(K3==0) //按下3键 { Delay10ms(1); if(K3==0) //按下3键 { Anjianzt=1; Jianzhi=3;BJ(); while(K3==0); } } if(K4==0) //按下4键 { Delay10ms(1); if(K4==0) //按下4键 { Anjianzt=1; Jianzhi=4; BJ(); while(K4==0); } }}void KeyDown(){ if(Jianzhi==1)//0x40 { set_wei++; if(set_wei>=6) { state=0; set_wei=0; } LCD_Display_DATE(); } if(Jianzhi==2)//0x07 { if(set_wei==0) { sec--; if(sec<0) sec=59; } else if(set_wei==1) { min--; if(min<0) min=59; } else if(set_wei==2) { hour--; if(hour<0) hour=23; } else if(set_wei==3) { day--; if(day==0) day=31; } else if(set_wei==4) { month--; if(month==0) month=12; } else if(set_wei==5) { year--; if(year<0) year=99; } LCD_Display_DATE(); } if(Jianzhi==3)//0x15 { if(set_wei==0) { sec++; if(sec>=60) sec=0; } else if(set_wei==1) { min++; if(min>=60) min=0; } else if(set_wei==2) { hour++; if(hour>=24) hour=0; } else if(set_wei==3) { day++; if(day>=32) day=1; } else if(set_wei==4) { month++; if(month>=13) month=1; } else if(set_wei==5) { year++; if(year>99) year=0; } LCD_Display_DATE(); } if(Jianzhi==4) { state=0; set_wei=0; }}void LCD_Display_DATE(){ LcdWriteCom(0x80+3); LcdWriteData('2'); LcdWriteData('0'); LcdWriteData('0'+year/10); LcdWriteData('0'+year%10); LcdWriteData('-'); LcdWriteData('0'+month/10); LcdWriteData('0'+month%10); LcdWriteData('-'); LcdWriteData('0'+day/10); LcdWriteData('0'+day%10); //--显示时钟--// LcdWriteCom(0xC0+4); LcdWriteData('0'+hour/10); LcdWriteData('0'+hour%10); LcdWriteData(':'); LcdWriteData('0'+min/10); LcdWriteData('0'+min%10); LcdWriteData(':'); LcdWriteData('0'+sec/10); LcdWriteData('0'+sec%10); if(state==1) { if(set_wei==0){LcdWriteCom(0xc0+11);} if(set_wei==1){LcdWriteCom(0xc0+8);} if(set_wei==2){LcdWriteCom(0xc0+5);} if(set_wei==3){LcdWriteCom(0x80+12);} if(set_wei==4){LcdWriteCom(0x80+9);} if(set_wei==5){LcdWriteCom(0x80+6);} } }void Lcd1602_Delay1ms(uint16 i){ while(i--);}void LcdWriteCom(uint8 c) //写命令{ Lcd1602_Delay1ms(1000); rs=0; rw=0; e=0; P0=c; e=1; Lcd1602_Delay1ms(10); e=0; rw=1; P0=0xff;}void LcdWriteData(uint8 dat) //写数据{ Lcd1602_Delay1ms(1000); rs=1; rw=0; e=0; P0=dat; e=1; Lcd1602_Delay1ms(10); e=0; rs=0; rw=1; P0=0xff;}void LcdInit(){ Lcd1602_Delay1ms(1000); LcdWriteCom(0x38);//不检测判忙信号 LcdWriteCom(0x38);//不检测判忙信号 LcdWriteCom(0x38);//不检测判忙信号 LcdWriteCom(0x06);//自动移位显示 LcdWriteCom(0x0c);//不显示光标 LcdWriteCom(0x01);//清屏} |
|