#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit rs=P1^0;
sbit rw=P1^1;
sbit lcden=P2^5;
sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^2;
sbit rd=P3^7;
uchar count,s1num;
char miao,shi,fen;
int weekday,day,month,year;
uchar code table[]=" 2008- 2-28 MON";
uchar code table1[]=" 23:59:00";
int weekday,day,month,year;
char code num1[]={"Mon"};
char code num2[]={"Tue"};
char code num3[]="Wed";
char code num4[]="Tur";
char code num5[]="Fri";
char code num6[]="Sta";
char code num7[]="Sun";
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
rs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date)
{
rs=1;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
uchar num;
dula=0;
wela=0;
lcden=0;
fen=59;
miao=53;
shi=23;
weekday=0;
day=31;
month=12;
year=2013;
rw=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay(5);
}
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_weekday()
{ char i;
write_com(0x80+15);
for(i=0;i<=2;i++){
if (weekday==0)
{write_date(num1
); }
if(weekday==1)
{write_date(num2); }
if(weekday==2)
{write_date(num3); }
if (weekday==3)
{write_date(num4); }
if(weekday==4)
{write_date(num5); }
if(weekday==5)
{write_date(num6); }
if(weekday==6)
{write_date(num7); }
if(weekday==7)
weekday=0;
}
}
void write_day()
{ uchar day0,day1;
day0=day%10;
day1=day/10;
write_com(0x80+11);
write_date(day0+0x30);
write_date(day1+0x30);
}
void write_month()
{ uchar mon0,mon1;
mon0=month%10;
mon1=month/10;
write_com(0x80+8);
write_date(mon0+0x30);
write_date(mon1+0x30);
}
void write_year()
{ uchar a1,a2,a3,a4;
write_com(0x80+5);
a4=year%10;
a3=(year/10)%10;
a2=(year/100)%10;
a1=year/1000;
write_date(a4+0x30);
write_date(a3+0x30);
write_date(a2+0x30);
write_date(a1+0x30);
}
void keyscan()
{
rd=0;
if(s1==0)
{
delay(5);
if(s1==0)
{ s1num++;
while(!s1);
if(s1num==1)
{
TR0=0;
write_com(0x80+0x40+10);
write_com(0x0f);
}
}
if(s1num==2)
{
write_com(0x80+0x40+7);
}
if(s1num==3)
{
write_com(0x80+0x40+4);
}
if(s1num==4)
{
s1num=0;
write_com(0x0c);
TR0=1;
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
/* if(miao==0)
{
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}*/
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
}
}
}
}
/* void main()
{
init();
while(1)
{
keyscan();
}
// while(1);
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==18)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(4,shi);
}
write_sfm(7,fen);
}
write_sfm(10,miao);
}
} */
void main()
{
init();
while(1)
{
keyscan();
if(count==18)
{
count=0;
miao++; }
if(miao==60)
{
miao=0;
fen++;}
if(fen==60)
{
fen=0;
shi++;}
if(shi==24)
{
shi=0;
weekday++;
day++; }
if(weekday==7)
{
weekday=0;
}
if(year%4==0)
{if(month==1)
if(day==30)
{month++;
day=1;}
}
else
if(month==1)
{if(day==29)
month++;
day=1;}
else
if(month%2==0)
{if(day==32)
month++;
day=1;
}
else
{if(day==31)
month++;
day=1;
}
if(month==12)
{ month=0;
year++;}
write_sfm(4,shi);
write_sfm(7,fen);
write_sfm(10,miao);
write_day();
write_weekday();
write_month();
write_year();
}
// while(1);
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
}
此帖出自小平头技术问答
一周热门 更多>