51單片機(jī)的模擬串口代碼
#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;
}
}
}
評(píng)論