📄 rs232_2ok.c
字号:
#include<AT89X52.H>
#include <reg51.h>
#include<absacc.h>
#define uchar unsigned char
#define uint unsigned int
void delay_1(int count);
uchar Rebuf;
uchar Trbuf;
uchar resetflag;
void initial(void)
{
resetflag=0;
SCON=0x50; //设置串口工作在方式1,波特率为9600
TMOD=0x20; //设置T1工作在方式2,8位自动装载模式
TH1=0x98; //定时器初值,
TL1=0x98;
PCON=0x80; //令SMOD=0
P0=0xff; //上电时使P0、P2口的小灯灭
P2=0xff;
ES=1; //开串口中断
EA=1; //开CPU中断
TR1=1; //开定时器1
}
void sconinterrupt(void) interrupt 4 using 0 //串口中断
{
if(RI==1) //如果是接收中断
{
RI=0; //清接收中断标志位
Rebuf = SBUF; //将接收到的数据保存在Rebuf变量中
if(Rebuf != 0xff) //如果接收到的数不等于0xff
{
//485发送使能
Trbuf =Rebuf;
Rebuf = ~ Rebuf;//将接收到的数取反
P0=Rebuf;//送P0
delay_1(20);
SBUF = Trbuf; //再发送回上位机
}
else if(Rebuf == 0xff) //如果接收到的数是0xff
resetflag=1; //将复位标志置1
}
else //如果是发送中断
{
TI=0; //清发送中断标志位
//485接收使能
}
}
void delay_1(int count)//延时10ms
{int i,j;
for(i=0;i<count;i++)
for(j=0;j<125;j++);}
void main(void)
{
initial(); //初始化
while(1) //主循环
{
if(resetflag == 1) //判断复位标志
{resetflag=0;} //软件复位
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -