一个困扰我好几天的问题,最近我想利用一个51单片机完成串口通信,并在数码管上显示下发数据的试验。由于数码管只有8个,就想显示最低的8位数据,结果无论如何也调试不出来。
我将代码粘贴如下,望各位大侠看看,能付给一些建议。
#include <reg52.h>
#include <intrins.h>
void ConfigureUART(unsigned int baud);
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
int j=0,k=0;
unsigned char code LedChar[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
unsigned char LedBuffer[]={0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f};
unsigned char index=0;
unsigned char RxdByte[8];
unsigned char shiftwela=0xFE;
void LedScan()
{
static unsigned char i=0;
if(i<7)
{
i++;
}
else i=0;
switch(i)
{
case 0: P0=LedBuffer[0]; dula=1; dula=0;P0=0x7f;wela=1;wela=0;break;
case 1: P0=LedBuffer[1]; dula=1; dula=0;P0=0xbf;wela=1;wela=0;break;
case 2: P0=LedBuffer[2]; dula=1; dula=0;P0=0xdf;wela=1;wela=0;break;
case 3: P0=LedBuffer[3]; dula=1; dula=0;P0=0xef;wela=1;wela=0;break;
case 4: P0=LedBuffer[4]; dula=1; dula=0;P0=0xf7;wela=1;wela=0;break;
case 5: P0=LedBuffer[5]; dula=1; dula=0;P0=0xfb;wela=1;wela=0;break;
case 6: P0=LedBuffer[6]; dula=1; dula=0;P0=0xfd;wela=1;wela=0;break;
case 7: P0=LedBuffer[7]; dula=1; dula=0;P0=0xfe;wela=1;wela=0;break;
}
}
void main()
{
ConfigureUART(9600);
EA=1;
TH0=(65536-46080)/256;//5ms???,
TL0=(65536-46080)%256;//4608,5000*11.0592/12
ET0=1;
TR0=1;
TMOD |=0x01;
while(1)
{
}
}
void ConfigureUART(unsigned int baud)
{
SCON=0x50;//SM1=1£¬REN=1
TMOD &=0x0F;
TMOD |=0x20;
ES=1;//Enable UART interrupt
TH1=256-(11059200/12/32/baud);
TL1=TH1;
TR1=1;//Enable T1
ET1=0;//T1 close interrupt
}
void InterruptUART() interrupt 4
{
if(RI)
{
RI=0;
for(j=0;j<=3;j++)
{
RxdByte[j]=SBUF;
SBUF=RxdByte[j]+1;
LedBuffer[2*j]=LedChar[RxdByte[2*j]&0x0F];
LedBuffer[2*j+1]=LedChar[(RxdByte[2*j]>>4)&0x0F];
}
}
if(TI)
{
TI=0;
}
}
void Timer0Isr() interrupt 1
{
TH0=(65536-2000)/256;//50ms???,
TL0=(65536-2000)%256;//46080,50000*11.0592/12
LedScan();
}
上述最主要的问题就是SBUF的处理,我这样处理逻辑上是不对的,但是显示的效果却是可以显示低4位的数据,太神奇了。 |