#include "reg51.h"
//define baudrate const
//BAUD = 256 - SYSclk/3/BAUDRATE/M (1T:M=1; 12T:M=12)//这里的的计算公式是怎么来的
//NOTE: (SYSclk/3/BAUDRATE) must be greater than 98, (RECOMMEND GREATER THAN 11 0)
//#define BAUD 0xF400 // 1200bps @ 11 .0592MHz
//#define BAUD 0xFA00 // 2400bps @ 11 .0592MHz
//#define BAUD 0xFD00 // 4800bps @ 11 .0592MHz
//#define BAUD 0xFE80 // 9600bps @ 11 .0592MHz
//#define BAUD 0xFF40 //19200bps @ 11 .0592MHz
//#define BAUD 0xFFA0 //38400bps @ 11 .0592MHz
//#define BAUD 0xEC00 // 1200bps @ 18.432MHz
//#define BAUD 0xF600 // 2400bps @ 18.432MHz
//#define BAUD 0xFB00 // 4800bps @ 18.432MHz
//#define BAUD 0xFD80 // 9600bps @ 18.432MHz
//#define BAUD 0xFEC0 //19200bps @ 18.432MHz
#define BAUD 0xFF55 //38400bps @ 18.432MHz
//#define BAUD 0xE800 // 1200bps @ 22.11 84MHz
//#define BAUD 0xF400 // 2400bps @ 22.11 84MHz
//#define BAUD 0xFA00 // 4800bps @ 22.11 84MHz
//#define BAUD 0xFD00 // 9600bps @ 22.11 84MHz
//#define BAUD 0xFE80 //19200bps @ 22.11 84MHz
//#define BAUD 0xFF40 //38400bps @ 22.11 84MHz
//#define BAUD 0xFF80 //57600bps @ 22.11 84MHz
sfr AUXR = 0x8E;
sbit RXB = P3^0; //define UART TX/RX port
sbit TXB = P3^1;
typedef bit BOOL;
typedef unsigned char BYTE;
typedef unsigned int WORD;
BYTE TBUF,RBUF;
BYTE TDAT,RDAT;
BYTE TCNT,RCNT;//baudrate counters
BYTE TBIT,RBIT;
BOOL TING,RING;//is recive/transmit or not
BOOL TEND,REND;//mark the transmit or recive when they are complete
void UART_INIT();
BYTE t, r;
BYTE buf[16];
void main()
{
TMOD = 0x00; //timer0 in 16-bit auto reload mode
AUXR = 0x80; //timer0 working at 1T mode
TL0 = BAUD;
TH0 = BAUD>>8; //initial timer0 and set reload value
TR0 = 1; //tiemr0 start running
ET0 = 1; //enable timer0 interrupt
PT0 = 1; //improve timer0 interrupt priority
EA = 1; //open global interrupt switch
UART_INIT();
while (1)
{ //user's function
if (REND)
{
REND = 0;
buf[r++ & 0x0f] = RBUF;
}
if (TEND)
{
if (t != r)
{
TEND = 0;
TBUF = buf[t++ & 0x0f];