📄 uart.lst
字号:
C51 COMPILER V7.06 UART 11/16/2008 16:00:07 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE UART
OBJECT MODULE PLACED IN UART.OBJ
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE UART.c BROWSE DEBUG OBJECTEXTEND TABS(1)
stmt level source
1 /******************************************************************
2 本程序只供学习使用,未经作者许可,不得用于其它任何用途
3
4 欢迎访问我的USB专区:http://group.ednchina.com/93/
5 欢迎访问我的blog: http://www.ednchina.com/blog/computer00
6 http://computer00.21ic.org
7
8 感谢PCB赞助商——电子园: http://bbs.cepark.com/
9
10 UART.C file
11
12 作者:电脑圈圈
13 建立日期: 2008.06.27
14 修改日期: 2008.08.05
15 版本:V1.1
16 版权所有,盗版必究。
17 Copyright(C) 电脑圈圈 2008-2018
18 All rights reserved
19 *******************************************************************/
20
21 #include <at89x52.H>
22
23 #include "UART.h"
24 #include "MyType.h"
25 #include "config.h"
26
27 idata uint8 UartBuffer[BUF_LEN];
28 idata uint8 UsbEp2Buffer[BUF_LEN];
29
30 uint8 UartBufferOutputPoint;
31 uint8 UartBufferInputPoint;
32 uint8 UsbEp2BufferOutputPoint;
33
34 uint8 UartByteCount;
35 uint8 UsbEp2ByteCount;
36
37 volatile uint8 Sending;
38
39 //使用T2做波特率发生器可以获得更多的波特率设置。
40 //删除本行将使用T1作为波特率发生器,最低波特率为300bps,最高为115200bps。
41 #define USE_T2
42
43 /********************************************************************
44 函数功能:设置串口波特率。
45 入口参数:BitRate: 需要设置的波特率。
46 返 回:实际设置的拨特率。
47 备 注:无。
48 ********************************************************************/
49 uint32 UartSetBitRate(uint32 BitRate)
50 {
51 1 #ifdef USE_T2
52 1 if(BitRate<=230400)
53 1 {
54 2 RCAP2L=0x10000-(Fclk/(BitRate*32));
55 2 RCAP2H=(0x10000-(Fclk/(BitRate*32)))>>8;
C51 COMPILER V7.06 UART 11/16/2008 16:00:07 PAGE 2
56 2 }
57 1 BitRate=(Fclk/32)/(0x10000-((((uint32)RCAP2H)<<8)+RCAP2L));
58 1 #else
if(BitRate<225)
{
BitRate=225;
PCON&=~0x80; //波特率不加倍
TH1=256-Fclk/(BitRate*12*16*2);
TL1=256-Fclk/(BitRate*12*16*2);
BitRate=(Fclk/12/32)/(0x100-((uint32)TH1));
}
else if(BitRate<1200)
{
PCON&=~0x80; //波特率不加倍
TH1=256-Fclk/(BitRate*12*16*2);
TL1=256-Fclk/(BitRate*12*16*2);
BitRate=(Fclk/12/32)/(0x100-((uint32)TH1));
}
else if(BitRate<=115200)
{
PCON|=0x80; //波特率加倍
TH1=256-Fclk/(BitRate*12*16);
TL1=256-Fclk/(BitRate*12*16);
BitRate=(Fclk/12/16)/(0x100-((uint32)TH1));
}
else
{
BitRate=115200;
PCON|=0x80; //波特率加倍
TH1=256-Fclk/(BitRate*12*16);
TL1=256-Fclk/(BitRate*12*16);
BitRate=(Fclk/12/16)/(0x100-((uint32)TH1));
}
#endif
90 1 return BitRate;
91 1 }
92 ////////////////////////End of function//////////////////////////////
93
94 /********************************************************************
95 函数功能:串口初始化。
96 入口参数:无。
97 返 回:无。
98 备 注:无。
99 ********************************************************************/
100 void InitUART(void)
101 {
102 1 EA=0;
103 1
104 1 #ifndef USE_T2
TMOD&=0x0F;
TMOD|=0x20; //定时器1工作在模式2
TCON=0x05;
#endif
109 1
110 1 SCON=0x50; //串口工作在模式1
111 1
112 1 UartSetBitRate(9600); //波特率初始化为9600
113 1
114 1 #ifdef USE_T2
115 1 T2CON=0x34; //使用T2作为波特率发生器
116 1 #endif
117 1
C51 COMPILER V7.06 UART 11/16/2008 16:00:07 PAGE 3
118 1 ES=1; //串行中断允许
119 1
120 1 #ifndef USE_T2
TR1=1; //启动定时器1
#endif
123 1
124 1 REN=1; //允许接收
125 1 EA=1; //允许中断
126 1 Sending=0; //允许发送
127 1 }
128 ////////////////////////End of function//////////////////////////////
129
130 /********************************************************************
131 函数功能:串口中断处理。
132 入口参数:无。
133 返 回:无。
134 备 注:无。
135 ********************************************************************/
136 void UartISR(void) interrupt 4
137 {
138 1 if(RI) //收到数据
139 1 {
140 2 RI=0; //清中断请求
141 2 //从SBUF读回一字节数据保存在缓冲区中
142 2 UartBuffer[UartBufferInputPoint]=SBUF;
143 2 //将输入位置下移
144 2 UartBufferInputPoint++;
145 2 //如果已经到达缓冲区末尾,则切换到缓冲区开头
146 2 if(UartBufferInputPoint>=BUF_LEN)
147 2 {
148 3 UartBufferInputPoint=0;
149 3 }
150 2 //接收字节数加1
151 2 UartByteCount++;
152 2 }
153 1 else //发送完一字节数据
154 1 {
155 2 TI=0;
156 2 Sending=0; //清正在发送标志
157 2 }
158 1 }
159 ////////////////////////End of function//////////////////////////////
160
161 /********************************************************************
162 函数功能:往串口发送一字节数据。
163 入口参数:d: 要发送的字节数据。
164 返 回:无。
165 备 注:无。
166 ********************************************************************/
167 void UartPutChar(uint8 d)
168 {
169 1 SBUF=d; //将数据写入到串口缓冲
170 1 Sending=1; //设置发送标志
171 1 while(Sending); //等待发送完毕
172 1 }
173 ////////////////////////End of function//////////////////////////////
174
175 #if (defined DEBUG0)||(defined DEBUG1)
code uint8 HexTable[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
/********************************************************************
C51 COMPILER V7.06 UART 11/16/2008 16:00:07 PAGE 4
函数功能:发送一个byte的数据。
入口参数:待发送的数据。
返 回:无。
备 注:无。
********************************************************************/
void Printc(uint8 x)
{
Sending=1;
SBUF=x;
while(Sending);
}
////////////////////////End of function//////////////////////////////
/********************************************************************
函数功能:以HEX格式发送一个byte的数据。
入口参数:待发送的数据
返 回:无。
备 注:无。
********************************************************************/
void PrintHex(uint8 x)
{
Printc('0');
Printc('x');
Printc(HexTable[x>>4]);
Printc(HexTable[x&0xf]);
Printc(' ');
}
////////////////////////End of function//////////////////////////////
/********************************************************************
函数功能:发送一个字符串。
入口参数:pd:要发送的字符串指针。
返 回:无。
备 注:无。
********************************************************************/
void Prints(uint8 * pd)
{
while((*pd)!='\0') //发送字符串,直到遇到0才结束
{
UartPutChar(*pd); //发送一个字符
pd++; //移动到下一个字符
}
}
////////////////////////End of function//////////////////////////////
/********************************************************************
函数功能:将整数转按十进制字符串发送。
入口参数:x:待显示的整数。
返 回:无。
备 注:无。
********************************************************************/
void PrintLongInt(uint32 x)
{
int8 i;
uint8 display_buffer[10];
for(i=9;i>=0;i--)
{
display_buffer[i]='0'+x%10;
x/=10;
}
for(i=0;i<9;i++)
C51 COMPILER V7.06 UART 11/16/2008 16:00:07 PAGE 5
{
if(display_buffer[i]!='0')break;
}
for(;i<10;i++)UartPutChar(display_buffer[i]);
}
////////////////////////End of function//////////////////////////////
#endif
250
251 #ifdef DEBUG0
/********************************************************************
函数功能:将短整数按十六进制发送。
入口参数:待发送的整数。
返 回:无。
备 注:无。
********************************************************************/
void PrintShortIntHex(uint16 x)
{
uint8 i;
uint8 display_buffer[7];
display_buffer[6]=0;
display_buffer[0]='0';
display_buffer[1]='x';
for(i=5;i>=2;i--) //将整数转换为4个字节的HEX值
{
display_buffer[i]=HexTable[(x&0xf)];
x>>=4;
}
Prints(display_buffer);
}
////////////////////////End of function//////////////////////////////
#endif
274
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 279 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 6 4
IDATA SIZE = 128 ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -