📄 serial.lst
字号:
C51 COMPILER V7.10 SERIAL 09/04/2007 10:27:37 PAGE 1
C51 COMPILER V7.10, 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.10 SERIAL 09/04/2007 10:27:37 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.10 SERIAL 09/04/2007 10:27:37 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.10 SERIAL 09/04/2007 10:27:37 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 + -