新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 51單片機(jī)的模擬串口代碼

51單片機(jī)的模擬串口代碼

作者: 時(shí)間:2016-11-19 來源:網(wǎng)絡(luò) 收藏
由于8位單片機(jī)一般只有一個(gè)串行接口,往往不夠用,只能自己寫一個(gè)模擬串口的程序,下面這個(gè)程序是自己早先寫的,放上來,對(duì)別人也許有點(diǎn)用,注意接收端同時(shí)接到P3.2(INT0)

#include
#define DOUBLE_BAUDRATE

#define DATA_CORRUPT 0x80
#define BUFFER_OVERFLOW 0x81

#define F_OSC 11059200

#define BAUDRATE 9600 //115200//57600//19200//9600

#define BAUDRATE_CONST (256 - F_OSC/12/BAUDRATE)

#define HALF_BAUDRATE (256 - F_OSC/12/BAUDRATE/2)

#define DELAY_TIME 50;//ms
#define DELAY_CONST (DELAY_TIME/1000 * F_OSC)/12

#define DISABLE_RECEIVE() EX0 = 0;
#define ENABLE_RECEIVE() EX0 = 1;

#define RXD P1_2
#define TXD P1_3

char idata buffer[32] ;

unsigned char bdata DataBuf;
sbit DataBuf_0 = DataBuf ^ 0;
sbit DataBuf_1 = DataBuf ^ 1;
sbit DataBuf_2 = DataBuf ^ 2;
sbit DataBuf_3 = DataBuf ^ 3;
sbit DataBuf_4 = DataBuf ^ 4;
sbit DataBuf_5 = DataBuf ^ 5;
sbit DataBuf_6 = DataBuf ^ 6;
sbit DataBuf_7 = DataBuf ^ 7;

void send(char len,char * buf)
{
char i,j;
i=j=0;
DISABLE_RECEIVE();

TMOD |= 0x02;//MODE 3
TL0 = BAUDRATE_CONST;//0x40;
TH0 = BAUDRATE_CONST;//0x40;

while(i {
DataBuf = buf[i];
TL0 = BAUDRATE_CONST;
TH0 = BAUDRATE_CONST;
TR0 = 1;

TXD = 0;//send start bit
//nop
while(!TF0);
TF0 = 0;

TXD = DataBuf_0;//send bit 0
while(!TF0);
TF0 = 0;

TXD = DataBuf_1;//send bit 1
while(!TF0);
TF0 = 0;

TXD = DataBuf_2;//send bit 2
while(!TF0);
TF0 = 0;

TXD = DataBuf_3;//send bit 3
while(!TF0);
TF0 = 0;

TXD = DataBuf_4;//send bit 4
while(!TF0);
TF0 = 0;

TXD = DataBuf_5;//send bit 5
while(!TF0);
TF0 = 0;

TXD = DataBuf_6;//send bit 6
while(!TF0);
TF0 = 0;

TXD = DataBuf_7;//send bit 7
while(!TF0);
TF0 = 0;

TXD = 1;//send end bit 8
while(!TF0);
TF0 = 0;

TR0 =0;//stop timer0
i++;//next byte
}

ENABLE_RECEIVE();
}

unsigned char bdata DataBuf;
unsigned char LastError;
char recv_len;
void exint0_receive (void) interrupt 0 using 2
{
DISABLE_RECEIVE();
TMOD |= 0x02;
TL0 = HALF_BAUDRATE;
TH0 = BAUDRATE_CONST;

TR0 = 1; //start timer0

while(!TF0); //delay 1/2 bit time to go to middle of start bit
TF0 = 0;
if(RXD == 1)
{
ENABLE_RECEIVE();
return;
}
while(!TF0);//receive data bit 0
TF0 = 0;
DataBuf_0 = RXD;

while(!TF0);//receive data bit 1
TF0 = 0;
DataBuf_1 = RXD;

while(!TF0);//receive data bit 2
TF0 = 0;
DataBuf_2 = RXD;

while(!TF0);//receive data bit 3
TF0 = 0;
DataBuf_3 = RXD;

while(!TF0);//receive data bit 4
TF0 = 0;
DataBuf_4 = RXD;

while(!TF0);//receive data bit 5
TF0 = 0;
DataBuf_5 = RXD;

while(!TF0);//receive data bit 6
TF0 = 0;
DataBuf_6 = RXD;

while(!TF0);//receive data bit 7
TF0 = 0;
DataBuf_7 = RXD;

while(!TF0);//receive end bit
TF0 = 0;

if(RXD != 1)
{
LastError = 1;
ENABLE_RECEIVE();
return ;
}
TR0 = 0;//stop timer

if(recv_len >=32)
{
LastError = BUFFER_OVERFLOW;
ENABLE_RECEIVE();
return;

}
buffer[recv_len++] = DataBuf;

ENABLE_RECEIVE();
}

void InitSimuSerial()
{
TMOD |= 0x02;
TL0 = HALF_BAUDRATE; //wait at
TH0 = BAUDRATE_CONST;

IT0 = 1;
EX0 = 1;
}

void main()
{
P0 = 0xff;
P1 = 0xff;
P2 = 0xff;
P3 = 0xff;
InitSimuSerial();
EA = 1;
while(1)
{
if(recv_len >0)
{
send(recv_len,buffer);
recv_len = 0;
}
}
}


關(guān)鍵詞: 51單片機(jī)模擬串

評(píng)論


技術(shù)專區(qū)

關(guān)閉