大家好,下面这个程序可以用串口助手发送十六进制数并在数码管上显示.因为串口通讯软件只能发2位数,所以显示也只能是2位,请教大家:如和实现三位数字的发送与显示,举例说明:比如我想发送123并在单片机的数码管上显示123,该如何发送与分离数字.程序如下:
#include<reg52.h>
#define uchar unsigned char #define uint unsigned int uchar ss,sg;
uchar code table[]={ 0xc0,0xf9,0xa4,0xb0, 0x99,0x92,0x82,0xf8, 0x80,0x90,0x88,0x83, 0xc6,0xa1,0x86,0x8e};
void Init_Uart(void);
void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); }
void display(uint i) { uchar ss,sg;
ss=i/16; sg=i%16;
P0=table[ss]; P2=0x7f; delay(2); P0=table[sg]; P2=0xbf; delay(2);
}
void main(void) { uint i = 0; Init_Uart(); while(1) { if(RI==1) { RI = 0; i = SBUF; } else display(i);
} }
void Init_Uart(void) { TMOD = 0x20; TH1 = 0xFD; TL1 = 0xFD; TR1 = 1; SCON = 0x50; PCON = 0; EA = 1; }
|