因为我想做个4位的精确时钟,但不知道自己程序错在哪,为之苦恼很久,在贵
论坛找到@pusa分享的DS12C887数码管的程序,因为没有注释有些地方不懂,想请教下大家。程序如下:
#include<reg52.h>
#define uchar unsigned char
#define ulong unsigned long
sbit dula=P2^6;
sbit wela=P2^7;
sbit dscs=P1^4;
sbit dsas=P1^5;
sbit dsds=P1^7;
sbit dsrw=P1^6;
sbit s1=P1^0;
sbit s2=P1^1;
sbit s3=P1^2;
void xianshi(uchar add,uchar shu);
uchar code table_shu[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code table_add[]={0x00,0xFE,0xFD,0xFB,
0xF7,0xEF,0xDF};
uchar code table_wela[]={0xff,0xe0,0xd0,0xc8,
0xc4,0xc2,0xc1 };
uchar shu_num,s1_num,s1_flag;
uchar shi,fen,miao;
void delay_ms(uchar t)
{
uchar x,y;
for(x=t;x>0;x--)
for(y=110;y>0;y--);
}
void write_ds(uchar add,uchar date)
{
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P0=add;
dsas=0;
dsrw=0;
P0=date;
dsrw=1;
dsas=1;
dscs=1;
}
void set_
time()
{
write_ds(0,55);
write_ds(2,59);
write_ds(4,23);
}
void init()
{
EA=1;
EX1=1;
IT1=1;
dula=0;
wela=0;
write_ds(0x0A,0x20);
write_ds(0x0B,0x06);
set_time();
}
void xianshi(uchar add,uchar shu)
{
uchar s,g;
s=shu/10;
g=shu%10;
wela=1;
P0=table_add[add];
wela=0;
delay_ms(1);
P0=0xff;
dula=1;
P0=table_shu[g];
dula=0;
delay_ms(1);
wela=1;
P0=table_add[add+1];
wela=0;
delay_ms(1);
P0=0xff;
dula=1;
P0=table_shu[s];
dula=0;
delay_ms(1);
}
void shanshuo(uchar add)
{
wela=1;
P0=table_add[add];
wela=0;
delay_ms(1);
P0=0xff;
dula=1;
P0=0x00;
dula=0;
delay_ms(1);
wela=1;
P0=table_add[add+1];
wela=0;
delay_ms(1);
P0=0xff;
dula=1;
P0=0x00;
dula=0;
delay_ms(1);
}
void keyscan()
{
if(s1==0)
{
delay_ms(5);
if(s1==0)
{
s1_num++;
s1_flag=1;
while(!s1);
}
}
switch(s1_num)
{
case 1:
xianshi(1,miao);
break;
case 2:
xianshi(3,fen);
break;
case 3:
xianshi(5,shi);
break;
case 4:
s1_num=0;
s1_flag=0;
break;
}
if(s1_num!=0)
{
if(s2==0)
{
delay_ms(5);
if(s2==0)
{
while(!s2);
switch(s1_num)
{
case 1:
miao++;
if(miao==60)
miao=0;
xianshi(1,miao);
write_ds(0,miao);
break;
case 2:
fen++;
if(fen==60)
fen=0;
xianshi(3,fen);
write_ds(2,fen);
break;
case 3:
shi++;
if(shi==24)
shi=0;
xianshi(5,shi);
write_ds(4,shi);
break;
}
}
}
if(s3==0)
{
delay_ms(5);
if(s3==0)
{
while(!s3);
switch(s1_num)
{
case 1:
miao--;
if(miao==-1)
miao=59;
xianshi(1,miao);
write_ds(0,miao);
break;
case 2:
fen--;
if(fen==-1)
fen=59;
xianshi(3,fen);
write_ds(2,fen);
break;
case 3:
shi--;
if(shi==-1)
shi=23;
xianshi(5,shi);
write_ds(4,shi);
break;
}
}
}
}
}
uchar read_ds(uchar add)
{
uchar ds_date;
dsas=1;
dsds=1;
dsrw=1;
dscs=0;
P0=add;
dsas=0;
dsds=0;
P0=0xff;
ds_date=P0;
dsds=1;
dsas=1;
dscs=1;
return ds_date;
}
void main()
{
init();
while(1)
{
keyscan();
if(s1_flag==0)
{
keyscan();
shi=read_ds(4);
fen=read_ds(2);
miao=read_ds(0);
xianshi(5,shi);
xianshi(3,fen);
xianshi(1,miao);
}
}
}
其中显示部分P0=table_add[add];P0=table_shu[g];P0=table_add[add+1];P0=table_shu[s];是什么意思??
一周热门 更多>