一般的单片机都只有一个串口,所以必须模拟一个出来。
/*
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;
return Output;
}
void WaitTF0(void) //check the counter
{
while(!TF0);
TF0=0;
}
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』