-
Notifications
You must be signed in to change notification settings - Fork 3
/
uart.c
127 lines (97 loc) · 1.91 KB
/
uart.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <stc12.h>
typedef unsigned char uchar;
#include "uart.h"
// Timer2 in STC15W204S
#ifdef STC15W204S
__sfr __at(0xD6) T2H;
__sfr __at(0xD7) T2L;
__sfr __at(0xAF) IE2;
#endif
void init_uart()
{
#ifndef __SOFT_UART
AUXR = 0;
SCON = 0x50; // SCON mode 1, 8bit enable ucvr
#ifdef STC11F04E // 有独立波特率发生器BRT
PCON |= 0x80; // SMOD = 1
AUXR = 0x03; // enable BRTR, ExtRAM, select BRTR
BRT = 0xFD; // Baud Rate Timer 9600
AUXR |= 0x10;
#elif defined STC15W204S
// 15W204S没有Timer1, 用Timer2做波特率发生器
T2L = (65536 - (FOSC/4/BAUD));
T2H = (65536 - (FOSC/4/BAUD)) >> 8 ;
AUXR |= 0x14; // T2 in 1T
AUXR |= 0x01; // select T2 as baud
#else
// Normal 8031
PCON |= 0x00; // SMOD = 0
TMOD |= 0x20; // Timer1 as baud
TH1 = 0xFD; // 9600
TL1 = 0xFD; // 9600
TR1 = 1;
#endif
#endif // __SOFT_UART
EA = 1;
}
#ifndef __SOFT_UART
// for SDCC, printf
void putchar(char c)
{
SBUF = c;
while(!TI) {
};
TI = 0;
}
#else // SOFT UART
#define P_TXD P3_1 /*定义模拟串口发送端,可以是任意IO*/
// for STC15F104E use *104
// for STC15F104W use *75
void BitTime(void)
{
unsigned int i;
i = ((FOSC/ 100) * 75) / 130000L - 1; //根据主时钟来计算位时间
while(--i);
}
void putchar(char dat)
{
char i;
P_TXD = 0; // start bit
BitTime();
for(i=0; i<8; i++) {
P_TXD = (dat & 1) ;
dat >>= 1;
BitTime();
}
P_TXD = 1;
BitTime(); // stop 2 bit
BitTime();
}
#endif
// 打印十进制数字
void print_num(unsigned char dat)
{
char i=0, j=0;
while (dat > 99) {
dat -= 100;
i++;
}
while (dat > 9) {
dat -= 10;
j++;
}
if (i>0) {
putchar('0'+i);
putchar('0'+j);
} else if (j>0) {
putchar('0'+j);
}
putchar('0'+dat);
}
// 打印16进制数据
char* const __code hexchar="0123456789ABCDEF";
void print_hex(char data)
{
putchar(hexchar[(data >> 4) & 0x0f]);
putchar(hexchar[data & 0x0f]);
}