|
10黑币
我的开发板是H0T51增强型开发板
我想让数码灯每秒从0跑到99999999,我知道long int跑不到99999999,但是我需要解决的是数字显示位置不对,我用的是12M晶振。我的P0是管显示数字的。P20.P21.P22是通过74HC138控制八个数码灯哪个亮的
问题:代码运行正常,就是显示不正常,数字显示位置不对
下面是我的源代码,希望大神们看过之后,指点一二~!附加显示1,7,10,12时候的错误显示图片。
#include<reg51.h>
unsigned long int i;
unsigned char s;
unsigned char number;
#define led P0
#define LED P2
sbit led1=P2^0;
sbit led2=P2^1;
sbit led3=P2^2;
unsigned char code LedChar[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char code LedArr[]={0,1,2,3,4,5,6,7};
unsigned char LedBuff[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
void time()
{
char qq;
for(qq=0;qq<100;qq++);
}
void main()
{
P2=0xff;
i=0;
s=0;
number=0;
TMOD=0x01;
TH0=0xb1;
TL0=0xe0;
TR0=1;
while(1)
{
if(TF0==1)
{
TF0=0;
TH0=0xb1;
TL0=0xe0;
s++;
}
if(s>=50)
{
s=0;
i++;
}
if(i<10)
{
LedBuff[0]=LedChar[i%10];
}else if(i>=10&&i<100)
{
LedBuff[0]=LedChar[i%10];
LedBuff[1]=LedChar[i/10%10];
}else if(i>=100&&i<1000)
{
LedBuff[0]=LedChar[i%10];
LedBuff[1]=LedChar[i/10%10];
LedBuff[2]=LedChar[i/100%10];
}else if(i>=1000&&i<10000)
{
LedBuff[0]=LedChar[i%10];
LedBuff[1]=LedChar[i/10%10];
LedBuff[2]=LedChar[i/100%10];
LedBuff[3]=LedChar[i/1000%10];
}else if(i>=10000&&i<100000)
{
LedBuff[0]=LedChar[i%10];
LedBuff[1]=LedChar[i/10%10];
LedBuff[2]=LedChar[i/100%10];
LedBuff[3]=LedChar[i/1000%10];
LedBuff[4]=LedChar[i/10000%10];
}else if(i>=100000&&i<1000000)
{
LedBuff[0]=LedChar[i%10];
LedBuff[1]=LedChar[i/10%10];
LedBuff[2]=LedChar[i/100%10];
LedBuff[3]=LedChar[i/1000%10];
LedBuff[4]=LedChar[i/10000%10];
LedBuff[5]=LedChar[i/100000%10];
}else if(i>=1000000&&i<10000000)
{
LedBuff[0]=LedChar[i%10];
LedBuff[1]=LedChar[i/10%10];
LedBuff[2]=LedChar[i/100%10];
LedBuff[3]=LedChar[i/1000%10];
LedBuff[4]=LedChar[i/10000%10];
LedBuff[5]=LedChar[i/100000%10];
LedBuff[6]=LedChar[i/1000000%10];
}else if(i>=10000000&&i<100000000)
{
LedBuff[0]=LedChar[i%10];
LedBuff[1]=LedChar[i/10%10];
LedBuff[2]=LedChar[i/100%10];
LedBuff[3]=LedChar[i/1000%10];
LedBuff[4]=LedChar[i/10000%10];
LedBuff[5]=LedChar[i/100000%10];
LedBuff[6]=LedChar[i/1000000%10];
LedBuff[7]=LedChar[i/10000000%10];
}else
{
LedBuff[0]=LedChar[9];
LedBuff[1]=LedChar[9];
LedBuff[2]=LedChar[9];
LedBuff[3]=LedChar[9];
LedBuff[4]=LedChar[9];
LedBuff[5]=LedChar[9];
LedBuff[6]=LedChar[9];
LedBuff[7]=LedChar[9];
}
if(number==0)
{
led3=1;
led2=1;
led1=1;
led=LedBuff[0];
number++;
}
if(number==1)
{
led3=1;
led2=1;
led1=0;
led=LedBuff[1];
number++;
}
if(number==2)
{
led3=1;
led2=0;
led1=1;
led=LedBuff[2];
number++;
}
if(number==3)
{
led3=1;
led2=0;
led1=0;
led=LedBuff[3];
number++;
}
if(number==4)
{
led3=0;
led2=1;
led1=1;
led=LedBuff[4];
number++;
}
if(number==5)
{
led3=0;
led2=1;
led1=0;
led=LedBuff[5];
number++;
}
if(number==6)
{
led3=0;
led2=0;
led1=1;
led=LedBuff[6];
number++;
}
if(number==7)
{
led3=0;
led2=0;
led1=0;
led=LedBuff[7];
number=0;
}
}
}
|
最佳答案
查看完整内容
你的程序写的太啰嗦,发生问题不易查找,简化一下即正常。
#include
#define led P0
#define LED P2
sbit led1=P2^0;
sbit led2=P2^1;
sbit led3=P2^2;
unsigned int i=0,j=0;
unsigned char k=0,s=0;
unsigned char code LedChar[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char code LedArr[]={0,1,2,3,4,5,6,7};
unsigned char LedBuff[8];
void main()
{
TMOD=0x01;
TH0=0xb1;
...
|