?? serial.lst
字號:
C51 COMPILER V7.20 SERIAL 08/23/2006 13:30:13 PAGE 1
C51 COMPILER V7.20, COMPILATION OF MODULE SERIAL
OBJECT MODULE PLACED IN serial.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE serial.c LARGE BROWSE DEBUG OBJECTEXTEND SRC(.\serial.SRC)
line level source
1 /*
2 *********************************************************************************************************
3 * uC/OS-II
4 * 實時內核
5 *
6 * (c) Copyright 1992-1998, Jean J. Labrosse, Plantation, FL
7 * 版權所有
8 *
9 * MCU-51 專用代碼
10 * KEIL C51大模式編譯
11 *
12 * 文件名 : SERIAL.C
13 * 作者 : Jean J. Labrosse
14 * 改編 : 楊屹 gdtyy@ri.gdt.com.cn 巨龍公司系統集成開發部 2002.09.27
15 *********************************************************************************************************
16 */
17
18 //**********************************************************************************
19 //楊屹 2002/08/20 第一版
20 //基于中斷的串口驅動及顯示程序
21 //聯系方法:gdtyy@ri.gdt.com.cn(2003/07/31以前有效)
22 //**********************************************************************************
23 //程序特點:
24 // 1.基于中斷,可并發執行
25 // 2.參數可配置(收發緩沖區大小,最大字符串長度,TAB鍵移動距離)
26 //**********************************************************************************
27 //使用方法:(此范例自包含,獨立于其他程序。)
28 // 先配制收發緩沖區大小等可變參數(在serial.h中的宏定義)
29 // 1.開頭加入#include <reg51.h>語句,一定要有。
30 // 2.初始化串口 InitSerial();//本例中為20MHz晶體,300波特率,模式2初始化
31 // 3.初始化串口緩沖區 InitSerialBuffer();
32 // 4.使用顯示字節,字,長字,字符,字符串,清屏函數。
33 //自包含
34 //**********************************************************************************
35 //波特率計算公式:
36 // TH1=256-(2^SMOD/32*Fosc/12*1/Bound)
37 // 其中:SMOD=0,1;Fosc=晶體或晶振頻率;Bound=波特率
38 // 本例中,SMOD=0;Fosc=20*10E6;Bound=300,所以TH1=0x52。
39 //Baud rate(20Mhz)
40 //300(52);1200(D5);2400(EA);4800(F5);9600(FB);19200(FD);38400(FF);
41 //**********************************************************************************
42 //書寫風格:
43 // 1.帶yy前綴標志的函數為楊屹改寫的等效C庫函數。
44 // 2.單個單詞用小寫,yy定義為前綴,不算一個單詞。
45 // 3.多個單詞(2個及以上),每個單詞首字母大寫。(有時變量名第一個單詞首字母小寫)
46 // 4.采用內縮風格,每次縮進8個空格。
47 //**********************************************************************************
48 //應用舉例:(可在KEIL仿真環境下運行)
49 //源程序文件:serial.h/serial.c/main.c
50 //main.c內容:
51 //#include <reg51.h>//Note:It must be added.必須在serial.c之前
52 //#include <serial.h>
53 //main()
54 //{
55 // unsigned char ch;
C51 COMPILER V7.20 SERIAL 08/23/2006 13:30:13 PAGE 2
56 //
57 // InitSerial();
58 // InitSerialBuffer();
59 // while(1){
60 // PrintStr("\n");
61 // PrintByte(90);PrintStr(" ");
62 // PrintWord(90);PrintStr(" ");
63 // PrintLong(90);PrintStr(" ");
64 // PrintChar('y');PrintChar(' ');//千萬別寫成雙引號,否則打印0(亂字符)。
65 // PrintStr("\nHello!\nI'm YangYi.\n");
66 // PrintStr("Press any key to continue...");
67 // while(!yygetch(&ch));
68 // }
69 //}
70 //**********************************************************************************
71 //建議:
72 // 你完全可以把該子程序當作函數庫使用,以便減少重復勞動,提高代碼質量。
73 //**********************************************************************************
74
75 #ifndef OS_MASTER_FILE
76 #include "includes.h"
77 #endif
78
79 //#include <includes.h>
80 //#include <serial.h>
81 //#include <reg51.h>
82
83 unsigned char TxBuf[LenTxBuf],RxBuf[LenRxBuf];//收發緩沖區實體
84 unsigned char *inTxBuf,*outTxBuf,*inRxBuf,*outRxBuf;//收發緩沖區讀寫指針
85 bit TIflag=1;//Note:It must be 1.
86
87 void InitSerial() reentrant//串口初始化
88 {
89 1 TMOD=TMOD&0x0F;
90 1 TMOD=TMOD|0x20;
91 1 TL1=0xFD,TH1=0xFD;//19200 , 22.1184MHz
92 1 SCON=0x50;PCON=0x00;
93 1 TR1=1;
94 1 }
95
96 void InitSerialBuffer(void) reentrant//串口緩沖區初始化
97 {
98 1 inTxBuf=TxBuf;outTxBuf=TxBuf;
99 1 inRxBuf=RxBuf;outRxBuf=RxBuf;
100 1 ES=1;
101 1 //EA=1;
102 1 }
103
104 void serial(void) reentrant
105 { //中斷在匯編中實現,去掉interrupt 4{//串口中斷服務子程序
106 1 unsigned char *t;
107 1
108 1 if(TI){
109 2 TI=0;
110 2 if(inTxBuf==outTxBuf) {TIflag=1;return;}//TxBuf Empty
111 2 SBUF=*outTxBuf; outTxBuf++;
112 2 if(outTxBuf==TxBuf+LenTxBuf) outTxBuf=TxBuf;
113 2 }
114 1 if(RI){
115 2 RI=0;
116 2 t=inRxBuf;t++;
117 2 if(t==RxBuf+LenRxBuf) t=RxBuf;
C51 COMPILER V7.20 SERIAL 08/23/2006 13:30:13 PAGE 3
118 2 if(t==outRxBuf) return; //RxBuf Full
119 2 *inRxBuf=SBUF;
120 2 inRxBuf=t;
121 2 }
122 1 }
123
124 bit yygetch(unsigned char *ch) reentrant//從串口緩沖區讀1字節數據
125 {
126 1 //ES=0;
127 1 if(inRxBuf==outRxBuf) {ES=1;return 0;} //RxBuf Empty
128 1 *ch=*outRxBuf; outRxBuf++;
129 1 if(outRxBuf==RxBuf+LenRxBuf) outRxBuf=RxBuf;
130 1 //ES=1;
131 1 return 1;
132 1 }
133
134 void PrintChar(unsigned char ch) reentrant//顯示字符
135 {
136 1 unsigned char *t;
137 1
138 1 //ES=0;
139 1 //入臨界區
140 1 #pragma ASM
141 1 PUSH IE;
142 1 CLR EA;
143 1 #pragma ENDASM
144 1 t=inTxBuf;t++;
145 1 if(t==TxBuf+LenTxBuf) t=TxBuf;
146 1 if(t==outTxBuf) {/*ES=1;*/
147 2 //出臨界區
148 2 #pragma ASM
149 2 POP IE;
150 2 #pragma ENDASM
151 2 return;} //TxBuf Full
152 1 *inTxBuf=ch;
153 1 inTxBuf=t;
154 1 //ES=1;
155 1 //出臨界區
156 1 #pragma ASM
157 1 POP IE;
158 1 #pragma ENDASM
159 1 if(TIflag){
160 2 TIflag=0;
161 2 TI=1;
162 2 }
163 1 }
164
165 void PrintCh(unsigned char ch) reentrant//內部使用,不建議用戶看到。
166 {
167 1 if(ch>=0&&ch<=9) ch=ch+'0';
168 1 else ch=ch+'A'-10;
169 1 PrintChar(ch);
170 1 }
171
172 void insidePrintByte(unsigned char Byte) reentrant//內部使用,以十六進制格式顯示1個字節數據
173 {
174 1 unsigned char c;
175 1 c=Byte;
176 1 c=c>>4;
177 1 PrintCh(c);
178 1 c=Byte;c=c&0x0F;PrintCh(c);
179 1 }
C51 COMPILER V7.20 SERIAL 08/23/2006 13:30:13 PAGE 4
180
181 void PrintByte(unsigned char Byte) reentrant//以十六進制格式顯示1個字節數據
182 {
183 1 //EA=0;
184 1 //入臨界區
185 1 #pragma ASM
186 1 PUSH IE;
187 1 CLR EA;
188 1 #pragma ENDASM
189 1 insidePrintByte(Byte);
190 1 //EA=1;
191 1 //出臨界區
192 1 #pragma ASM
193 1 POP IE;
194 1 #pragma ENDASM
195 1 }
196
197 void insidePrintWord(unsigned int Word) reentrant//內部使用,以十六進制格式顯示1個字數據
198 {
199 1 insidePrintByte(Word>>8);
200 1 insidePrintByte(Word&0xFF);
201 1 }
202
203 void PrintWord(unsigned int Word) reentrant//以十六進制格式顯示1個字數據
204 {
205 1 //EA=0;
206 1 //入臨界區
207 1 #pragma ASM
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -