关于单片机串口收发不一致的问题
时间:10-02
整理:3721RD
点击:
单片机接收PC的信息和发送的不一致,我用串口助手发000000,接收显示到TFT屏幕上是pppppp,请问是什么原因?
#include "REG51.H"
#include<stdio.h>
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
uchar ser_receive;
uchar ser_flag=0;
uchar receive_number[6];
uchar i=0;
uchar j;
#include "lcd_drive.h"
#include "gui.h"
unsigned char a[6]={'1','E','A','0','3','3'};
void init_ser()
{
TMOD=0X20;
TH1=0XFD;
TL1=0XFD; //9600
TR1=1;
EA=1;
SM0=0;
SM1=1;
REN=1;
ES=1;
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void ser() interrupt 4
{
if(RI==1)
{
RI=0;
ser_receive=SBUF;
if(ser_receive!='\0')
{
receive_number[i]=ser_receive;
i++;
if(i==6)
{
i=0;
ser_flag=1;
}
}
}
}
void main(void)
{unsigned char *p;
p=&a[0];
TFT_Init();
init_ser();
while(1)
{
if(ser_flag==1)
{
ES=0;
ser_flag=0;
for(j=0;j<6;j++)
{
SBUF=receive_number[j];
while(!TI);
TI=0;
delay(10);
}
ES=1;
}
TFT_ClearScreen(~BLACK);
strncpy(a,receive_number,6);
GUI_Write32CnChar(20, 24, "??ü°ìáê?", ~0xF800, ~0x0000);
GUI_Write32CnChar(20, 64, "??", ~0xF800, ~0x0000);
GUI_WriteASCII(55, 64, p, ~0xF800, ~0x0000);
GUI_Write32CnChar(170, 60,"ó??§", ~0xF800, ~0x0000);
GUI_Write32CnChar(20, 104, "?úμ???ì¥???è", ~0xF800, ~0x0000);
GUI_Write32CnChar(20, 144, "????", ~0xF800, ~0x0000);
//while(1);
}
}
希望懂的人帮帮忙,初学者,不是很懂,谢谢。
#include "REG51.H"
#include<stdio.h>
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
uchar ser_receive;
uchar ser_flag=0;
uchar receive_number[6];
uchar i=0;
uchar j;
#include "lcd_drive.h"
#include "gui.h"
unsigned char a[6]={'1','E','A','0','3','3'};
void init_ser()
{
TMOD=0X20;
TH1=0XFD;
TL1=0XFD; //9600
TR1=1;
EA=1;
SM0=0;
SM1=1;
REN=1;
ES=1;
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void ser() interrupt 4
{
if(RI==1)
{
RI=0;
ser_receive=SBUF;
if(ser_receive!='\0')
{
receive_number[i]=ser_receive;
i++;
if(i==6)
{
i=0;
ser_flag=1;
}
}
}
}
void main(void)
{unsigned char *p;
p=&a[0];
TFT_Init();
init_ser();
while(1)
{
if(ser_flag==1)
{
ES=0;
ser_flag=0;
for(j=0;j<6;j++)
{
SBUF=receive_number[j];
while(!TI);
TI=0;
delay(10);
}
ES=1;
}
TFT_ClearScreen(~BLACK);
strncpy(a,receive_number,6);
GUI_Write32CnChar(20, 24, "??ü°ìáê?", ~0xF800, ~0x0000);
GUI_Write32CnChar(20, 64, "??", ~0xF800, ~0x0000);
GUI_WriteASCII(55, 64, p, ~0xF800, ~0x0000);
GUI_Write32CnChar(170, 60,"ó??§", ~0xF800, ~0x0000);
GUI_Write32CnChar(20, 104, "?úμ???ì¥???è", ~0xF800, ~0x0000);
GUI_Write32CnChar(20, 144, "????", ~0xF800, ~0x0000);
//while(1);
}
}
希望懂的人帮帮忙,初学者,不是很懂,谢谢。
波特率设置的一致吗
一致的,9600啊
我也遇到了类似的问题,就是一个发送一个接收,但是收到的第一个数总不正常,以后的数就对了,
路过看见,支持一下!顶!顶!顶!