?? 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 + -