📄 sio.lst
字号:
C51 COMPILER V7.02a SIO 12/09/2002 11:28:22 PAGE 1
C51 COMPILER V7.02a, COMPILATION OF MODULE SIO
OBJECT MODULE PLACED IN SIO.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE SIO.C OBJECTADVANCED OPTIMIZE(11,SPEED) REGFILE(.\intsio2.ORC) BROWSE DEFIN
-E(XTAL=11059200) DEBUG CODE SYMBOLS PREPRINT
stmt level source
1 /*------------------------------------------------------------------------------
2 SIO.C: Serial Communication Routines.
3
4 Copyright 1995-2002 KEIL Software, Inc.
5 ------------------------------------------------------------------------------*/
6
7 #include <reg51.h>
8 #include <string.h>
9 #include "sio.h"
10
11 /*------------------------------------------------------------------------------
12 Notes:
13
14 The length of the receive and transmit buffers must be a power of 2.
15
16 Each buffer has a next_in and a next_out index.
17
18 If next_in = next_out, the buffer is empty.
19
20 (next_in - next_out) % buffer_size = the number of characters in the buffer.
21 ------------------------------------------------------------------------------*/
22 #define TBUF_SIZE 2 /*** Must be one of these powers of 2 (2,4,8,16,32,64,128) ***/
23 #define RBUF_SIZE 8 /*** Must be one of these powers of 2 (2,4,8,16,32,64,128) ***/
24
25 #define TBUF_SPACE idata /*** Memory space where the transmit buffer resides ***/
26 #define RBUF_SPACE idata /*** Memory space where the receive buffer resides ***/
27
28 #define CTRL_SPACE data /*** Memory space for the buffer indexes ***/
29
30 /*------------------------------------------------------------------------------
31 ------------------------------------------------------------------------------*/
32 #if TBUF_SIZE < 2
#error TBUF_SIZE is too small. It must be larger than 1.
#elif TBUF_SIZE > 128
#error TBUF_SIZE is too large. It must be smaller than 129.
#elif ((TBUF_SIZE & (TBUF_SIZE-1)) != 0)
#error TBUF_SIZE must be a power of 2.
#endif
39
40 #if RBUF_SIZE < 2
#error RBUF_SIZE is too small. It must be larger than 1.
#elif RBUF_SIZE > 128
#error RBUF_SIZE is too large. It must be smaller than 129.
#elif ((RBUF_SIZE & (RBUF_SIZE-1)) != 0)
#error RBUF_SIZE must be a power of 2.
#endif
47
48 /*------------------------------------------------------------------------------
49 ------------------------------------------------------------------------------*/
50 static TBUF_SPACE unsigned char tbuf [TBUF_SIZE];
51 static RBUF_SPACE unsigned char rbuf [RBUF_SIZE];
52
53 static CTRL_SPACE unsigned char t_in = 0;
54 static CTRL_SPACE unsigned char t_out = 0;
C51 COMPILER V7.02a SIO 12/09/2002 11:28:22 PAGE 2
55
56 static CTRL_SPACE unsigned char r_in = 0;
57 static CTRL_SPACE unsigned char r_out = 0;
58
59 static bit ti_restart = 0; /* NZ if TI=1 is required */
60
61
62 /*------------------------------------------------------------------------------
63 ------------------------------------------------------------------------------*/
64 static void com_isr (void) interrupt 4
65 {
66 1 /*------------------------------------------------
67 1 Received data interrupt.
68 1 ------------------------------------------------*/
69 1 if (RI != 0)
70 1 {
71 2 RI = 0;
72 2
73 2 if (((r_in - r_out) & ~(RBUF_SIZE-1)) == 0)
74 2 {
75 3 rbuf [r_in & (RBUF_SIZE-1)] = SBUF;
76 3 r_in++;
77 3 }
78 2 }
79 1
80 1 /*------------------------------------------------
81 1 Transmitted data interrupt.
82 1 ------------------------------------------------*/
83 1 if (TI != 0)
84 1 {
85 2 TI = 0;
86 2
87 2 if (t_in != t_out)
88 2 {
89 3 SBUF = tbuf [t_out & (TBUF_SIZE-1)];
90 3 t_out++;
91 3 ti_restart = 0;
92 3 }
93 2 else
94 2 {
95 3 ti_restart = 1;
96 3 }
97 2 }
98 1
99 1 }
100
101 /*------------------------------------------------------------------------------
102 ------------------------------------------------------------------------------*/
103 #pragma disable
104
105 void com_initialize (void)
106 {
107 1 /*------------------------------------------------
108 1 Setup TIMER1 to generate the proper baud rate.
109 1 ------------------------------------------------*/
110 1 com_baudrate (1200);
111 1
112 1 /*------------------------------------------------
113 1 Clear com buffer indexes.
114 1 ------------------------------------------------*/
115 1 t_in = 0;
116 1 t_out = 0;
C51 COMPILER V7.02a SIO 12/09/2002 11:28:22 PAGE 3
117 1
118 1 r_in = 0;
119 1 r_out = 0;
120 1
121 1 /*------------------------------------------------
122 1 Setup serial port registers.
123 1 ------------------------------------------------*/
124 1 SM0 = 0; SM1 = 1; /* serial port MODE 1 */
125 1 SM2 = 0;
126 1 REN = 1; /* enable serial receiver */
127 1
128 1 RI = 0; /* clear receiver interrupt */
129 1 TI = 0; /* clear transmit interrupt */
130 1 ti_restart = 1;
131 1
132 1 ES = 1; /* enable serial interrupts */
133 1 PS = 0; /* set serial interrupts to low priority */
134 1 }
135
136 /*------------------------------------------------------------------------------
137 ------------------------------------------------------------------------------*/
138 #pragma disable
139
140 void com_baudrate (
141 unsigned baudrate)
142 {
143 1 /*------------------------------------------------
144 1 Clear transmit interrupt and buffer.
145 1 ------------------------------------------------*/
146 1 TI = 0; /* clear transmit interrupt */
147 1 t_in = 0; /* empty transmit buffer */
148 1 t_out = 0;
149 1
150 1 /*------------------------------------------------
151 1 Set timer 1 up as a baud rate generator.
152 1 ------------------------------------------------*/
153 1 TR1 = 0; /* stop timer 1 */
154 1 ET1 = 0; /* disable timer 1 interrupt */
155 1
156 1 PCON |= 0x80; /* 0x80=SMOD: set serial baudrate doubler */
157 1
158 1 TMOD &= ~0xF0; /* clear timer 1 mode bits */
159 1 TMOD |= 0x20; /* put timer 1 into MODE 2 */
160 1
161 1 TH1 = (unsigned char) (256 - (XTAL / (16L * 12L * baudrate)));
162 1
163 1 TR1 = 1; /* start timer 1 */
164 1 }
165
166 /*------------------------------------------------------------------------------
167 ------------------------------------------------------------------------------*/
168 #pragma disable
169
170 char com_putchar (
171 unsigned char c)
172 {
173 1 /*------------------------------------------------
174 1 If the buffer is full, return an error value.
175 1 ------------------------------------------------*/
176 1 if (com_tbuflen () >= TBUF_SIZE)
177 1 return (-1);
178 1
C51 COMPILER V7.02a SIO 12/09/2002 11:28:22 PAGE 4
179 1 /*------------------------------------------------
180 1 Add the data to the transmit buffer. If the
181 1 transmit interrupt is disabled, then enable it.
182 1 ------------------------------------------------*/
183 1 tbuf [t_in & (TBUF_SIZE - 1)] = c;
184 1 t_in++;
185 1
186 1 if (ti_restart)
187 1 {
188 2 ti_restart = 0;
189 2 TI = 1; /* generate transmit interrupt */
190 2 }
191 1
192 1 return (0);
193 1 }
194
195 /*------------------------------------------------------------------------------
196 ------------------------------------------------------------------------------*/
197 #pragma disable
198
199 int com_getchar (void)
200 {
201 1 if (com_rbuflen () == 0)
202 1 return (-1);
203 1
204 1 return (rbuf [(r_out++) & (RBUF_SIZE - 1)]);
205 1 }
206
207 /*------------------------------------------------------------------------------
208 ------------------------------------------------------------------------------*/
209 #pragma disable
210
211 unsigned char com_rbuflen (void)
212 {
213 1 return (r_in - r_out);
214 1 }
215
216 /*------------------------------------------------------------------------------
217 ------------------------------------------------------------------------------*/
218 #pragma disable
219
220 unsigned char com_tbuflen (void)
221 {
222 1 return (t_in - t_out);
223 1 }
224
225 /*------------------------------------------------------------------------------
226 ------------------------------------------------------------------------------*/
227
C51 COMPILER V7.02a SIO 12/09/2002 11:28:22 PAGE 5
ASSEMBLY LISTING OF GENERATED OBJECT CODE
; FUNCTION com_isr (BEGIN)
PUSH ACC
PUSH PSW
MOV PSW,#00H
PUSH AR0
; SOURCE LINE # 64
; SOURCE LINE # 69
R xJNB RI,?C0001
; SOURCE LINE # 70
; SOURCE LINE # 71
CLR RI
; SOURCE LINE # 73
CLR C
R MOV A,r_in
R SUBB A,r_out
ANL A,#0F8H
R xJNZ ?C0001
; SOURCE LINE # 74
; SOURCE LINE # 75
R MOV A,r_in
ANL A,#07H
R ADD A,#LOW rbuf
MOV R0,A
MOV @R0,SBUF
; SOURCE LINE # 76
R INC r_in
; SOURCE LINE # 77
; SOURCE LINE # 78
?C0001:
; SOURCE LINE # 83
R xJNB TI,?C0006
; SOURCE LINE # 84
; SOURCE LINE # 85
CLR TI
; SOURCE LINE # 87
R MOV A,t_in
R XRL A,t_out
R xJZ ?C0004
; SOURCE LINE # 88
; SOURCE LINE # 89
R MOV A,t_out
ANL A,#01H
R ADD A,#LOW tbuf
MOV R0,A
MOV A,@R0
MOV SBUF,A
; SOURCE LINE # 90
R INC t_out
; SOURCE LINE # 91
R CLR ti_restart
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -