新闻中心

EEPW首页>嵌入式系统>设计应用> 模拟串口的实现单片机IO口

模拟串口的实现单片机IO口

作者: 时间:2016-11-19 来源:网络 收藏
用EBOX向我们的电力线调试模块发数据,但是要有一个控制接口,但是EBOX没有提供,所有只好用MCU来做伺服器。

一般的单片机都只有一个串口,所以必须模拟一个出来。

本文引用地址://m.amcfsurvey.com/article/201611/318339.htm

/*

sbit TXD1= P1^4;//define p14 as the analog transmit port

sbit RXD1= P1^2;//define p12 as the analog recieve port

*/

#include "REG52.H"

#define uint unsigned int

#define uchar unsigned char

#include

void WaitTF0(void);

void TIMEINI(void)//counter initial

{

TMOD=0x02;

TH0=0xA0;、//9600 BPS

TL0=TH0;

TR0=1;

TF0=0;

}

void WByte(uchar input)//send function

{

uchar i=8;

TR0=1;

TXD1=0;//begin bit

WaitTF0();

//send the Byte

while(i--)

{

TXD1=input&0x01;//send Byte from low bit to high bit

WaitTF0();

input=input>>1;//right shift

}

TXD1=1;//stop bit

WaitTF0();

TR0=0;//stop the counter

}

uchar RByte()//the recieve function

{

uchar Output=0;

uchar i=8;

while(RXD1);

i=i;

i=i;

i=i;

i=i;

i=i;

i=i;

i=i;

TR0=1;//计数器开始工作

WaitTF0();

while(i--)//接收8位数据位

{

Output>>=1;

if(RXD1)Output=Output|0x80;//recieve from the high bit to low bit

WaitTF0();

}

TR0=0;

returnOutput;

}

void WaitTF0(void)//check the counter

{

while(!TF0);

TF0=0;

}



评论


技术专区

关闭