最近在对12887实时时钟芯片进行编程 编了蛮久了 基本的框架已经出来了 就差没有完善,剩下的以后再完善咯!
程序如下:#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcdrs="P3"^7;
sbit lcdrw="P3"^6;
sbit lcden="P3"^4;
sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^2;
sbit s4=P3^5;
sbit dscs="P1"^0;
sbit dsas="P1"^1;
sbit dsrw="P1"^2;
sbit dsds="P1"^3;
uchar count,s1num;
char miao,shi,fen;
char nian,yue,ri,xq;
uchar code table[]=" 2009-00-00 000";
uchar code table1[]=" 00:00:00";
uchar code table2[][3]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
lcdrs="0";
lcden="0";
lcdrw="0";
P0=com;
delay(5);
lcden="1";
delay(5);
lcden="0";
}
void write_date(uchar date)
{
lcdrs="1";
lcden="0";
P0=date;
delay(5);
lcden="1";
delay(5);
lcden="0";
}void write_ds(uchar add,uchar date)
{
dsas="1";
dsds="1";
dsrw="1";
dscs="0";
P2=add;
dsas="0";
dsrw="0";
P2=date;
dsrw="1";
dsas="1";
dscs="1";
}
void uinit ()
{
uint num;
lcden="0";
write_ds(0x0A,0x70);
write_ds(0x06,4);
/* write_ds(0x0b,0x07);
write_ds(0x02,11);
write_ds(0x04,15);
write_ds(0x07,30);
write_ds(0x08,3);
write_ds(0x09,9);*/
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);
}
}
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 writ_nyr(uchar add,uchar date)
{
uchar shi,ge;
shi="date/10";
ge="date"%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_day(uchar date)
{
uchar day,i;
day=date;
if(day<=7)
{ if(day==1){write_com(0x80+12);for(i=0;i<3;i++) write_date(table2[1]);}
if(day==2) {write_com(0x80+12);for(i=0;i<3;i++) write_date(table2[2]);}
if(day==3){write_com(0x80+12);for(i=0;i<3;i++) write_date(table2[3]);}
if(day==4){write_com(0x80+12);for(i=0;i<3;i++) write_date(table2[4]);}
if(day==5){write_com(0x80+12);for(i=0;i<3;i++) write_date(table2[5]);}
if(day==6){write_com(0x80+12);for(i=0;i<3;i++) write_date(table2[6]);}
if(day==7){write_com(0x80+12);for(i=0;i<3;i++) write_date(table2[7]);}
}
}
uchar read_ds(uchar add)
{
uchar ds_date;
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P2=add;
dsas=0;
dsds=0;
P2=0xff;
ds_date=P2;
dsds=1;
dsas=1;
dscs=1;
return ds_date;
}
void keyscan()
{
if(s1==0)
{
delay(5);
if(s1==0)
{ s1num++;
while(!s1);
if(s1num==1)
{
write_ds(0x0A,0x70);
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);
write_ds(0x0A,0x20);
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
write_ds(0x00,miao+1);
if(miao==60)
{ miao=0;write_ds(0x00,0);}
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
write_ds(0x02,fen+1);
if(fen==60)
{fen=0;write_ds(0x02,0);}
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
write_ds(0x04,shi+1);
if(shi==24)
{shi=0;write_ds(0x04,0);}
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
write_ds(0x00,miao-1);
miao="miao-1";
if(miao==-1)
{miao=59;write_ds(0x00,59);}
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
write_ds(0x02,fen-1);
fen=fen-1;
if(fen==-1)
{fen=59;write_ds(0x02,59);}
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
write_ds(0x04,shi-1);
shi=shi-1;
if(shi==-1)
{shi=23;write_ds(0x04,23);}
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
}
}
}
/* if(s4==0)
{
delay(5);
if(s4==0);
{ y++;
TR0=0;
while(!s4);
if(y==1)
{
write_com(0x80+9);
write_com(0x0f);
}
if(y==2)
{
write_com(0x80+6);
}
if(y==3)
{
write_com(0x80+3);
}
if(y==4)
{
TR0=1;
y="0";
write_com(0x0c);
}
}
}
if(y!=0)
{
if(s2==0)
{ delay(5);
if(s2==0)
{while(!s2);
if(y==1)
{ri++;
if(ri==30)
ri="0";
writ_nyr(9,ri);
write_com(0x80+9);
}
if(y==2)
{
yue++;
if(yue==13)
yue="0";
writ_nyr(6,yue);
write_com(0x80+6);
}
}
if(y==3)
{nian++;
writ_nyr(3,nian);
write_com(0x80+3);
}
}
}*/
}
void main()
{
uinit();
while(1)
{
keyscan();
miao="read"_ds(0);
write_sfm(10,miao);
fen="read"_ds(2);
write_sfm(7,fen);
shi="read"_ds(4);
write_sfm(4,shi);
ri="read"_ds(7);
writ_nyr(9,ri);
yue="read"_ds(8);
writ_nyr(6,yue);
nian="read"_ds(9);
writ_nyr(3,nian);
xq="read"_ds(6);
write_day(xq);
}
}
文章评论(0条评论)
登录后参与讨论