串口助手发数据没问题 但单片机自己却不行
串口助手发01,现象就对。但单片机送01,就不对。程序那儿需要改吗?#include<reg51.h>voidInitUART(void){TMOD=0x20;//T1选...
串口助手发01,现象就对。但单片机送01,就不对。程序那儿需要改吗?
#include <reg51.h>
void InitUART(void)
{
TMOD = 0x20; //T1选方式2
TH1=0xfd; //波特率为19200
TL1=0xfd;
TR1=1; //启动T1
SM0 = 0; //串行口设置选用方式1
SM1 = 1;
REN = 1; //允许接受数据
PCON=0x80; //波特率不加倍
EA=1; //总中断打开
ES=1; //串行口中断允许
}
void delay(void)
{
unsigned char i,j;
for(i=0;i<100;i++)
for(j=0;j<100;j++)
;
}
void SendOneByte(unsigned char c)
{
SBUF = c;
while(!TI);
TI = 0;
}
void main(void)
{
unsigned char tmp;
InitUART();
while(1){
P2=0xff;
tmp = P2;
SendOneByte(tmp);
delay();
}
while(1)
{
delay();
delay();
delay();
if(tmp==0x01)
{
SendOneByte(0x01);
}
else if(tmp==0x02)
{
SendOneByte(0x02);
}
else if(tmp==0x04)
{
SendOneByte(0x03);
}
else if(tmp==0x08)
{
SendOneByte(0x04);
}
else if(tmp==0x10)
{
SendOneByte(0x05);
}
else if(tmp==0x20)
{
SendOneByte(0x06);
}
else if(tmp==0x40)
{
SendOneByte(0x07);
}
else if(tmp==0x80)
{
SendOneByte(0x08);
}
else if(tmp !=0x00)
{
SendOneByte(0x00);
}
}
}
void UARTInterrupt(void) interrupt 4
{
unsigned char sbuf_buff = 0;
//add your code here!
if(RI==1){
sbuf_buff = SBUF;
SendOneByte(sbuf_buff) ;
if(sbuf_buff==0x01)
{
P1=0x01;
}
else if(sbuf_buff==0x02)
{
P1=0x02;
}
else if(sbuf_buff==0x03)
{
P1=0x04;
}
else if(sbuf_buff==0x04)
{
P1=0x08;
}
else if(sbuf_buff==0x05)
{
P1=0x10;
}
else if(sbuf_buff==0x06)
{
P1=0x20;
}
else if(sbuf_buff==0x07)
{
P1=0x40;
}
else if(sbuf_buff==0x08)
{
P1=0x80;
}
else if(sbuf_buff==0x00)
{
P1=0x00;
}
RI = 0;
}
} 展开
#include <reg51.h>
void InitUART(void)
{
TMOD = 0x20; //T1选方式2
TH1=0xfd; //波特率为19200
TL1=0xfd;
TR1=1; //启动T1
SM0 = 0; //串行口设置选用方式1
SM1 = 1;
REN = 1; //允许接受数据
PCON=0x80; //波特率不加倍
EA=1; //总中断打开
ES=1; //串行口中断允许
}
void delay(void)
{
unsigned char i,j;
for(i=0;i<100;i++)
for(j=0;j<100;j++)
;
}
void SendOneByte(unsigned char c)
{
SBUF = c;
while(!TI);
TI = 0;
}
void main(void)
{
unsigned char tmp;
InitUART();
while(1){
P2=0xff;
tmp = P2;
SendOneByte(tmp);
delay();
}
while(1)
{
delay();
delay();
delay();
if(tmp==0x01)
{
SendOneByte(0x01);
}
else if(tmp==0x02)
{
SendOneByte(0x02);
}
else if(tmp==0x04)
{
SendOneByte(0x03);
}
else if(tmp==0x08)
{
SendOneByte(0x04);
}
else if(tmp==0x10)
{
SendOneByte(0x05);
}
else if(tmp==0x20)
{
SendOneByte(0x06);
}
else if(tmp==0x40)
{
SendOneByte(0x07);
}
else if(tmp==0x80)
{
SendOneByte(0x08);
}
else if(tmp !=0x00)
{
SendOneByte(0x00);
}
}
}
void UARTInterrupt(void) interrupt 4
{
unsigned char sbuf_buff = 0;
//add your code here!
if(RI==1){
sbuf_buff = SBUF;
SendOneByte(sbuf_buff) ;
if(sbuf_buff==0x01)
{
P1=0x01;
}
else if(sbuf_buff==0x02)
{
P1=0x02;
}
else if(sbuf_buff==0x03)
{
P1=0x04;
}
else if(sbuf_buff==0x04)
{
P1=0x08;
}
else if(sbuf_buff==0x05)
{
P1=0x10;
}
else if(sbuf_buff==0x06)
{
P1=0x20;
}
else if(sbuf_buff==0x07)
{
P1=0x40;
}
else if(sbuf_buff==0x08)
{
P1=0x80;
}
else if(sbuf_buff==0x00)
{
P1=0x00;
}
RI = 0;
}
} 展开
2个回答
展开全部
你好!
程序修改好了,你对照原来的看看吧
#include <reg51.h>
void InitUART(void)
{
TMOD = 0x20; //T1选方式2
TH1=0xfd; //波特率为19200
TL1=0xfd;
TR1=1; //启动T1
SM0 = 0; //串行口设置选用方式1
SM1 = 1;
REN = 1; //允许接受数据
PCON=0x80; //波特率不加倍
EA=1; //总中断打开
ES=1; //串行口中断允许
}
void delay(void)
{
unsigned char i,j;
for(i=0;i<100;i++)
for(j=0;j<100;j++)
;
}
void SendOneByte(unsigned char c)
{
SBUF = c;
while(!TI);
TI = 0;
}
void main(void)
{
unsigned char tmp;
InitUART();
while(1){
}
}
void UARTInterrupt(void) interrupt 4
{
unsigned char sbuf_buff = 0;
//add your code here!
if(RI==1){
RI = 0;
sbuf_buff = SBUF;
SendOneByte(sbuf_buff) ;
if(sbuf_buff==0x01)
{
P1=0x01;
}
else if(sbuf_buff==0x02)
{
P1=0x02;
}
else if(sbuf_buff==0x03)
{
P1=0x04;
}
else if(sbuf_buff==0x04)
{
P1=0x08;
}
else if(sbuf_buff==0x05)
{
P1=0x10;
}
else if(sbuf_buff==0x06)
{
P1=0x20;
}
else if(sbuf_buff==0x07)
{
P1=0x40;
}
else if(sbuf_buff==0x08)
{
P1=0x80;
}
else if(sbuf_buff==0x00)
{
P1=0x00;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询