我用 C 语言为微控制器 8051 架构编写了程序,它从串口接收数据并显示在 7seg 显示器上,但数字在闪烁。你能帮我解决这个问题吗?处理器是 80C32。4051多路复用器与微控制器连接并选择显示器。有5个7seg显示器,通过ULN2803与微控制器连接。谢谢。这是我的代码:
enter code here
#include <reg51.h>
#include <stdio.h>
#include <stdlib.h>
void displayDigit(unsigned char cifra);
void delayTimer();
void showNumber(); //receive data from serial port and store data in buffer buff
void initSerial(); // serial port initialization
char receiveData();
void delay(unsigned int msdelay);
char buff[20]; // store data from serial port
unsigned int tmp1,tmp2,tmp3,tmp4,tmp5;
int tail=0; // index of element in buffer buff
sbit A=P3^4; // A is pin on 4051 Multiplexer
sbit BB=P3^5; // BB is pin on 4051 Multiplexer
sbit C=P3^6; // C is pin on 4051 Multiplexer
sbit INH=P3^7; // INH pin on 4051 Multiplexer
void main(void)
{
P1=0x00;
P3=0x03;
PT0=1;
while(1)
{
initSerial();
showNumber();
delayTimer();
}
}
// mask
void displayDigit(unsigned char cifra)
{
switch(cifra)
{
case 0: P1=0x3F; break;
case 1: P1=0x06; break;
case 2: P1=0x5B; break;
case 3: P1=0x4F; break;
case 4: P1=0x66; break;
case 5: P1=0x6D; break;
case 6: P1=0x7D; break;
case 7: P1=0x07; break;
case 8: P1=0x7F; break;
case 9: P1=0x6F; break;
}
}
// refresh display - a
void delayTimer()
{
TMOD&=0xF0;
TMOD|=0x01;
TH0=0xFF;
TL0=0xFF;
ET0=1;
EA=1;
TR0=1;
while(TF0==0);
TF0=0;
TR0=0;
}
// serial port initialization
void initSerial()
{
SCON=0xD0;
TMOD&=0x0F;
TMOD|=0x20;
TH1=0xFD;
TR1=1;
}
// receive data
char receiveData()
{
char el;
while(RI==0);
el=SBUF;
RI=0;
return el;
}
// receive data from serial port and store data in buffer
void showNumber()
{
int tail=0;
char el;
el=receiveData();
if(el==0x02) // start of communication
{
buff[tail]=el;
tail++;
while(el!=0x03)
{
el=receiveData();
if(el!=0x03)
{
buff[tail]=el;
tail++;
}
else
{
buff[tail]=el;
tail++;
buff[tail]='\0';
}
if(tail==20)
tail=0;
}
}
else if(el==0x03) // end of communication
{
buff[tail]=el;
tail++;
buff[tail]='\0';
}
}
// show on display
void timer0(void) interrupt 1
{
int i;
tmp5=buff[3]-'0';
tmp4=buff[4]-'0';
tmp3=buff[5]-'0';
tmp2=buff[6]-'0';
tmp1=buff[7]-'0';
if(tmp5==0 && tmp4==0 && tmp3==0 && tmp2==0) // 1 digit on display
{
for(i=0;i<40;i++)
{
INH=0;
C=0;
BB=0;
A=1;
displayDigit(tmp1);
delay(8);
}
}
else if(tmp5==0 && tmp4==0 && tmp3==0) // 2 digits on display
{
for(i=0;i<40;i++)
{
INH=0;
C=0;
BB=0;
A=1;
displayDigit(tmp1);
delay(8);
INH=0;
C=0;
BB=1;
A=0;
displayDigit(tmp2);
delay(8);
}
}
else if(tmp5==0 && tmp4==0) // 3 digits on display
{
for(i=0;i<40;i++)
{
INH=0;
C=0;
BB=0;
A=1;
displayDigit(tmp1);
delay(8);
INH=0;
C=0;
BB=1;
A=0;
displayDigit(tmp2);
delay(8);
INH=0;
C=0;
BB=1;
A=1;
displayDigit(tmp3);
delay(8);
}
}
else if(tmp5==0) // 4 digits on display
{
for(i=0;i<40;i++)
{
INH=0;
C=0;
BB=0;
A=1;
displayDigit(tmp1);
delay(8);
INH=0;
C=0;
BB=1;
A=0;
displayDigit(tmp2);
delay(8);
INH=0;
C=0;
BB=1;
A=1;
displayDigit(tmp3);
delay(8);
INH=0;
C=1;
BB=0;
A=0;
displayDigit(tmp4);
delay(8);
}
}
else // 5 digits on display
{
for(i=0;i<40;i++)
{
INH=0;
C=0;
BB=0;
A=1;
displayDigit(tmp1);
delay(8);
INH=0;
C=0;
BB=1;
A=0;
displayDigit(tmp2);
delay(8);
INH=0;
C=0;
BB=1;
A=1;
displayDigit(tmp3);
delay(8);
INH=0;
C=1;
BB=0;
A=0;
displayDigit(tmp4);
delay(8);
INH=0;
C=1;
BB=0;
A=1;
displayDigit(tmp5);
delay(8);
}
}
}
void delay(unsigned int msdelay)
{
unsigned int i,j;
for(i=0;i<msdelay;i++)
{
for(j=0;j<100;j++);
}
}