STC单片机的串口通信问题

我用的是STC12C5A60S2,写了一个简单的串口通信程序(485),单片机根据接收到的字符,置相应IO口电平,并返回数据,程序如下,现在问题是单片机只能接收数据(可以实现功能),但不能发送数据。改来改去也不行。

#include "stc12c5a60s2.h"

#define uchar unsigned char
#define uint unsigned int

uchar dat = 0;
uchar neww;
uchar send;
sbit RS485_DIR = P1^7; //RS485 方向选择引脚,1发送,0接收

sbit P00=P0^0;
sbit P01=P0^1;
sbit P02=P0^2;
sbit P03=P0^3;
sbit P04=P0^4;
sbit P05=P0^5;
sbit P06=P0^6;
sbit P07=P0^7;

void delay (uchar t ) // 延时
{
uchar i,j;
for( i=0;i<t;i++)
for( j=0;j<250;j++);
}

void init()
{
TMOD = 0x20; // 定时器T1使用工作方式2
SCON = 0x50; // 工作方式1,波特率9600bps,允许接收
PCON = 0x00; // bps不倍增
AUXR = 0x00;
IP = 0x00;
TH1 = 0xfd; // 设置初值
TL1 = 0xfd;
TR1 = 1; // 开始计时(启动 T1)
EA = 1; // 开启中断
ES = 1; // 开串口中断
RS485_DIR = 0; // RS485 设置为接收方向
}
void txd_c() //发送

{
RS485_DIR = 1;
SBUF = dat;
delay(200);
RS485_DIR = 0;
}

void uart_isr() interrupt 4 // 中断
{
if (RI)
{RI = 0;
dat = SBUF;
neww = 1;}
if(TI)
{TI = 0;
send = 1;
}

}

void main() // 主函数
{

init();

while(1){
P0=dat;

if((neww ==1)&&(send ==1))

txd_c();
neww = 0;
send = 0;

}
}

第1个回答  2015-06-25
你是跟什么设备连接、发现只能接收不能发送的?
那个设备支持方向切换吗?本回答被网友采纳