#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]=" 2012-12-12 SUN";
uchar code table1[]=" : : ";
sbit dula=P2^6;
sbit wela=P2^7;
sbit rs=P3^5;
sbit lcden=P3^4;
sbit FM=P2^3;
sbit cs=P1^2;
sbit as=P1^1;
sbit rw=P1^0;
sbit ds=P1^3;
sbit s1=P3^1;//功能键
sbit s2=P3^2;//增大键
sbit s3=P3^3;//减小键
sbit s5=P3^7; //前提键
char hour,min,sec,week,day,mon,year,asec,amin,ahour;
bit flag_ri,flag,flag1;
uchar k;
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void di()
{
FM=0;
delay(100);
FM=1;
}
void write_com(uchar com)
{
rs=0;
lcden=0;
P0=com;
lcden=1;
delay(3);
lcden=0;
}
void write_date(uchar date)
{
rs=1;
lcden=0;
P0=date;
lcden=1;
delay(3);
lcden=0;
}
void write_sfm(uchar add,uchar date)
{
uchar a,b;
a=date/10;
b=date%10;
write_com(0x80+0x40+add);
write_date(0x30+a);
write_date(0x30+b);
}
void write_ymd(uchar add,char date)
{
char c,d;
c=date/10;
d=date%10;
write_com(0x80+add);
write_date(0x30+c);
write_date(0x30+d);
}
void write_week(uchar k) //星期
{
write_com(0x80+12);
switch(k)
{
case 1:write_date('M');
write_date('O');
write_date('N');
break;
case 2:write_date('T');
write_date('U');
write_date('E');
break;
case 3:write_date('W');
write_date('E');
write_date('D');
break;
case 4:write_date('T');
write_date('H');
write_date('U');
break;
case 5:write_date('F');
write_date('R');
write_date('I');
break;
case 6:write_date('S');
write_date('A');
write_date('T');
break;
case 7:write_date('S');
write_date('U');
write_date('N');
break;
}
}
void write_del(uchar add,uchar date)
{
cs=0;
as=1;
ds=1;
rw=1;
P0=add;
as=0;
rw=0;
P0=date;
rw=1;
as=1;
cs=1;
}
uchar read_del(uchar add)
{
uchar date0;
as=1;
ds=1;
rw=1;
cs=0;
P0=add;
as=0;
ds=0;
P0=0xff;
date0=P0;
ds=1;
as=1;
cs=1;
return date0;
}
void keyscan()
{
s5=0;
if(s1==0)
{
delay(10);
if(s1==0)
{
while(!s1);
di();
k++;
flag=1;
if(k==1)
{
write_com(0x80+0x40+9);
write_com(0x0f);
}
if(k==2)
write_com(0x80+0x40+6);
if(k==3)
write_com(0x80+0x40+3);
if(k==4)
{
k=0;
write_com(0x0c);
write_del(0,sec);
write_del(2,min);
write_del(4,hour);
}
}
}
if(s1!=0)
{
if(s2==0)
{
delay(1);
if(s2==0)
{
while(!s2);
di();
if(k==1)
{
sec++;
if(sec==60)
sec=0;
write_sfm(9,sec);
write_com(0x80+0x40+9);
}
if(k==2)
{
min++;
if(min==60)
min=0;
write_sfm(6,min);
write_com(0x80+0x40+6);
}
if(k==3)
{
hour++;
if(hour==24)
hour=0;
write_sfm(3,hour);
write_com(0x80+0x40+3);
}
}
}
if(s3==0)
{
delay(10);
if(s3==0)
{
while(!s3);
di();
if(k==1)
{
sec--;
if(sec==-1)
sec=59;
write_sfm(9,sec);
write_com(0x80+0x40+9);
}
if(k==2)
{
min--;
if(min==-1)
min=59;
write_sfm(6,min);
write_com(0x80+0x40+6);
}
if(k==3)
{
hour--;
if(hour==-1)
hour=23;
write_sfm(3,hour);
write_com(0x80+0x40+3);
}
}
}
}
}
void init()
{
uint i;
wela=0;
dula=0;
lcden=0;
// write_del(0x0a,0xa0);
write_del(0x0b,0x27);
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x02);
write_com(0x01);
write_com(0x80);
for(i=0;i<15;i++)
{
write_date(table[i]);
delay(3);
}
write_com(0x80+0x40);
for(i=0;i<11;i++)
{
write_date(table1[i]);
delay(3);
}
}
void main()
{
init();
while(1)
{
keyscan();
sec=read_del(0);
min=read_del(2);
hour=read_del(4);
week=read_del(6);
day=read_del(7);
if(day>=32) day=1;
mon=read_del(8);
if(mon>=13) mon=1;
year=read_del(9);
if(year>=100) year=0;
write_sfm(9,sec);
write_sfm(6,min);
write_sfm(3,hour);
write_week(week);
write_ymd(9,day);
write_ymd(6,mon);
write_ymd(3,year);
}
}
当把keyscan函数去掉时可以准确计时,键盘声明正确 |