问题描述
- 单片机时钟C语言程序 烧进去数码管乱跳。。
-
#include
#define uchar unsigned char
#define uint unsigned intsbit duan=P2^6;
sbit wei=P2^7;sbit key1=P3^4;
sbit key2=P3^5;
sbit key3=P3^6;
sbit key4=P3^7;uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uint shi=12,fen=12,miao=12,shig,shis,feng,fens,miaog,miaos;
uchar num,nop=0;void delay(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}void display()
{duan=1;
P0=table[miaog];
duan=0;
P0=0x00;
wei=1;
P0=0xdf;
wei=1;
delay(1);duan=1;
P0=table[miaos];
duan=0;
P0=0x00;
wei=1;
P0=0xef;
wei=0;
delay(1);duan=1;
P0=table[feng];
duan=0;
P0=0x00;
wei=1;
P0=0x77;
wei=0;
delay(1);duan=1;
P0=table[fens];
duan=0;
P0=0x00;
wei=1;
P0=0x7b;
wei=0;
delay(1);duan=1;
P0=table[shig];
duan=0;
P0=0x00;
wei=1;
P0=0xfd;
wei=0;
delay(1);duan=1;
P0=table[shis];
duan=0;
P0=0x00;
wei=1;
P0=0xfe;
wei=0;
delay(1);}
void shige()
{
miaog=miao%10;
miaos=miao/10;
feng=fen%10;
fens=fen/10;
shig=shi%10;
shis=shi/10;
}void T0_time() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num++;
if(num==20)
{
num=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
}
}
}
}void key()
{
nop=0;
if(key1==0)
{
delay(10);
nop++;
//while(!key1);
if(key1==0)
{
delay(10);
nop++;
//while(!key1);
if(key1==0)
{
delay(10);
nop++;
//while(!key1);
}
}
}if(nop==1&&key2==0) //时+
{
delay(10);
if(key2==0)
{
shi++;
if(shi==24)
{
shi=0;
while(!key2);
}
}
}if(nop==1&&key3==0) //时-
{
delay(10);
if(key3==0)
{
shi--;
if(shi==0)
{
shi=24;
while(!key3);
}
}
}if(nop==1&&key2==0)
{
delay(10);
if(key2==0)
{
fen++;
if(fen==60)
{
fen=0;
while(!key2);
}
}
}if(nop==1&&key3==0)
{
delay(10);
if(key3==0)
{
fen--;
if(fen==0)
{
fen=60;
while(!key3);
}
}
}if(nop==1&&key2==0)
{
delay(10);
if(key2==0)
{
miao++;
if(miao==60)
{
miao=0;
while(!key2);
}
}
}if(nop==1&&key3==0)
{
delay(10);
if(key3==0)
{
miao--;
if(miao==0)
{
miao=60;
while(!key3);
}
}
}}
void main()
{
TMOD=0x11;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;EA=1;
ET0=1;
TR0=1;while(1)
{
shige();
key();
display();
}}
解决方案
你的显示程序明显有问题,你仔细检查一下数码管位选怎么操作的,你最好把你的接线图发上来看看!