程序如下:
#include<reg52.h>
#include<math.h>
#define u8 unsigned char
#define u16 unsigned int
sbit RS=P2^7;
sbit RW=P2^6;
sbit E=P2^5;
sbit beep=P1^4;
u8 table0[]="0123456789";
u8 table1[]="00:00:00";
u8 table2[]="0000年00月00日";
u8 table4[]="祝大家天天快乐!";
u8 dat,ms=0,zc,hm=0,s=0,adjh=0,Md=0,b1,b2,FastSlow=0,day=10,month=05,week=5,Playflag=0,num=0,last=0,t1=0,jishu=0,cxs=0,ClockSet=0;
u16 a=3036,year=2014;
int m=0,h=0;
void delay(u16 t);
void displaydate();
void T2_init();
/**********************delay()****************/
void delay(u16 t){
u8 i;
while(t--){
for(i=0;i<19;i++);
}
}
/**********************BeepOn()******************/
void BeepOn(){
beep=0;
delay(200);
beep=1;
}
/********************Write_cmd(u8 cmd)往12864写命令******************************/
void Write_cmd(u8 cmd){
RS=0;
RW=0;
P0=cmd;
delay(5);
E=1;
delay(10);
E=0;
}
/**********************write_cmd(u8 dat)写数据*********************************/
void Write_data(u8 dat){
RS=1;
RW=0;
P0=dat;
delay(5);
E=1;
delay(10);
E=0;
}
/**********************LCD初始化***********/
void LCD_init(){
delay(400);
Write_cmd(0x30);
delay(100);
Write_cmd(0x30);
delay(37);
Write_cmd(0x0c);
delay(10);
Write_cmd(0x01);
delay(100);
Write_cmd(0x06);
}
/********************CaculateWeek() 由年月日计算星期********************/
int CaculateWeek(int y,int m,int d){
int w;
if(m<3){
m+=12;
--y;
}
w=(d+2*m+3*(m+1)/5+y+(y>>2)-y/100+y/400)%7;
return w;
}
/*********************产生时分秒,日期,调用星期函数************************************/
void Timer2() interrupt 5 {
static u8 i=0;
TF2=0;
i++;
if(i==16){
i=0;
s++;
if(s>59){
s=0;
m++;
if(m>59){
m=0;
h++;
adjh++;
if(h>23){
h=0;
day++;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
if(day>=32){
day=1;
month++;
}
}
else if(month==11||month==4||month==6||month==9){
if(day>=31){
day=1;
month++;
}
}
else if(month==2){
if(((0==year%4)&&(0!=year%100))||0==year%400){
if(day>=30){
day=1;
month++;
}
}
else{
if(day>=29){
day=1;
month++;
}
}
}
}
}
displaydate();
}
if(month>=13){
year++;
month=1;
}
week=CaculateWeek(year,month,day);
}
}
/***********************12864显示一个字符***************************/
void Display_Char(u8 x,u8 y){
switch(y){
case 1:Write_cmd(0x80|x);break;
case 2:Write_cmd(0x90|x);break;
case 3:Write_cmd(0x88|x);break;
case 4:Write_cmd(0x98|x);break;
}
}
/***********************x显示日期:0000年00月00日**************************/
void displaydate(){
u8 i;
table2[0]=table0[year/1000];
table2[1]=table0[year/100%10];
table2[2]=table0[year/10%10];
table2[3]=table0[year%10];
table2[6]=table0[month/10];
table2[7]=table0[month%10];
table2[10]=table0[day/10];
table2[11]=table0[day%10];
table2[14]=' ';
table2[15]=' ';
Display_Char(0,1);
for(i=0;i<16;i++)
Write_data(table2[i]);
}
/**********************12864显示时间:00:00:00**************************/
void display1(){
u8 j;
table1[4]=table0[h/10];
table1[5]=table0[h%10];
table1[7]=table0[m/10];
table1[8]=table0[m%10];
table1[10]=table0[s/10];
table1[11]=table0[s%10];
//Write_cmd(0x90);
Display_Char(0,2);
for(j=0;j<12;j++)
Write_data(table1[j]);
}
/***********************12864显示星期**************************/
void display3(){
Write_cmd(0x88);Write_data(' ');Write_data(' ');
Write_cmd(0x89);Write_data(' ');Write_data(' ');
Write_cmd(0x8A);Write_data(' ');Write_data(' ');
Write_cmd(0x8B);Write_data(0xD0);Write_data(0xC7);
Write_cmd(0x8c);Write_data(0xC6);Write_data(0xDA);
switch(week+1){
case 1:
Write_cmd(0x8d);
Write_data(0xD2);
Write_data(0xBB);
break;
case 2:
Write_cmd(0x8d);
Write_data(0xB6);
Write_data(0xFE);
break;
case 3:
Write_cmd(0x8d);
Write_data(0xC8);
Write_data(0xFD);
break;
case 4:
Write_cmd(0x8d);
Write_data(0xCB);
Write_data(0xC4);
break;
case 5:
Write_cmd(0x8d);
Write_data(0xCE);
Write_data(0xE5);
break;
case 6:
Write_cmd(0x8d);
Write_data(0xC1);
Write_data(0xF9);
break;
case 7:
Write_cmd(0x8d);
Write_data(0xC8);
Write_data(0xD5);
break;
}
Write_cmd(0x8E);
Write_data(' ');
Write_data(' ');
}
/*****************12864显示祝福语******************************/
void display4(){
u8 j;
Write_cmd(0x98);
for(j=0;j<16;j++)
Write_data(table4[j]);
}
/*****************区分C键按下还是D键按下***************/
void KeyScan(){
dat=(P1&0x0f);
if(dat==02){
delay(120);
if(dat==02){
BeepOn();
switch(hm){
case 1:
year++;
if(h>65535) year=0;
displaydate();
break;
case 2:
month++;
if(h>12) month=1;
displaydate();
break;
case 3:
day++;
if(h>31) day=1;
displaydate();
break;
case 4:
h++;
if(h>23) h=0;
break;
case 5:
m++;
if(h>59) m=0;
break;
}
}
}
if(dat==01){
delay(120);
if(dat==01){
BeepOn();
switch(hm){
case 1:
year--;
if(year<1) year=1;
displaydate();
break;
case 2:
month--;
if(month<1) month=12;
displaydate();
break;
case 3:
day--;
if(day<1) day=31;
displaydate();
break;
case 4:
h--;
if(h<0) h=23;
display1();
break;
case 5:
m--;
if(m<0) m=59;
display1();
break;
}
}
}
week=CaculateWeek(year,month,day);
}
/**********************定时器2初始化**********************************/
void T2_init(){
RCAP2H=a/256;
RCAP2L=a%256;
ET2=1;
EA=1;
TR2=1;
}
/********************判断对表键是否按下,若是,则对表*************************/
void main(){
LCD_init();
T2_init();
displaydate();
display3();
display4();
while(1){
dat=(P1&0x0f);
display1();
display4();
last=num;
display3();
if(dat==0x04){
delay(60);
if(dat==0x04){
BeepOn();
hm++;
if(hm==1){
TR2=0;
zc=ms;
displaydate();
}
else if(hm>5){
ms=zc;
hm=0;
TR2=1;
}
}
}
KeyScan();/*设置日期和时间*/
switch(hm){
case 1:{
Write_cmd(0x80);
Write_data(' ');
Write_data(' ');
Write_cmd(0x81);
Write_data(' ');
Write_data(' ');
delay(30);
Write_cmd(0x87);
Write_data(' ');
Write_data(' ');
Write_cmd(0x80);
Write_data(year/1000+0x30);
Write_data(year/100%10+0x30);
Write_cmd(0x81);
Write_data(year/10%10+0x30);
Write_data(year%10+0x30);
KeyScan();
break;
}
case 2:{
Write_cmd(0x83);
Write_data(' ');
Write_data(' ');
delay(30);
Write_cmd(0x87);
Write_data(' ');
Write_data(' ');
Write_cmd(0x83);
Write_data(month/10+0x30);
Write_data(month%10+0x30);
KeyScan();
break;
}
case 3:{
Write_cmd(0x85);
Write_data(' ');
Write_data(' ');
delay(30);
Write_cmd(0x87);
Write_data(' ');
Write_data(' ');
Write_cmd(0x85);
Write_data(day/10+0x30);
Write_data(day%10+0x30);
KeyScan();
break;
}
case 4:{
Write_cmd(0x92);
Write_data(' ');
Write_data(' ');
delay(30);
Write_cmd(0x97);
Write_data(' ');
Write_data(' ');
Write_cmd(0x92);
Write_data(h/10+0x30);
Write_data(h%10+0x30);
KeyScan();
break;
}
case 5:{
Write_cmd(0x93);
Write_data(':');
Write_data(' ');
Write_data(' ');
delay(30);
Write_cmd(0x97);
Write_data(' ');
Write_data(' ');
Write_cmd(0x93);
Write_data(':');
Write_data(m/10+0x30);
Write_data(m%10+0x30);
KeyScan();
break;
}
}
}
}
求大神指点 |