//lcd显示数字时钟
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit rs=P3^0;
sbit rw=P3^1;
sbit lcden=P3^2;
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
sbit key4=P1^3;
sbit beep=P2^1;
int year;
uchar num,num1,t,tt,shi,fen,miao,key1_num,flag,flag1,date,month,week,week_num2;
/***************************
key1_num指按键key1按的次数
flag 指按键key1是否按下
flag1 指闹钟关否
t 定时器0计数
tt 定时器1计数
num1 调星期按键2按的次数
****************************/
uchar code table1[]=" - - ";
uchar code table2[]=" : : ";
uchar code table3[]="Mon,Tue,Wed,Thu,Fri,Sat,Sun";
//延时函数
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=114;y>0;y--);
}
//lcd写命令函数
void write_com(uchar com)
{
rs=0;
rw=0;
lcden=0;
delay(5);
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
//lcd写数据函数
void write_data(uchar date)
{
rs=1;
rw=0;
lcden=0;
delay(5);
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
//初始化函数
void init()
{
write_com(0x38);
delay(5);
write_com(0x0c);
delay(5);
write_com(0x06);
write_com(0x01);
write_com(0x80);
TMOD=0x01;
EA=1;
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
ET0=1;
TR0=1;
TH1=(65536-46080)/256;
TL1=(65536-46080)%256;
ET1=1;
TR1=1;
}
//lcd显示函数
void display()
{
for(num=0;num<16;num++)
{
write_data(table1[num]);
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_data(table2[num]);
}
}
//十、分、秒设置函数
void write_sfm(uchar add,uchar dat)
{
uchar shi_w,ge_w;
shi_w=dat/10;
ge_w=dat%10;
write_com(0x80+0x40+add);
write_data(0x30+shi_w);
write_data(0x30+ge_w);
}
//年、月、日设置函数
void write_nyr(uchar add1,uchar dat1)
{
uchar qian_w,bai_w,shi_w1,ge_w1;
uchar year_a,year_b,year_a_shiw,year_a_gew,year_b_shiw,year_b_gew;
qian_w=dat1/1000;
bai_w=dat1/100%10;
if(qian_w==0&&bai_w==0)
{
shi_w1=dat1/10%10;
ge_w1=dat1%10;
write_com(0x80+add1);
write_data(0x30+shi_w1);
write_data(0x30+ge_w1);
}
else
{
year_a=year/100;
year_b=year%100;
year_a_shiw=year_a/10;
year_a_gew=year_a%10;
year_b_shiw=year_b/10;
year_b_gew=year_b%10;
write_com(0x80+add1);
write_data(0x30+year_a_shiw);
write_data(0x30+year_a_gew);
write_data(0x30+year_b_shiw);
write_data(0x30+year_b_gew);
}
}
//星期设置函数
void write_week(uchar week_num)
{
uchar week_num1;
week_num1=week_num;
switch(week_num1)
{
case 1: write_com(0x80+12);
write_data(table3[0]);
write_data(table3[1]);
write_data(table3[2]);
write_com(0x80+12);
break;
case 2: write_com(0x80+12);
write_data(table3[4]);
write_data(table3[5]);
write_data(table3[6]);
write_com(0x80+12);
break;
case 3: write_com(0x80+12);
write_data(table3[8]);
write_data(table3[9]);
write_data(table3[10]);
write_com(0x80+12);
break;
case 4: write_com(0x80+12);
write_data(table3[12]);
write_data(table3[13]);
write_data(table3[14]);
write_com(0x80+12);
break;
case 5: write_com(0x80+12);
write_data(table3[16]);
write_data(table3[17]);
write_data(table3[18]);
write_com(0x80+12);
break;
case 6: write_com(0x80+12);
write_data(table3[20]);
write_data(table3[21]);
write_data(table3[22]);
write_com(0x80+12);
break;
case 7: write_com(0x80+12);
write_data(table3[24]);
write_data(table3[25]);
write_data(table3[26]);
write_com(0x80+12);
break;
}
}
//键盘控制函数
void key_scan()
{
if(flag1==1)
{
//闹钟暂停函数
if(key4==0)
{
delay(5);
if(key4==0)
{
while(!key4);
beep=1;
flag1=0;
}
}
}
//时间暂停
if(key1==0)
{
delay(10);
if(key1==0)
{
delay(10);
}
while(!key1);
if(key1==1)
{
flag=1;
key1_num++;
TR1=0; //关闹钟定时器
if(key1_num==8)
{
TR0=1; //关时间定时器
key1_num=0;
write_com(0x0c);
}
if(key1_num==1)
{
TR0=0;
write_com(0x80+0x40+11);
write_com(0x0d);
}
if(key1_num==2)
{
TR0=0;
write_com(0x80+0x40+8);
write_com(0x0d);
}
if(key1_num==3)
{
TR0=0;
write_com(0x80+0x40+5);
write_com(0x0d);
}
if(key1_num==4)
{
TR0=0;
write_com(0x80+12);
write_com(0x0d);
}
if(key1_num==5)
{
TR0=0;
write_com(0x80+10);
write_com(0x0d);
}
if(key1_num==6)
{
TR0=0;
write_com(0x80+7);
write_com(0x0d);
}
if(key1_num==7)
{
TR0=0;
write_com(0x80+4);
write_com(0x0d);
}
}
}
if(flag==1)
{
if(key2==0)
{
delay(10);
if(key2==0)
{
delay(10);
}
while(!key2);
if(key2==1)
{
//时、分、秒变化程序
if(key1_num==1)
{
miao++;
if(miao==60)
{
miao=0;
}
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
if(key1_num==2)
{
fen++;
if(fen==60)
{
fen=0;
}
write_sfm(7,fen);
write_com(0x80+0x40+8);
}
if(key1_num==3)
{
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(4,shi);
write_com(0x80+0x40+5);
}
//星期几递增变化程序
if(key1_num==4)
{
num1++;
if(num1==8)
{
num1=0;
}
write_week(num1);
}
//日递增变化程序
if(key1_num==5)
{
date++;
if(date==32)
{
date=01;
}
write_nyr(9,date);
write_com(0x80+10);
}
//月递增变化程序
if(key1_num==6)
{
month++;
if(month==13)
{
month=01;
}
write_nyr(6,month);
write_com(0x80+7);
}
//年递增变化程序
if(key1_num==7)
{
year++;
write_nyr(1,year);
write_com(0x80+4);
}
}
}
if(key3==0)
{
delay(10);
if(key3==0)
{
delay(10);
}
while(!key3);
if(key3==1)
{
//调整时分秒递减
if(key1_num==1)
{
if(miao==0)
{
miao=60;
}
miao--;
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
if(key1_num==2)
{
if(fen==0)
{
fen=60;
}
fen--;
write_sfm(7,fen);
write_com(0x80+0x40+8);
}
if(key1_num==3)
{
if(shi==0)
{
shi=24;
}
shi--;
write_sfm(4,shi);
write_com(0x80+0x40+5);
}
//星期几递减变化程序
if(key1_num==4)
{
num1--;
if(num1==0)
{
num1=7;
}
write_week(num1);
}
//日递减变化程序
if(key1_num==5)
{
date--;
if(date==0)
{
date=31;
}
write_nyr(9,date);
write_com(0x80+10);
}
//月递减变化程序
if(key1_num==6)
{
month--;
if(month==0)
{
month=12;
}
write_nyr(6,month);
write_com(0x80+7);
}
//年递减变化程序
if(key1_num==7)
{
year--;
write_nyr(1,year);
write_com(0x80+4);
}
}
}
}
}
//十分秒变化函数
void adjust_time()
{
//时间初始化程序
year=2009;
write_nyr(1,year);
month=12;
write_nyr(6,month);
date=31;
write_nyr(9,date);
week_num2=1;
write_week(week_num2);
shi=23;
write_sfm(4,shi);
fen=59;
write_sfm(7,fen);
miao=52;
write_sfm(10,miao);
while(1)
{
if(t==20)
{
t=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm