题目:以16进制发送一个0-65536之间的任一数,当单片机收到后在数码管上动态显示出来,波特率自定。

这是我写的代码,仿真时如图所示,请问错了哪里啊?#include<reg51.h>#define uchar unsigned char#define uint unsigned intuchar flag,wan,qian,bai,shi,ge;uint a,shu;uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};void init();void display(uchar wan,uchar qian,uchar bai,uchar shi,uchar ge);void main(){ init(); while (1) { if(RI==1) { while (!RI); RI = 0; shu = SBUF; } wan = shu / 10000; qian = shu % 10000 /1000; bai = shu % 1000 /100; shi = shu % 100 / 10; ge = shu % 10; display(wan,qian,bai,shi,ge); }}void init(){ TMOD = 0x20; TH1 = 0xf3; TL1 = 0xf3; TR1 = 1; SCON = 0x50; EA = 1; ES = 1;}void display(uchar wan,uchar qian,uchar bai,uchar shi,uchar ge){ uchar i; P2 = 0x01; P0 = table[wan]; i = 10; while (i--); P0 = 0x00; P2 = 0x02; P0 = table[qian]; i = 10; while (i--); P0 = 0x00; P2 = 0x04; P0 = table[bai]; i = 10; while (i--); P0 = 0x00; P2 = 0x08; P0 = table[shi]; i = 10; while (i--); P0 = 0x00; P2 = 0x10; P0 = table[ge]; i = 10; while (i--); P0 = 0x00;}

以16进制发送一个0-65536之间的任一数:
这题目本身是有问题的,因为超过255的数,要用两个字节了,就是需要发送两次。
而你程序只接收一次,shu = SBUF;
这是不可能的。
发送两次,要分高8位和低8位,这还要确定是先发高8位,还是先发低8位。
下一个问题是,看你的仿真图,是用虚拟终端发的,对吗?如果是的话,你这个题目又错啦。因为虚拟终端只能以ASCII码的形式发送,不能以16进制发送的,非要用16进制发送,必须在电脑上用串口调试助手,而且是发送两个字节。
所以,不论是仿真图,还是程序都是不可能完成这个题目的,因为题目本身就是错误的。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-09-22
你好!
单片机串口是按照字节接收的,你这样是不对的追问

哪里不对啊?

我加你了,请确认下,谢谢!

相似回答