51单片机电子钟显示程序(6位数码管)不知道错在哪,程.....
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#include<absacc.h>
#define uchar unsigned char
#define set 0
#define run 1
#define keyset 4 //定义按键值
#define keyplu 5
#define keymin 6
#define keymain 7
sbit dula=P2^6;
sbit wela=P2^7;
sbit beep=P2^3;
uchar keyval,ledbit=0,ledsetbit=0;
uchar h,m,s,pp;
uchar code BIT[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar mode=run;
void setl();
void led(uchar number,uchar ledbit);
void keyscan();
void display(uchar h,uchar m,uchar s);
void delayms(uchar x)
{
uchar i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void keyscan()
{
uchar key,temp;
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(20); //防抖
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:
key=0;
break;
case 0xde:
key=1;
break;
case 0xbe:
key=2;
break;
case 0x7e:
key=3;
break;
}
while(temp!=0xf0)
{
P3=temp;
temp=temp&0xf0;
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(20);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:
key=4;
break;
case 0xdd:
key=5;
break;
case 0xbd:
key=6;
break;
case 0x7d:
key=7;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(20);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:
key=8;
break;
case 0xdb:
key=9;
break;
case 0xbb:
key=10;
break;
case 0x7b:
key=11;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(20);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:
key=12;
break;
case 0xd7:
key=13;
break;
case 0xb7:
key=14;
break;
case 0x77:
key=15;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
}
void setl()
{
keyscan();
if(keyval=keyset)
{
if(mode==set&&ledsetbit<=5)
{
ledsetbit++;
}
if(mode==set&&ledsetbit>5)
{
mode=run;
ledsetbit=0;
}
if(keyval==keyplu&&mode==set)
{
switch(ledsetbit)
{
case 0:if(s<59)s++;break;
case 1:if(s<50)s+=10;break;
case 2:if(m<59)m++;break;
case 3:if(m<50)m+=10;break;
case 4:if(h<23)h++;break;
case 5:if(h<14)h+=10;break;
}
}
if(keyval==keymin&&mode==set)
{
switch(ledsetbit)
{
case 0:if(s>0)s--;break;
case 1:if(s>10)s-=10;break;
case 2:if(m>0)m--;break;
case 3:if(m>10)m-=10;break;
case 4:if(h>0)h--;break;
case 5:if(h>10)h-=10;break;
}
}
}
}
void led(uchar number,uchar ledbit)
{
P0=table[number];
dula=1;
dula=0;
if(mode==run)
P0=BIT[ledbit];
wela=1;
wela=0;
if(mode==set)
{
if(ledbit==ledsetbit)
P0=0xff;
else
P0=table[number];
dula=1;
dula=0;
}
}
void display(uchar h,uchar m,uchar s)
{
unsigned int t=100;
switch(ledbit)
{
case 0:led(s%10,ledbit);
case 1:led(s/10,ledbit);
case 2:led(m%10,ledbit);
case 3:led(m/10,ledbit);
case 4:led(h%10,ledbit);
case 5:led(h/10,ledbit);
}
ledbit++;
if(ledbit>5)ledbit=0;
while(t--);
}
void time0() interrupt 1
{
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
pp++;
}
void main()
{
TMOD=0x01;
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
ET0=1;
EA=0;
TR0=1;
while(1)
{
setl();
if(mode==run)
{
display(h,m,s);
}
else
{
led();
}
if(pp==20)
{
pp=0;
s++;
if(s==60)
{
s=0;
m++;
if(m=60)
{
m=0;
m++;
if(m==60)
{
h++;
if(h==24)
{
h=0;
m=0;
s=0;
}
}
}
}
}
}
}
#include<absacc.h>
#define uchar unsigned char
#define set 0
#define run 1
#define keyset 4 //定义按键值
#define keyplu 5
#define keymin 6
#define keymain 7
sbit dula=P2^6;
sbit wela=P2^7;
sbit beep=P2^3;
uchar keyval,ledbit=0,ledsetbit=0;
uchar h,m,s,pp;
uchar code BIT[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar mode=run;
void setl();
void led(uchar number,uchar ledbit);
void keyscan();
void display(uchar h,uchar m,uchar s);
void delayms(uchar x)
{
uchar i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void keyscan()
{
uchar key,temp;
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(20); //防抖
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:
key=0;
break;
case 0xde:
key=1;
break;
case 0xbe:
key=2;
break;
case 0x7e:
key=3;
break;
}
while(temp!=0xf0)
{
P3=temp;
temp=temp&0xf0;
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(20);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:
key=4;
break;
case 0xdd:
key=5;
break;
case 0xbd:
key=6;
break;
case 0x7d:
key=7;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(20);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:
key=8;
break;
case 0xdb:
key=9;
break;
case 0xbb:
key=10;
break;
case 0x7b:
key=11;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(20);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:
key=12;
break;
case 0xd7:
key=13;
break;
case 0xb7:
key=14;
break;
case 0x77:
key=15;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
}
void setl()
{
keyscan();
if(keyval=keyset)
{
if(mode==set&&ledsetbit<=5)
{
ledsetbit++;
}
if(mode==set&&ledsetbit>5)
{
mode=run;
ledsetbit=0;
}
if(keyval==keyplu&&mode==set)
{
switch(ledsetbit)
{
case 0:if(s<59)s++;break;
case 1:if(s<50)s+=10;break;
case 2:if(m<59)m++;break;
case 3:if(m<50)m+=10;break;
case 4:if(h<23)h++;break;
case 5:if(h<14)h+=10;break;
}
}
if(keyval==keymin&&mode==set)
{
switch(ledsetbit)
{
case 0:if(s>0)s--;break;
case 1:if(s>10)s-=10;break;
case 2:if(m>0)m--;break;
case 3:if(m>10)m-=10;break;
case 4:if(h>0)h--;break;
case 5:if(h>10)h-=10;break;
}
}
}
}
void led(uchar number,uchar ledbit)
{
P0=table[number];
dula=1;
dula=0;
if(mode==run)
P0=BIT[ledbit];
wela=1;
wela=0;
if(mode==set)
{
if(ledbit==ledsetbit)
P0=0xff;
else
P0=table[number];
dula=1;
dula=0;
}
}
void display(uchar h,uchar m,uchar s)
{
unsigned int t=100;
switch(ledbit)
{
case 0:led(s%10,ledbit);
case 1:led(s/10,ledbit);
case 2:led(m%10,ledbit);
case 3:led(m/10,ledbit);
case 4:led(h%10,ledbit);
case 5:led(h/10,ledbit);
}
ledbit++;
if(ledbit>5)ledbit=0;
while(t--);
}
void time0() interrupt 1
{
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
pp++;
}
void main()
{
TMOD=0x01;
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
ET0=1;
EA=0;
TR0=1;
while(1)
{
setl();
if(mode==run)
{
display(h,m,s);
}
else
{
led();
}
if(pp==20)
{
pp=0;
s++;
if(s==60)
{
s=0;
m++;
if(m=60)
{
m=0;
m++;
if(m==60)
{
h++;
if(h==24)
{
h=0;
m=0;
s=0;
}
}
}
}
}
}
}
你先说哪里有问题啊!
就是数码管只显示6个0,还有按按键时,并没有调时间