#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit rs=P3^5;
sbit lcden=P3^4;
sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^2;
sbit rd=P3^7;
sbit beep=P2^3;
sbit dscs=P1^4;
sbit dsas=P1^5;
sbit dsrw=P1^6;
sbit dsds=P1^7;
sbit dsirq=P3^3;
uchar count,shi,s1num,flag;
char si,fen,miao;
uchar code table[]=" 2016-08-24 MON";
uchar code table1[]=" : : ";
void write_ds(uchar,uchar);
void set_alarm(uchar,uchar,uchar);
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 set_time();
void init()
{
uchar num;
//EA=1;
//EX0=1;
//IT0=1;
dula=0;
wela=0;
lcden=0;
//si=18;
//fen=41;
//miao=20;
//set_time();
//set_alarm(20,17,00);
//write_ds(0x0B,0x26);
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<11;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 keyscan()
{
rd=0;
if(s1==0)
{
delay(5);
if(s1==0)
{
flag=1;
while(!s1);
s1num++;
if(s1num==1)
{
write_com(0x80+0x40+10);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0x80+0x40+7);
write_com(0x0f);
}
if(s1num==3)
{
write_com(0x80+0x40+4);
write_com(0x0f);;
}
if(s1num==4)
{
s1num=0;
write_com(0x0c);
flag=0;
write_ds(0,miao);
write_ds(2,fen);
write_ds(4,si);
}
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(9,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(6,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
si++;
if(si==24)
si=0;
write_sfm(3,si);
write_com(0x80+0x40+4);
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(9,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(6,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
si--;
if(si==-1)
si=23;
write_sfm(3,si);
write_com(0x80+0x40+4);
}
}
}
}
}
void write_ds(uchar add,uchar date)
{
dsas=1;
dsds=1;
dsrw=1;
dscs=0;
P0=add;
dsas=0;
dsrw=0;
P0=date;
dsrw=1;
dsas=1;
dscs=1;
}
uchar read_ds(uchar add)
{
uchar ds_date;
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P0=add;
dsas=0;
dsds=0;
P0=0xff; //读取前口赋值高 0xff
ds_date=P0;
dsds=1;
dsas=1;
dscs=1;
return ds_date;
}
void set_time()
{
write_ds(4,18);
write_ds(2,41);
write_ds(0,20);
}
void set_alarm(uchar ashi,uchar afen,uchar amiao)
{
write_ds(1,amiao);
write_ds(3,afen);
write_ds(5,ashi);
}
void main()
{
init();
while(1)
{
keyscan();
if(flag==0)
{
miao=read_ds(0);
fen=read_ds(2);
si=read_ds(4);
write_sfm(9,miao);
write_sfm(6,fen);
write_sfm(3,si);
}
}
//while(1);
}
/*void exter() interrupt 0
{
uchar c;
beep=0;
c=read_ds(0x0c);
} */
这是代码 如果最后一段
[size=15.1999998092651px]void exter() interrupt 0 [size=15.1999998092651px]{ [size=15.1999998092651px] uchar c; [size=15.1999998092651px] beep=0; [size=15.1999998092651px] c=read_ds(0x0c); [size=15.1999998092651px]} [size=15.1999998092651px]不加的话液晶正常显示时间 [size=15.1999998092651px]但如果一旦加进去 就会显示成这样 [size=15.1999998092651px]
[size=15.1999998092651px]不过半分钟之后 时间又能正常显示了 想了很长时间 一直没有答案
|