?? serial.lst
字號:
C51 COMPILER V7.06 SERIAL 03/05/2005 08:11:34 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE SERIAL
OBJECT MODULE PLACED IN Serial.OBJ
COMPILER INVOKED BY: c:\KEIL\C51\BIN\C51.exe Serial.c DB OE
stmt level source
1 //-----------------------------------------------------------------------------
2 // Copyright (c) 2002 Jim Brady
3 // Do not use commercially without author's permission
4 // Last revised August 2002
5 // Net SERIAL.C
6 //
7 // This module handles RS-232 messages and associated tasks
8 //-----------------------------------------------------------------------------
9 #include "C8051f.h"
10 #include "intrins.h"
11 #include "net.h"
12 #include "serial.h"
13
14 bit CommRecDataOverflowFlag,FlagRecComm,SendItComm;
15
16 unsigned char CommSendBufferHead, CommSendBufferTail;
17 unsigned char CommRecBufferHead, CommRecBufferTail;
18
19 unsigned char xdata CommSendBuffer[DB_SENDMAXSIZE] _at_ 0 ; //串行口緩沖區定位在內部4K XRAM中
20 unsigned char xdata CommRecBuffer[DB_RECMAXSIZE] _at_ DB_SENDMAXSIZE;
21
22 void ClearCommRecBuffer(void)
23 {
24 1 unsigned char i;
25 1 CommRecBufferHead=CommRecBufferTail=0;
26 1 CommSendBufferHead=CommSendBufferTail=0;
27 1 FlagRecComm=0;
28 1 for (i=0;i<DB_SENDMAXSIZE;i++)
29 1 {
30 2 CommSendBuffer[i]=0;
31 2 }
32 1 for (i=0;i<DB_RECMAXSIZE;i++)
33 1 {
34 2 CommRecBuffer[i]=0;
35 2 }
36 1 }
37
38 void init_serial(void)
39 {
40 1 ClearCommRecBuffer();
41 1 OpenComm();
42 1 }
43
44 void OpenComm(void)
45 {
46 1 PCON |= 0x80; // SMOD=1 (HW_UART uses Timer 1 overflow with no divide down).
47 1 TMOD |= 0x20; // Configure Timer 1 for use by UART0
48 1 CKCON |= 0x10; // Timer 1 derived from SYSCLK
49 1
50 1 RCAP2H=(65536-(SYSCLK/BAUDRATE0/32))/256;
51 1 RCAP2L=(65536-(SYSCLK/BAUDRATE0/32))%256;
52 1 TH2=RCAP2H;TL2=RCAP2L;
53 1 CT2=0; //T2:timer mode
54 1 TR2=1;
55 1 TCLK=1;RCLK=1; //說明:52,對于SIO0,可選擇T1(TCLK=0,RCLK=0)或T2(TCLK=1,RCLK=1)作為波特率發生器
C51 COMPILER V7.06 SERIAL 03/05/2005 08:11:34 PAGE 2
56 1 // SIO1只能用T1作為波特率發生器
57 1 //baud=OSC/(32*(65536-[RCAP2H,RCAP2L])
58 1 CommSendBufferHead=CommSendBufferTail=0; // set the head and tail to the base of the ring buffer
59 1 CommRecBufferHead=CommRecBufferTail=0;
60 1 FlagRecComm=0;
61 1 RI0=0; // Clear HW_UART receive and transmit
62 1 TI0=0; // complete indicators.
63 1 SCON0 = 0x50; // Configure UART0 for mode 1, receiver enabled.
64 1 ES0=1; // allow the serial interrupt
65 1 SendItComm=1;
66 1 }
67 /*
68 void SendCommChar(char ch)
69 {
70 CommSendBuffer[CommSendBufferTail]=ch;
71 CommSendBufferTail++;
72 if (CommSendBufferTail==DB_SENDMAXSIZE)
73 {
74 CommSendBufferTail=0;
75 }
76 if (SendItComm)
77 {
78 SendItComm=0;
79 SBUF0=CommSendBuffer[CommSendBufferHead];
80 }
81 while (CommSendBufferHead!=CommSendBufferTail);
82 return ;
83 }
84
85 code unsigned char hex[]="0123456789ABCDEF";
86 void SendCommHex(unsigned char senddata)//往串口發送hex碼 表示的一個字符 例如senddata=0x3A那么將向串口發送
-兩個字符'3','A'hex[]為轉換表,在前面有定義
87 {
88 unsigned char ch;
89 ch=senddata>>4;
90 SendCommChar(hex[ch]);
91 ch=senddata&0x0F;
92 SendCommChar(hex[ch]);
93 }
94 void SendCommWord(unsigned int asciiword)
95 //向串口發送一個int型的 hex碼表示的字符 例如:asciiword=0x124D 將向串口發送4個字符:'1','2','4','D'
96 {
97 unsigned char ascii;
98 ascii=asciiword>>8;
99 SendCommHex(ascii);
100 ascii=asciiword&0xff;
101 SendCommHex(ascii);
102 }
103
104 void SendCommLong(unsigned long asciilong)
105 {
106 SendCommWord(asciilong>>16);
107 SendCommWord(asciilong&0xffff);
108 }
109 */
110 void serial_send(unsigned char *base)
111 {
112 1 SendCommString(base);
113 1 }
114
115 void SendCommString(unsigned char *base)
116 {
C51 COMPILER V7.06 SERIAL 03/05/2005 08:11:34 PAGE 3
117 1 unsigned char i=0;
118 1 if (base[0]==0) return;
119 1 for (;;)
120 1 {
121 2 if (base[i]==0) break;
122 2 CommSendBuffer[CommSendBufferTail]=base[i];
123 2 CommSendBufferTail++;
124 2 if (CommSendBufferTail==DB_SENDMAXSIZE)
125 2 {
126 3 CommSendBufferTail=0;
127 3 }
128 2 i++;
129 2 }
130 1 if (SendItComm)
131 1 {
132 2 SendItComm=0;
133 2 SBUF0=CommSendBuffer[CommSendBufferHead];
134 2 }
135 1 while (CommSendBufferHead!=CommSendBufferTail);
136 1 }
137 /*
138 void SendCommBuffer(unsigned char *base, unsigned char size)
139 {
140 unsigned char i=0;
141 if (!size) { return; }
142 while (i<size)
143 {
144 CommSendBuffer[CommSendBufferTail]=base[i];
145 i++;
146 CommSendBufferTail++;
147 if (CommSendBufferTail==DB_SENDMAXSIZE)
148 {
149 CommSendBufferTail=0;
150 }
151 }
152 if (SendItComm)
153 {
154 SendItComm=0;
155 SBUF0=CommSendBuffer[CommSendBufferHead];
156 }
157 }
158 */
159 void CommISR(void) interrupt 4
160 {
161 1 if (_testbit_(TI0))
162 1 {
163 2 TI0=0;
164 2 CommSendBufferHead++;
165 2 if (CommSendBufferHead==DB_SENDMAXSIZE)
166 2 {
167 3 CommSendBufferHead=0;
168 3 }
169 2 if (CommSendBufferHead!=CommSendBufferTail)
170 2 {
171 3 SBUF0=CommSendBuffer[CommSendBufferHead]; // send the next byte
172 3 SendItComm=0;
173 3 }
174 2 else
175 2 {
176 3 SendItComm=1;
177 3 }
178 2 }
C51 COMPILER V7.06 SERIAL 03/05/2005 08:11:34 PAGE 4
179 1 if (_testbit_(RI0))
180 1 {
181 2 RI0=0;
182 2 if (CommRecBufferTail==CommRecBufferHead)
183 2 {
184 3 CommRecDataOverflowFlag=1; //接收緩沖區溢出
185 3 }
186 2 CommRecBuffer[CommRecBufferTail]=SBUF0; //receive data
187 2 CommRecBufferTail++;
188 2 if (CommRecBufferTail==DB_RECMAXSIZE)
189 2 {
190 3 CommRecBufferTail=0;
191 3 }
192 2 FlagRecComm=1;
193 2 }
194 1 }
195
196 //從接收緩沖區讀數據 ,無數據返回0,有數據返回1
197 bit GetCommChar(unsigned char idata *ch)
198 {
199 1 if (CommRecBufferTail==CommRecBufferHead) return 0;
200 1 *ch=CommRecBuffer[CommRecBufferHead];
201 1 CommRecBufferHead++;
202 1 if (CommRecBufferHead==DB_RECMAXSIZE)
203 1 {
204 2 CommRecBufferHead=0;
205 2 }
206 1 if (CommRecBufferTail==CommRecBufferHead) FlagRecComm=0;
207 1 return 1;
208 1 }
209 /*
210 //在T(0-255)毫秒內從接收緩沖區讀數據 ,無數據返回0,有數據返回1
211 bit GetCommCharWait(unsigned char idata *ch,unsigned char T) //T ms
212 {
213 Count1ms=T;*ch=0;
214 while (Count1ms)
215 {
216 if (CommRecBufferTail!=CommRecBufferHead) break;
217 }
218 if (Count1ms==0) return 0;
219 *ch=CommRecBuffer[CommRecBufferHead];
220 CommRecBufferHead++;
221 if (CommRecBufferHead==DB_RECMAXSIZE)
222 {
223 CommRecBufferHead=0;
224 }
225 return 1;
226 }
227 */
228
229 //------------------------------------------------------------------------
230 // This function converts an integer to an ASCII string. It is a
231 // normally provided as a standard library function but the Keil
232 // libraries do not include it. Caution: The string passed to this
233 // must be at least 12 bytes long
234 //------------------------------------------------------------------------
235 /*char * itoa(UINT value, char * buf, UCHAR radix)
236 {
237 UINT i;
238
239 char * ptr;
240 char * temphold;
C51 COMPILER V7.06 SERIAL 03/05/2005 08:11:34 PAGE 5
241
242 temphold = buf;
243 ptr = buf + 12;
244 *--ptr = 0; // Insert NULL char
245
246 do
247 {
248 // First create string in reverse order
249 i = (value % radix) + 0x30;
250 if(i > 0x39) i += 7;
251 *--ptr = i;
252 value = value / radix;
253 } while(value != 0);
254
255 // Next, move the string 6 places to the left
256 // Include NULL character
257 for( ; (*buf++ = *ptr++); );
258 return(temphold);
259 }*/
260
261 char * itoa(UINT value, char * buf, UCHAR radix)
262 {
263 1 UINT i,t1;
264 1 char * ptr;
265 1 char * temphold;
266 1
267 1 temphold = buf;
268 1 ptr = buf + 12;
269 1 *--ptr = 0; // Insert NULL char
270 1 t1=0;
271 1 if(radix==10)
272 1 {
273 2 do
274 2 {
275 3 // First create string in reverse order
276 3 i = (value % radix) + 0x30;
277 3 if(i > 0x39) i += 7;
278 3 *--ptr = i;
279 3 value = value / radix;
280 3 t1++;
281 3 } while(value != 0);
282 2 }
283 1
284 1 // Next, move the string 6 places to the left
285 1 // Include NULL character
286 1
287 1 else
288 1 {
289 2 radix=radix-1;
290 2 do
291 2 {
292 3 // First create string in reverse order
293 3 i = (value % radix) + 0x30;
294 3 if(i > 0x39) i += 7;
295 3 *--ptr = i;
296 3 value = value / radix;
297 3 t1++;
298 3 } while(value != 0);
299 2 if(!(t1==2))
300 2 { t1=2-t1;
301 3 for(i=0;i<t1;i++)
302 3 *--ptr=0x30;
C51 COMPILER V7.06 SERIAL 03/05/2005 08:11:34 PAGE 6
303 3 }
304 2 }
305 1 for( ; (*buf++ = *ptr++); );
306 1 return(temphold);
307 1 }
308
309
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 673 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 4 19
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 + -