📄 serial.lst
字号:
C51 COMPILER V8.01 SERIAL 11/23/2005 11:07:37 PAGE 1
C51 COMPILER V8.01, COMPILATION OF MODULE SERIAL
OBJECT MODULE PLACED IN Serial.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE Serial.c BROWSE DEBUG OBJECTEXTEND
line level source
1 //-----------------------------------------------------------------------------
2 // Net SERIAL.C
3 //
4 // This module handles RS-232 messages and associated tasks
5 //-----------------------------------------------------------------------------
6 #include "C8051f.h"
7 #include "intrins.h"
8 #include "net.h"
9 #include "serial.h"
10
11 bit CommRecDataOverflowFlag,FlagRecComm,SendItComm;
12
13 unsigned char CommSendBufferHead, CommSendBufferTail;
14 unsigned char CommRecBufferHead, CommRecBufferTail;
15
16 unsigned char xdata CommSendBuffer[DB_SENDMAXSIZE] _at_ 0 ; //串行口缓冲区定位在内部4K XRAM中
17 unsigned char xdata CommRecBuffer[DB_RECMAXSIZE] _at_ DB_SENDMAXSIZE;
18
19 void ClearCommRecBuffer(void)
20 {
21 1 unsigned char i;
22 1 CommRecBufferHead=CommRecBufferTail=0;
23 1 CommSendBufferHead=CommSendBufferTail=0;
24 1 FlagRecComm=0;
25 1 for (i=0;i<DB_SENDMAXSIZE;i++)
26 1 {
27 2 CommSendBuffer[i]=0;
28 2 }
29 1 for (i=0;i<DB_RECMAXSIZE;i++)
30 1 {
31 2 CommRecBuffer[i]=0;
32 2 }
33 1 }
34
35 void init_serial(void)
36 {
37 1 ClearCommRecBuffer();
38 1 OpenComm();
39 1 }
40
41 void OpenComm(void)
42 {
43 1 PCON |= 0x80; // SMOD=1 (HW_UART uses Timer 1 overflow with no divide down).
44 1 TMOD |= 0x20; // Configure Timer 1 for use by UART0
45 1 CKCON |= 0x10; // Timer 1 derived from SYSCLK
46 1
47 1 RCAP2H=(65536-(SYSCLK/BAUDRATE0/32))/256;
48 1 RCAP2L=(65536-(SYSCLK/BAUDRATE0/32))%256;
49 1 TH2=RCAP2H;TL2=RCAP2L;
50 1 CT2=0; //T2:timer mode
51 1 TR2=1;
52 1 TCLK=1;RCLK=1; //说明:52,对于SIO0,可选择T1(TCLK=0,RCLK=0)或T2(TCLK=1,RCLK=1)作为波特率发生器
53 1 // SIO1只能用T1作为波特率发生器
54 1 //baud=OSC/(32*(65536-[RCAP2H,RCAP2L])
55 1 CommSendBufferHead=CommSendBufferTail=0; // set the head and tail to the base of the ring buffer
C51 COMPILER V8.01 SERIAL 11/23/2005 11:07:37 PAGE 2
56 1 CommRecBufferHead=CommRecBufferTail=0;
57 1 FlagRecComm=0;
58 1 RI0=0; // Clear HW_UART receive and transmit
59 1 TI0=0; // complete indicators.
60 1 SCON0 = 0x50; // Configure UART0 for mode 1, receiver enabled.
61 1 ES0=1; // allow the serial interrupt
62 1 SendItComm=1;
63 1 }
64 /*
65 void SendCommChar(char ch)
66 {
67 CommSendBuffer[CommSendBufferTail]=ch;
68 CommSendBufferTail++;
69 if (CommSendBufferTail==DB_SENDMAXSIZE)
70 {
71 CommSendBufferTail=0;
72 }
73 if (SendItComm)
74 {
75 SendItComm=0;
76 SBUF0=CommSendBuffer[CommSendBufferHead];
77 }
78 while (CommSendBufferHead!=CommSendBufferTail);
79 return ;
80 }
81
82 code unsigned char hex[]="0123456789ABCDEF";
83 void SendCommHex(unsigned char senddata)//往串口发送hex码 表示的一个字符 例如senddata=0x3A那么将向串口发送
-两个字符'3','A'hex[]为转换表,在前面有定义
84 {
85 unsigned char ch;
86 ch=senddata>>4;
87 SendCommChar(hex[ch]);
88 ch=senddata&0x0F;
89 SendCommChar(hex[ch]);
90 }
91 void SendCommWord(unsigned int asciiword)
92 //向串口发送一个int型的 hex码表示的字符 例如:asciiword=0x124D 将向串口发送4个字符:'1','2','4','D'
93 {
94 unsigned char ascii;
95 ascii=asciiword>>8;
96 SendCommHex(ascii);
97 ascii=asciiword&0xff;
98 SendCommHex(ascii);
99 }
100
101 void SendCommLong(unsigned long asciilong)
102 {
103 SendCommWord(asciilong>>16);
104 SendCommWord(asciilong&0xffff);
105 }
106 */
107 void serial_send(unsigned char *base)
108 {
109 1 SendCommString(base);
110 1 }
111
112 void SendCommString(unsigned char *base)
113 {
114 1 unsigned char i=0;
115 1 if (base[0]==0) return;
116 1 for (;;)
C51 COMPILER V8.01 SERIAL 11/23/2005 11:07:37 PAGE 3
117 1 {
118 2 if (base[i]==0) break;
119 2 CommSendBuffer[CommSendBufferTail]=base[i];
120 2 CommSendBufferTail++;
121 2 if (CommSendBufferTail==DB_SENDMAXSIZE)
122 2 {
123 3 CommSendBufferTail=0;
124 3 }
125 2 i++;
126 2 }
127 1 if (SendItComm)
128 1 {
129 2 SendItComm=0;
130 2 SBUF0=CommSendBuffer[CommSendBufferHead];
131 2 }
132 1 while (CommSendBufferHead!=CommSendBufferTail);
133 1 }
134 /*
135 void SendCommBuffer(unsigned char *base, unsigned char size)
136 {
137 unsigned char i=0;
138 if (!size) { return; }
139 while (i<size)
140 {
141 CommSendBuffer[CommSendBufferTail]=base[i];
142 i++;
143 CommSendBufferTail++;
144 if (CommSendBufferTail==DB_SENDMAXSIZE)
145 {
146 CommSendBufferTail=0;
147 }
148 }
149 if (SendItComm)
150 {
151 SendItComm=0;
152 SBUF0=CommSendBuffer[CommSendBufferHead];
153 }
154 }
155 */
156 void CommISR(void) interrupt 4
157 {
158 1 if (_testbit_(TI0))
159 1 {
160 2 TI0=0;
161 2 CommSendBufferHead++;
162 2 if (CommSendBufferHead==DB_SENDMAXSIZE)
163 2 {
164 3 CommSendBufferHead=0;
165 3 }
166 2 if (CommSendBufferHead!=CommSendBufferTail)
167 2 {
168 3 SBUF0=CommSendBuffer[CommSendBufferHead]; // send the next byte
169 3 SendItComm=0;
170 3 }
171 2 else
172 2 {
173 3 SendItComm=1;
174 3 }
175 2 }
176 1 if (_testbit_(RI0))
177 1 {
178 2 RI0=0;
C51 COMPILER V8.01 SERIAL 11/23/2005 11:07:37 PAGE 4
179 2 if (CommRecBufferTail==CommRecBufferHead)
180 2 {
181 3 CommRecDataOverflowFlag=1; //接收缓冲区溢出
182 3 }
183 2 CommRecBuffer[CommRecBufferTail]=SBUF0; //receive data
184 2 CommRecBufferTail++;
185 2 if (CommRecBufferTail==DB_RECMAXSIZE)
186 2 {
187 3 CommRecBufferTail=0;
188 3 }
189 2 FlagRecComm=1;
190 2 }
191 1 }
192
193 //从接收缓冲区读数据 ,无数据返回0,有数据返回1
194 bit GetCommChar(unsigned char idata *ch)
195 {
196 1 if (CommRecBufferTail==CommRecBufferHead) return 0;
197 1 *ch=CommRecBuffer[CommRecBufferHead];
198 1 CommRecBufferHead++;
199 1 if (CommRecBufferHead==DB_RECMAXSIZE)
200 1 {
201 2 CommRecBufferHead=0;
202 2 }
203 1 if (CommRecBufferTail==CommRecBufferHead) FlagRecComm=0;
204 1 return 1;
205 1 }
206 /*
207 //在T(0-255)毫秒内从接收缓冲区读数据 ,无数据返回0,有数据返回1
208 bit GetCommCharWait(unsigned char idata *ch,unsigned char T) //T ms
209 {
210 Count1ms=T;*ch=0;
211 while (Count1ms)
212 {
213 if (CommRecBufferTail!=CommRecBufferHead) break;
214 }
215 if (Count1ms==0) return 0;
216 *ch=CommRecBuffer[CommRecBufferHead];
217 CommRecBufferHead++;
218 if (CommRecBufferHead==DB_RECMAXSIZE)
219 {
220 CommRecBufferHead=0;
221 }
222 return 1;
223 }
224 */
225
226 //------------------------------------------------------------------------
227 // This function converts an integer to an ASCII string. It is a
228 // normally provided as a standard library function but the Keil
229 // libraries do not include it. Caution: The string passed to this
230 // must be at least 12 bytes long
231 //------------------------------------------------------------------------
232 char * itoa(UINT value, char * buf, UCHAR radix)
233 {
234 1 UINT i;
235 1 char * ptr;
236 1 char * temphold;
237 1
238 1 temphold = buf;
239 1 ptr = buf + 12;
240 1 *--ptr = 0; // Insert NULL char
C51 COMPILER V8.01 SERIAL 11/23/2005 11:07:37 PAGE 5
241 1 do
242 1 {
243 2 // First create string in reverse order
244 2 i = (value % radix) + 0x30;
245 2 if(i > 0x39) i += 7;
246 2 *--ptr = i;
247 2 value = value / radix;
248 2 } while(value != 0);
249 1
250 1 // Next, move the string 6 places to the left
251 1 // Include NULL character
252 1 for( ; (*buf++ = *ptr++); );
253 1 return(temphold);
254 1 }
255
256
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 489 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 4 17
IDATA SIZE = ---- ----
BIT SIZE = 3 ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -