综述

STC12C5A60S2拥有双串口,波特率由Timer1或者独立波特率发生器(BRT)产生。

对于串口1,我们可以使用TImer1或者BRT产生波特率。
对于串口2,我们只能使用BRT产生波特率。

UART所涉及的寄存器

UART1涉及寄存器

UART2涉及寄存器

SCON

S2CON

AUXR

例子

UART1_Timer1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <reg52.h>

sfr AUXR = 0x8e;

#define TIMS 0xdc // (1/11059200)*9600*32*n = 1 n = 36 TIMS = 256-36

/************
11.0592MHZ Timer1 1t no double BAUD 9600
*************/

unsigned char num;

void UART_Init()
{
EA = 1;
ES = 1;

TR1 = 1; //enable Timer1

TMOD = 0x20; // 8bit reload

AUXR = 0x40; // UART1*12

TH1 = TL1 = TIMS;

SCON = 0x50; // Mode 1 enable RXD
PCON = 0x00; // BAUD no double

}

void main()
{
UART_Init();
while(1);
}

void UART1() interrupt 4
{
if(RI)
{
RI = 0;
num = SBUF;
SBUF = num;
while(!TI); // wait TXD finish
}

}

UART2_BRT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <reg52.h>

sfr AUXR = 0x8e;

sfr S2CON = 0x9A;
sfr S2BUF = 0x9B;
sfr BRT = 0x9c;


sfr IE2 = 0xAF;
sfr AUXR1 = 0xA2;

sbit LED = P3^0;

#define S2RI 0x01
#define S2TI 0x02

unsigned char dat;

#define TIMS (11059200L*2/32/115200)

void UART2_BRT_Init()
{
EA = 1;
IE2 =0x01;

AUXR = 0x1C; // 1t double BAUD

BRT = 256 - TIMS;

S2CON = 0x50; // 8 bit enable RXD

}



void main()
{
UART2_BRT_Init();

while(1)
{

if(dat==0x01)
{
LED = 1;
}
if(dat==0x00)
{
LED = 0;
}

}

}

void UART2_BRT() interrupt 8 using 1
{
if(S2CON&S2RI) // can not addressable
{
S2CON &= ~S2RI;
dat = S2BUF ;
S2BUF = dat;
}

if(S2CON&S2TI)
{
S2CON &= ~S2TI;
}


}