📄 sio.lst
字号:
C51 COMPILER V6.14 SIO 04/12/2003 22:48:18 PAGE 1
C51 COMPILER V6.14, COMPILATION OF MODULE SIO
OBJECT MODULE PLACED IN F:\当前工作夹\INTSIO2\SIO.OBJ
COMPILER INVOKED BY: C:\kCOMP\c51\BIN\C51.EXE F:\当前工作夹\INTSIO2\SIO.C DB SB OE
stmt level source
1 /*------------------------------------------------------------------------------
2 SIO.C: Serial Communication Routines.
3
4 Copyright 1995-2002 KEIL Software, Inc.
5 ------------------------------------------------------------------------------*/
6 #pragma small
7
8 #include <reg51.h>
9 #include <string.h>
10 #include "sio.h"
11
12 /*------------------------------------------------------------------------------
13 Notes:
14
15 The length of the receive and transmit buffers must be a power of 2.
16
17 Each buffer has a next_in and a next_out index.
18
19 If next_in = next_out, the buffer is empty.
20
21 (next_in - next_out) % buffer_size = the number of characters in the buffer.
22 ------------------------------------------------------------------------------*/
23 #define TBUF_SIZE 4 /*** Must be one of these powers of 2 (2,4,8,16,32,64,128) ***/
24 #define RBUF_SIZE 16 /*** Must be one of these powers of 2 (2,4,8,16,32,64,128) ***/
25
26 #define TBUF_SPACE idata /*** Memory space where the transmit buffer resides ***/
27 #define RBUF_SPACE idata /*** Memory space where the receive buffer resides ***/
28
29 #define CTRL_SPACE data /*** Memory space for the buffer indexes ***/
30
31 #define XTAL 11.059*1000000
32 /*------------------------------------------------------------------------------
33 ------------------------------------------------------------------------------*/
34 #if TBUF_SIZE < 2
#error TBUF_SIZE is too small. It must be larger than 1.
#elif TBUF_SIZE > 256
#error TBUF_SIZE is too large. It must be smaller than 257.
#elif ((TBUF_SIZE & (TBUF_SIZE-1)) != 0)
#error TBUF_SIZE must be a power of 2.
#endif
41
42 #if RBUF_SIZE < 2
#error RBUF_SIZE is too small. It must be larger than 1.
#elif RBUF_SIZE > 256
#error RBUF_SIZE is too large. It must be smaller than 257.
#elif ((RBUF_SIZE & (RBUF_SIZE-1)) != 0)
#error RBUF_SIZE must be a power of 2.
#endif
49
50 /*------------------------------------------------------------------------------
51 ------------------------------------------------------------------------------*/
52 static TBUF_SPACE unsigned char tbuf [TBUF_SIZE];
53 static RBUF_SPACE unsigned char rbuf [RBUF_SIZE];
54
55 static CTRL_SPACE unsigned char t_in = 0;
C51 COMPILER V6.14 SIO 04/12/2003 22:48:18 PAGE 2
56 static CTRL_SPACE unsigned char t_out = 0;
57
58 static CTRL_SPACE unsigned char r_in = 0;
59 static CTRL_SPACE unsigned char r_out = 0;
60
61 static bit ti_restart = 0; /* NZ if TI=1 is required */
62
63
64 /*------------------------------------------------------------------------------
65 ------------------------------------------------------------------------------*/
66 static void com_isr (void) interrupt 4
67 {
68 1 /*------------------------------------------------
69 1 Received data interrupt.
70 1 ------------------------------------------------*/
71 1 if (RI != 0)
72 1 {
73 2 RI = 0;
74 2
75 2 if (((r_in - r_out) & ~(RBUF_SIZE-1)) == 0)
76 2 {
77 3 rbuf [r_in & (RBUF_SIZE-1)] = SBUF;
78 3 r_in++;
79 3 }
80 2 }
81 1
82 1 /*------------------------------------------------
83 1 Transmitted data interrupt.
84 1 ------------------------------------------------*/
85 1 if (TI != 0)
86 1 {
87 2 TI = 0;
88 2
89 2 if (t_in != t_out)
90 2 {
91 3 SBUF = tbuf [t_out & (TBUF_SIZE-1)];
92 3 t_out++;
93 3 ti_restart = 0;
94 3 }
95 2 else
96 2 {
97 3 ti_restart = 1;
98 3 }
99 2 }
100 1
101 1 }
102
103 /*------------------------------------------------------------------------------
104 ------------------------------------------------------------------------------*/
105 #pragma disable
106
107 void com_initialize (void)
108 {
109 1 /*------------------------------------------------
110 1 Setup TIMER1 to generate the proper baud rate.
111 1 ------------------------------------------------*/
112 1 com_baudrate (1200);
113 1
114 1 /*------------------------------------------------
115 1 Clear com buffer indexes.
116 1 ------------------------------------------------*/
117 1 t_in = 0;
C51 COMPILER V6.14 SIO 04/12/2003 22:48:18 PAGE 3
118 1 t_out = 0;
119 1
120 1 r_in = 0;
121 1 r_out = 0;
122 1
123 1 /*------------------------------------------------
124 1 Setup serial port registers.
125 1 ------------------------------------------------*/
126 1 SM0 = 0; SM1 = 1; /* serial port MODE 1 */
127 1 SM2 = 0;
128 1 REN = 1; /* enable serial receiver */
129 1
130 1 RI = 0; /* clear receiver interrupt */
131 1 TI = 0; /* clear transmit interrupt */
132 1 ti_restart = 1;
133 1
134 1 ES = 1; /* enable serial interrupts */
135 1 PS = 0; /* set serial interrupts to low priority */
136 1 }
137
138 /*------------------------------------------------------------------------------
139 ------------------------------------------------------------------------------*/
140 #pragma disable
141
142 void com_baudrate (
143 unsigned baudrate)
144 {
145 1 /*------------------------------------------------
146 1 Clear transmit interrupt and buffer.
147 1 ------------------------------------------------*/
148 1 TI = 0; /* clear transmit interrupt */
149 1 t_in = 0; /* empty transmit buffer */
150 1 t_out = 0;
151 1
152 1 /*------------------------------------------------
153 1 Set timer 1 up as a baud rate generator.
154 1 ------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -