||
用EBOX向我们的电力线调试模块发数据,但是要有一个控制接口,但是EBOX没有提供,所有只好用MCU来做伺服器。
一般的单片机都只有一个串口,所以必须模拟一个出来。
/*
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 <intrins.h>
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;
return Output;
}
void WaitTF0(void) //check the counter
{
while(!TF0);
TF0=0;
}
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-9-27 09:19
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社