#include "reg51.h"
#define FOSC 18432000L
#define BAUD 115200
#define NONE_PARITY 0 //No check digit
#define ODD_PARITY 1 //Odd check
#define EVEN_PARITY 2 // Even parity
#define MARK_PARITY 3 //Marker verification
#define SPACE_PARITY 4 // null check
#define PARITYBIT EVEN_PARITY
#define S2RI 0x01
#define S2TI 0x02
#define S2RB8 0x04
#define S2TB8 0x08
Sfr AUXR = 0x8e;
Sfr S2CON = 0x9a;
Sfr S2BUF = 0x9b;
Sfr BRT = 0x9c;
Sfr IE2 = 0xaf;
Bit busy;
Void SendData(char dat);
Void SendString(char *s);
Void main()
{
#if (PARITYBIT == NONE_PARITY)
S2CON = 0x5a; //8-bit variable baud rate (no parity)
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
S2CON = 0xda; //9-bit variable baud rate, check digit initial 1
#elif (PARITYBIT == SPACE_PARITY)
S2CON = 0xd5; //9-bit variable baud rate, check digit initial 0
#endif
BRT = -(FOSC/32/BAUD); //Set the initial value of the independent baud rate generator
AUXR = 0x14; // Independent baud rate generator works in 1T mode
IE2 = 0x01; //Enable serial port 2 interrupt
EA = 1; //open total interruption
SendString(" STC12C5A60S2 UART2 Test !");
While(1);
}
Void Uart2() interrupt 8 using 1
{
If (S2CON & S2RI)
{
S2CON &= ~S2RI; //Clear the reception completion flag
P0 = S2BUF; //P0 shows serial data
P2 = (S2CON & S2RB8); //P2.2 shows the check digit
}
If (S2CON & S2TI)
{
S2CON &= ~S2TI; //Clear the send completion flag
Busy = 0;
}
}
Void SendData(char dat)
{
While (busy); / / wait for the last data to be sent
ACC = dat; //Get even parity bit P
If (P) //Set the check digit of the serial data according to P
{
#if (PARITYBIT == ODD_PARITY)
S2CON &= ~S2TB8; //Set the check digit to 0
#elif (PARITYBIT == EVEN_PARITY)
S2CON |= S2TB8; //Set the check digit to 1
#endif
}
Else
{
#if (PARITYBIT == ODD_PARITY)
S2CON |= S2TB8; //Set the check digit to 1
#elif (PARITYBIT == EVEN_PARITY)
S2CON &= ~S2TB8; //Set the check digit to 0
#endif
}
Busy = 1;
S2BUF = ACC; //send data
}
Void SendString(char *s)
{
While (*s) / / determine the end of the string flag
{
SendData(*s++); //Send characters
}
}
Round Hole Breadboard,Solderless Breadboard Connections,Tie Point Breadboard,Solderless Prototype Breadboard
Cixi Zhongyi Electronics Factory , https://www.zybreadboard.com