C51单片机串口收不到数据,能发送,接不到?为什么? 5
C51单片机串口收不到数据,能发送,接不到?为什么?#include<reg52.h>#include<intrins.h>#defineucharunsignedcha...
C51单片机串口收不到数据,能发送,接不到?为什么?
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
void Serial_Init()
{
PCON &= 0X7F;
SCON =0X20;
TH1 = 0XFD;
TL1 =TH1;
TR1 =1;
ES=1;
EA=1;
}
void main()
{
uchar a,b;
Serial_Init();
for(b=0; b<8; b++)
{
SBUF = 0X55;
while(TI==0)
;
TI=0;
}
while(1)
{
while(RI==0)
;
RI=0;
a=SBUF;
_nop_();
SBUF=a;
while(TI==0)
;
TI=0;
}
} 展开
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
void Serial_Init()
{
PCON &= 0X7F;
SCON =0X20;
TH1 = 0XFD;
TL1 =TH1;
TR1 =1;
ES=1;
EA=1;
}
void main()
{
uchar a,b;
Serial_Init();
for(b=0; b<8; b++)
{
SBUF = 0X55;
while(TI==0)
;
TI=0;
}
while(1)
{
while(RI==0)
;
RI=0;
a=SBUF;
_nop_();
SBUF=a;
while(TI==0)
;
TI=0;
}
} 展开
2个回答
展开全部
我自己写的串口部分
#ifndef SERIAL_DEF
#define SERRIAL_DEF
#include "mystddef.c"
void serial_init()
{
unsigned char tm;
ET1 = 0;
TR1 = 0;
tm = (TMOD&0x0f)|0x20;
TMOD = tm;//timer1=(0,0,1,0)
SCON = 0x50; //send & recive mode
TH1 = 0xfd;//11.0592MHz,9600
TL1 = 0xfd;
TR1 = 1;
}
void serial_send(unsigned char dat)
{
TI = 0;
RI = 0;
SBUF = dat;
while(TI==0);
TI = 0;
}
void serial_sends(unsigned char *dat, unsigned char n)
{
unsigned char i;
TI = 0;
RI = 0;
for(i=0;i<n;++i){
SBUF = dat[i];
while(TI==0);
TI = 0;
}
}
void serial_sendstr(unsigned char *dat)
{
TI = 0;
RI = 0;
while(*dat){
SBUF = *dat;
while(TI==0);
++dat;
TI = 0;
}
serial_send(0);
}
#endif
串口的中断部分:
void sc() interrupt 4
{
unsigned char dat;
if(RI){ //接收过程
dat = SBUF;
RI = 0;
}
else{ //发送过程
TI = 0;
SBUF = 0xff;
}
}
#ifndef SERIAL_DEF
#define SERRIAL_DEF
#include "mystddef.c"
void serial_init()
{
unsigned char tm;
ET1 = 0;
TR1 = 0;
tm = (TMOD&0x0f)|0x20;
TMOD = tm;//timer1=(0,0,1,0)
SCON = 0x50; //send & recive mode
TH1 = 0xfd;//11.0592MHz,9600
TL1 = 0xfd;
TR1 = 1;
}
void serial_send(unsigned char dat)
{
TI = 0;
RI = 0;
SBUF = dat;
while(TI==0);
TI = 0;
}
void serial_sends(unsigned char *dat, unsigned char n)
{
unsigned char i;
TI = 0;
RI = 0;
for(i=0;i<n;++i){
SBUF = dat[i];
while(TI==0);
TI = 0;
}
}
void serial_sendstr(unsigned char *dat)
{
TI = 0;
RI = 0;
while(*dat){
SBUF = *dat;
while(TI==0);
++dat;
TI = 0;
}
serial_send(0);
}
#endif
串口的中断部分:
void sc() interrupt 4
{
unsigned char dat;
if(RI){ //接收过程
dat = SBUF;
RI = 0;
}
else{ //发送过程
TI = 0;
SBUF = 0xff;
}
}
追问
为什么我的能发 而收不到?我把电脑的收发连在一起,用串口调试软件收发,可以收发,但发4收到0,发1至3时接收正确。还有几个1到F中的数字发收到的也是0。用了2个串口调试软件收发都一样。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询