📄 serial.lst
字号:
C51 COMPILER V7.50 SERIAL 02/10/2003 23:00:30 PAGE 1
C51 COMPILER V7.50, 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.50 SERIAL 02/10/2003 23:00:30 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 1
96 1 // TMOD=0x20;
97 1 TMOD=TMOD&0x0F;
98 1 TMOD=TMOD|0x20;
99 1 TL1=0xFD,TH1=0xFD;//19200 , 22.1184MHz
100 1 SCON=0x50;PCON=0x00;
101 1 TR1=1;
102 1 }
103
104 void InitSerialBuffer(void) reentrant//串口缓冲区初始化
105 {
106 1 inTxBuf=TxBuf;outTxBuf=TxBuf;
107 1 inRxBuf=RxBuf;outRxBuf=RxBuf;
108 1 ES=1;
109 1 //EA=1;
110 1 }
111
112 void serial(void) reentrant
113 { //中断在汇编中实现,去掉interrupt 4{//串口中断服务子程序
114 1 unsigned char *t;
115 1
116 1 if(TI){
117 2 TI=0;
C51 COMPILER V7.50 SERIAL 02/10/2003 23:00:30 PAGE 3
118 2 if(inTxBuf==outTxBuf) {TIflag=1;return;}//TxBuf Empty
119 2 SBUF=*outTxBuf; outTxBuf++;
120 2 if(outTxBuf==TxBuf+LenTxBuf) outTxBuf=TxBuf;
121 2 }
122 1 if(RI){
123 2 RI=0;
124 2 t=inRxBuf;t++;
125 2 if(t==RxBuf+LenRxBuf) t=RxBuf;
126 2 if(t==outRxBuf) return; //RxBuf Full
127 2 *inRxBuf=SBUF;
128 2 inRxBuf=t;
129 2 }
130 1 }
131
132 bit yygetch(unsigned char *ch) reentrant//从串口缓冲区读1字节数据
133 {
134 1 //ES=0;
135 1 if(inRxBuf==outRxBuf) {ES=1;return 0;} //RxBuf Empty
136 1 *ch=*outRxBuf; outRxBuf++;
137 1 if(outRxBuf==RxBuf+LenRxBuf) outRxBuf=RxBuf;
138 1 //ES=1;
139 1 return 1;
140 1 }
141
142 void PrintChar(unsigned char ch) reentrant//显示字符
143 {
144 1 unsigned char *t;
145 1
146 1 //ES=0;
147 1 //入临界区
148 1 #pragma ASM
149 1 PUSH IE;
150 1 CLR EA;
151 1 #pragma ENDASM
152 1 t=inTxBuf;t++;
153 1 if(t==TxBuf+LenTxBuf) t=TxBuf;
154 1 if(t==outTxBuf) {/*ES=1;*/
155 2 //出临界区
156 2 #pragma ASM
157 2 POP IE;
158 2 #pragma ENDASM
159 2 return;} //TxBuf Full
160 1 *inTxBuf=ch;
161 1 inTxBuf=t;
162 1 //ES=1;
163 1 //出临界区
164 1 #pragma ASM
165 1 POP IE;
166 1 #pragma ENDASM
167 1 if(TIflag){
168 2 TIflag=0;
169 2 TI=1;
170 2 }
171 1 }
172
173 void PrintCh(unsigned char ch) reentrant//内部使用,不建议用户看到。
174 {
175 1 if(ch>=0&&ch<=9) ch=ch+'0';
176 1 else ch=ch+'A'-10;
177 1 PrintChar(ch);
178 1 }
179
C51 COMPILER V7.50 SERIAL 02/10/2003 23:00:30 PAGE 4
180 void insidePrintByte(unsigned char Byte) reentrant//内部使用,以十六进制格式显示1个字节数据
181 {
182 1 unsigned char c;
183 1 c=Byte;
184 1 c=c>>4;
185 1 PrintCh(c);
186 1 c=Byte;c=c&0x0F;PrintCh(c);
187 1 }
188
189 void PrintByte(unsigned char Byte) reentrant//以十六进制格式显示1个字节数据
190 {
191 1 //EA=0;
192 1 //入临界区
193 1 #pragma ASM
194 1 PUSH IE;
195 1 CLR EA;
196 1 #pragma ENDASM
197 1 insidePrintByte(Byte);
198 1 //EA=1;
199 1 //出临界区
200 1 #pragma ASM
201 1 POP IE;
202 1 #pragma ENDASM
203 1 }
204
205 void insidePrintWord(unsigned int Word) reentrant//内部使用,以十六进制格式显示1个字数据
206 {
207 1 insidePrintByte(Word>>8);
208 1 insidePrintByte(Word&0xFF);
209 1 }
210
211 void PrintWord(unsigned int Word) reentrant//以十六进制格式显示1个字数据
212 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -