|
51单片机串口通信以16进制发送一个0-65536之间的任一数,当单片机收到后在数码管上动态显示出来
[复制链接]
- /*以16进制发送一个0-65536之间的任一数,
- *当单片机收到后在数码管上动态显示出来,波特率自定。
- */
- //我写的程序发送第一个数便成功显示,此后但不行了。。
- /*比如说我发fffe 数码管显示65534
- 之后再发fffe..只有第5个数码管有显示4,其他几个都不亮了。
- 我得这样子输ff 发送,,再输ff。发送。。这样子才可以显示
- 很奇怪。为什么从第二次开始就不能直接发送2个字节的数据。
- */
- #include
- #define UCHAR unsigned char
- #define UINT unsigned int
- sbit dula = P2^6;
- sbit wela = P2^7;
- /*0 1 2 3 4 5 6
- *7 8 9 A b C d
- *E F H L P U .
- */
- UCHAR code table[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d,
- 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x37, 0x5e,
- 0x79, 0x71, 0x76, 0x38, 0x73, 0x3e, 0x80,
- 0x00};
- bit flag;
- void main()
- {
- void display(UCHAR first, UCHAR second, UCHAR third, UCHAR fourth, UCHAR fifth);
- void init();
- char times;
- UINT temp;
- UCHAR first, second, third, fourth, fifth;
- init();
- while(1)
- {
- if(flag)
- {
- flag = 0;
- if(++times == 1)
- temp = SBUF * 256;
- /*//读入两次数据时才把temp值显示出来
- *因为一次只能收1个字节的数据*/
- else if(times == 2)
- {
- temp += SBUF;
- times = 0;
- first = temp % 10;
- second = temp % 100 / 10;
- third = temp % 1000 / 100;
- fourth = temp % 10000 / 1000;
- fifth = temp / 10000;
- display(fifth, fourth, third, second, first);
- ES = 0
- SBUF = temp;
- while(!TI)
- ;
- TI = 0;
- ES = 1;
- }
- }
- }
- }
- void init()
- {
- TMOD = 0x20;
- TH1 = 0XFD;
- TL1 = 0XFD;
- SM0 = 0;
- SM1 = 1;
- EA = 1;
- TR1 = 1;
- ES = 1;
- REN = 1;
- wela = 1;
- P0 = 0xff;
- wela = 0;
- }
- void delay(UINT ms)
- {
- UINT x, y;
- for(x = ms; x; x--)
- for(y = 197; y; y--)
- ;
- }
- void port() interrupt 4
- {
- RI = 0;
- flag = 1;
- }
- void display(UCHAR first, UCHAR second, UCHAR third, UCHAR fourth, UCHAR fifth)
- {
- char i;
-
- while(!flag)
- {
- for(i = 1; i <= 5; i++)
- {
- P0 = 0xff;
- wela = 1;
- switch(i)
- {
- case 1: P0 = 0xfe;break;
- case 2: P0 = 0xfd;break;
- case 3: P0 = 0xfb;break;
- case 4: P0 = 0xf7;break;
- case 5: P0 = 0xef;break;
- }
- wela = 0;
- dula = 1;
- switch(i)
- {
- case 1: P0 = table[first];break;
- case 2: P0 = table[second];break;
- case 3: P0 = table[third];break;
- case 4: P0 = table[fourth];break;
- case 5: P0 = table[fifth];break;
- }
- dula = 0;
- delay(1);
- }
- }
- }
复制代码
|
|