Files
RCU_C12_Dimming/Listings/UART.lst
caocong 6ef7f0c503 docs:发布文件
发布生产文件:BLV_C12_Dimm_V19.hex
2025-12-16 16:59:36 +08:00

532 lines
29 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
C51 COMPILER V9.01 UART 12/16/2025 16:53:47 PAGE 1
C51 COMPILER V9.01, COMPILATION OF MODULE UART
OBJECT MODULE PLACED IN .\Objects\UART.obj
COMPILER INVOKED BY: D:\Keil_v5\C51\BIN\C51.EXE UART.C OPTIMIZE(8,SPEED) BROWSE DEBUG OBJECTEXTEND PRINT(.\Listings\UART
-.lst) OBJECT(.\Objects\UART.obj)
line level source
1 /*---------------------------------------------------------------------*/
2 /* --- STC MCU Limited ------------------------------------------------*/
3 /* --- STC 1T Series MCU Demo Programme -------------------------------*/
4 /* --- Mobile: (86)13922805190 ----------------------------------------*/
5 /* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
6 /* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
7 /* --- Web: www.STCMCU.com --------------------------------------------*/
8 /* --- Web: www.STCMCUDATA.com ---------------------------------------*/
9 /* --- QQ: 800003751 -------------------------------------------------*/
10 /* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */
11 /*---------------------------------------------------------------------*/
12
13 #include "UART.h"
14 #include "timer.h"
15
16 #ifdef UART1
17 COMx_Define COM1;
18 u8 xdata TX1_Buffer[COM_TX1_Lenth]; //发送缓冲
19 u8 xdata RX1_Buffer[COM_RX1_Lenth]; //接收缓冲
20 #endif
21 #ifdef UART2
COMx_Define COM2;
u8 xdata TX2_Buffer[COM_TX2_Lenth]; //发送缓冲
u8 xdata RX2_Buffer[COM_RX2_Lenth]; //接收缓冲
#endif
26 #ifdef UART3
27 COMx_Define COM3;
28 u8 xdata TX3_Buffer[COM_TX3_Lenth]; //发送缓冲
29 u8 xdata RX3_Buffer[COM_RX3_Lenth]; //接收缓冲
30 #endif
31 #ifdef UART4
COMx_Define COM4;
u8 xdata TX4_Buffer[COM_TX4_Lenth]; //发送缓冲
u8 xdata RX4_Buffer[COM_RX4_Lenth]; //接收缓冲
#endif
36
37 u8 UART_Configuration(u8 UARTx, COMx_InitDefine *COMx)
38 {
39 1 u8 i;
40 1 u32 j;
41 1
42 1 #ifdef UART1
43 1 if(UARTx == UART1)
44 1 {
45 2 COM1.id = 1;
46 2 COM1.TX_read = 0;
47 2 COM1.TX_write = 0;
48 2 COM1.B_TX_busy = 0;
49 2 COM1.RX_Cnt = 0;
50 2 COM1.RX_TimeOut = 0;
51 2 COM1.B_RX_OK = 0;
52 2 for(i=0; i<COM_TX1_Lenth; i++) TX1_Buffer[i] = 0;
53 2 for(i=0; i<COM_RX1_Lenth; i++) RX1_Buffer[i] = 0;
54 2
C51 COMPILER V9.01 UART 12/16/2025 16:53:47 PAGE 2
55 2 if(COMx->UART_Priority > Priority_3) return 2; //错误
56 2 UART1_Priority(COMx->UART_Priority); //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_
-3
57 2 if(COMx->UART_Mode > UART_9bit_BRTx) return 2; //模式错误
58 2 SCON = (SCON & 0x3f) | COMx->UART_Mode;
59 2 if((COMx->UART_Mode == UART_9bit_BRTx) || (COMx->UART_Mode == UART_8bit_BRTx)) //可变波特率
60 2 {
61 3 j = (MAIN_Fosc / 4) / COMx->UART_BaudRate; //按1T计算
62 3 if(j >= 65536UL) return 2; //错误
63 3 j = 65536UL - j;
64 3 if(COMx->UART_BRT_Use == BRT_Timer1)
65 3 {
66 4 TR1 = 0;
67 4 AUXR &= ~0x01; //S1 BRT Use Timer1;
68 4 TMOD &= ~(1<<6); //Timer1 set As Timer
69 4 TMOD &= ~0x30; //Timer1_16bitAutoReload;
70 4 AUXR |= (1<<6); //Timer1 set as 1T mode
71 4 TH1 = (u8)(j>>8);
72 4 TL1 = (u8)j;
73 4 ET1 = 0; //禁止中断
74 4 TMOD &= ~0x40; //定时
75 4 INT_CLKO &= ~0x02; //不输出时钟
76 4 TR1 = 1;
77 4 }
78 3 else if(COMx->UART_BRT_Use == BRT_Timer2)
79 3 {
80 4 AUXR &= ~(1<<4); //Timer stop
81 4 AUXR |= 0x01; //S1 BRT Use Timer2;
82 4 AUXR &= ~(1<<3); //Timer2 set As Timer
83 4 AUXR |= (1<<2); //Timer2 set as 1T mode
84 4 TH2 = (u8)(j>>8);
85 4 TL2 = (u8)j;
86 4 IE2 &= ~(1<<2); //禁止中断
87 4 AUXR |= (1<<4); //Timer run enable
88 4 }
89 3 else return 2; //错误
90 3 }
91 2 else if(COMx->UART_Mode == UART_ShiftRight)
92 2 {
93 3 if(COMx->BaudRateDouble == ENABLE) AUXR |= (1<<5); //固定波特率SysClk/2
94 3 else AUXR &= ~(1<<5); //固定波特率SysClk/12
95 3 }
96 2 else if(COMx->UART_Mode == UART_9bit) //固定波特率SysClk*2^SMOD/64
97 2 {
98 3 if(COMx->BaudRateDouble == ENABLE) PCON |= (1<<7); //固定波特率SysClk/32
99 3 else PCON &= ~(1<<7); //固定波特率SysClk/64
100 3 }
101 2 if(COMx->UART_Interrupt == ENABLE) ES = 1; //允许中断
102 2 else ES = 0; //禁止中断
103 2 if(COMx->UART_RxEnable == ENABLE) REN = 1; //允许接收
104 2 else REN = 0; //禁止接收
105 2 P_SW1 = (P_SW1 & 0x3f) | (COMx->UART_P_SW & 0xc0); //切换IO
106 2 return 0;
107 2 }
108 1 #endif
109 1 #ifdef UART2
if(UARTx == UART2)
{
COM2.id = 2;
COM2.TX_read = 0;
COM2.TX_write = 0;
COM2.B_TX_busy = 0;
C51 COMPILER V9.01 UART 12/16/2025 16:53:47 PAGE 3
COM2.RX_Cnt = 0;
COM2.RX_TimeOut = 0;
COM2.B_RX_OK = 0;
for(i=0; i<COM_TX2_Lenth; i++) TX2_Buffer[i] = 0;
for(i=0; i<COM_RX2_Lenth; i++) RX2_Buffer[i] = 0;
if((COMx->UART_Mode == UART_9bit_BRTx) ||(COMx->UART_Mode == UART_8bit_BRTx)) //可变波特率
{
if(COMx->UART_Priority > Priority_3) return 2; //错误
UART2_Priority(COMx->UART_Priority); //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority
-_3
if(COMx->UART_Mode == UART_9bit_BRTx) S2CON |= (1<<7); //9bit
else S2CON &= ~(1<<7); //8bit
j = (MAIN_Fosc / 4) / COMx->UART_BaudRate; //按1T计算
if(j >= 65536UL) return 2; //错误
j = 65536UL - j;
AUXR &= ~(1<<4); //Timer stop
AUXR &= ~(1<<3); //Timer2 set As Timer
AUXR |= (1<<2); //Timer2 set as 1T mode
TH2 = (u8)(j>>8);
TL2 = (u8)j;
IE2 &= ~(1<<2); //禁止中断
AUXR |= (1<<4); //Timer run enable
}
else return 2; //模式错误
if(COMx->UART_Interrupt == ENABLE) IE2 |= 1; //允许中断
else IE2 &= ~1; //禁止中断
if(COMx->UART_RxEnable == ENABLE) S2CON |= (1<<4); //允许接收
else S2CON &= ~(1<<4); //禁止接收
P_SW2 = (P_SW2 & ~1) | (COMx->UART_P_SW & 0x01); //切换IO
return 0;
}
#endif
148 1 #ifdef UART3
149 1 if(UARTx == UART3)
150 1 {
151 2 COM3.id = 3;
152 2 COM3.TX_read = 0;
153 2 COM3.TX_write = 0;
154 2 COM3.B_TX_busy = 0;
155 2 COM3.RX_Cnt = 0;
156 2 COM3.RX_TimeOut = 0;
157 2 COM3.B_RX_OK = 0;
158 2 for(i=0; i<COM_TX3_Lenth; i++) TX3_Buffer[i] = 0;
159 2 for(i=0; i<COM_RX3_Lenth; i++) RX3_Buffer[i] = 0;
160 2
161 2 if((COMx->UART_Mode == UART_9bit_BRTx) || (COMx->UART_Mode == UART_8bit_BRTx)) //可变波特率
162 2 {
163 3 if(COMx->UART_Priority > Priority_3) return 2; //错误
164 3 UART3_Priority(COMx->UART_Priority); //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priorit
-y_3
165 3 if(COMx->UART_Mode == UART_9bit_BRTx) S3_9bit(); //9bit
166 3 else S3_8bit(); //8bit
167 3 j = (MAIN_Fosc / 4) / COMx->UART_BaudRate; //按1T计算
168 3 if(j >= 65536UL) return 2; //错误
169 3 j = 65536UL - j;
170 3 if(COMx->UART_BRT_Use == BRT_Timer3)
171 3 {
172 4 S3_BRT_UseTimer3(); //S3 BRT Use Timer3;
173 4 TH3 = (u8)(j>>8);
174 4 TL3 = (u8)j;
175 4 T4T3M &= 0xf0;
C51 COMPILER V9.01 UART 12/16/2025 16:53:47 PAGE 4
176 4 T4T3M |= 0x0a; //Timer3 set As Timer, 1T mode, Start timer3
177 4 }
178 3 else if(COMx->UART_BRT_Use == BRT_Timer2)
179 3 {
180 4 AUXR &= ~(1<<4); //Timer stop
181 4 S3_BRT_UseTimer2(); //S3 BRT Use Timer2;
182 4 AUXR &= ~(1<<3); //Timer2 set As Timer
183 4 AUXR |= (1<<2); //Timer2 set as 1T mode
184 4 TH2 = (u8)(j>>8);
185 4 TL2 = (u8)j;
186 4 IE2 &= ~(1<<2); //禁止中断
187 4 AUXR |= (1<<4); //Timer run enable
188 4 }
189 3 else return 2; //错误
190 3 }
191 2 else return 2; //模式错误
192 2 if(COMx->UART_Interrupt == ENABLE) S3_Int_Enable(); //允许中断
193 2 else S3_Int_Disable(); //禁止中断
194 2 if(COMx->UART_RxEnable == ENABLE) S3_RX_Enable(); //允许接收
195 2 else S3_RX_Disable(); //禁止接收
196 2 P_SW2 = (P_SW2 & ~2) | (COMx->UART_P_SW & 0x02); //切换IO
197 2 return 0;
198 2 }
199 1 #endif
200 1 #ifdef UART4
if(UARTx == UART4)
{
COM4.id = 3;
COM4.TX_read = 0;
COM4.TX_write = 0;
COM4.B_TX_busy = 0;
COM4.RX_Cnt = 0;
COM4.RX_TimeOut = 0;
COM4.B_RX_OK = 0;
for(i=0; i<COM_TX4_Lenth; i++) TX4_Buffer[i] = 0;
for(i=0; i<COM_RX4_Lenth; i++) RX4_Buffer[i] = 0;
if((COMx->UART_Mode == UART_9bit_BRTx) || (COMx->UART_Mode == UART_8bit_BRTx)) //可变波特率
{
if(COMx->UART_Priority > Priority_3) return 2; //错误
UART4_Priority(COMx->UART_Priority); //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority
-_3
if(COMx->UART_Mode == UART_9bit_BRTx) S4_9bit(); //9bit
else S4_8bit(); //8bit
j = (MAIN_Fosc / 4) / COMx->UART_BaudRate; //按1T计算
if(j >= 65536UL) return 2; //错误
j = 65536UL - j;
if(COMx->UART_BRT_Use == BRT_Timer4)
{
S4_BRT_UseTimer4(); //S4 BRT Use Timer4;
TH4 = (u8)(j>>8);
TL4 = (u8)j;
T4T3M &= 0x0f;
T4T3M |= 0xa0; //Timer4 set As Timer, 1T mode, Start timer4
}
else if(COMx->UART_BRT_Use == BRT_Timer2)
{
AUXR &= ~(1<<4); //Timer stop
S4_BRT_UseTimer2(); //S4 BRT Use Timer2;
AUXR &= ~(1<<3); //Timer2 set As Timer
AUXR |= (1<<2); //Timer2 set as 1T mode
TH2 = (u8)(j>>8);
C51 COMPILER V9.01 UART 12/16/2025 16:53:47 PAGE 5
TL2 = (u8)j;
IE2 &= ~(1<<2); //禁止中断
AUXR |= (1<<4); //Timer run enable
}
else return 2; //错误
}
else return 2; //模式错误
if(COMx->UART_Interrupt == ENABLE) S4_Int_Enable(); //允许中断
else S4_Int_Disable(); //禁止中断
if(COMx->UART_RxEnable == ENABLE) S4_RX_Enable(); //允许接收
else S4_RX_Disable(); //禁止接收
P_SW2 = (P_SW2 & ~4) | (COMx->UART_P_SW & 0x04); //切换IO
return 0;
}
#endif
252 1 return 2; //错误
253 1 }
254
255 /*********************************************************/
256
257 /********************* UART1 函数 ************************/
258 #ifdef UART1
259 void TX1_write2buff(u8 dat) //写入发送缓冲,指针+1
260 {
261 1 TX1_Buffer[COM1.TX_write] = dat; //装发送缓冲
262 1 if(++COM1.TX_write >= COM_TX1_Lenth) COM1.TX_write = 0;
263 1
264 1 if(COM1.B_TX_busy == 0) //空闲
265 1 {
266 2 COM1.B_TX_busy = 1; //标志忙
267 2 TI = 1; //触发发送中断
268 2 }
269 1 }
270
271 void PrintString1(u8 *puts)
272 {
273 1 for (; *puts != 0; puts++) TX1_write2buff(*puts); //遇到停止符0结束
274 1 }
275 void Printbuffer1(u8 *puts,u8 len)
276 {
277 1 u8 i;
278 1 for (i=0; i<len;i++) TX1_write2buff(puts[i]);
279 1 }
280
281 u8 recv_start_flag=0;
282 u32 recv_time=0;
283 void UART1_int (void) interrupt UART1_VECTOR
284 {
285 1 static u8 stage=0;
286 1 u8 dat;
287 1 if(RI)
288 1 {
289 2 RI = 0;
290 2 recv_start_flag=1;
291 2 recv_time=systick_1ms;
292 2
293 2 if(COM1.B_RX_OK == 0)
294 2 {
295 3 dat=SBUF;
296 3 if(COM1.RX_Cnt >= COM_RX1_Lenth) COM1.RX_Cnt = 0;
297 3 if(dat=='D') //连续10个'D',进入系统区
298 3 {
C51 COMPILER V9.01 UART 12/16/2025 16:53:47 PAGE 6
299 4 stage++;
300 4 }
301 3 else
302 3 {
303 4 stage=0;
304 4 }
305 3 if(stage==10)
306 3 {
307 4 IAP_CONTR=0x60;
308 4 }
309 3 RX1_Buffer[COM1.RX_Cnt++] = SBUF;
310 3 COM1.RX_TimeOut = TimeOutSet1;
311 3 }
312 2 }
313 1
314 1 if(TI)
315 1 {
316 2 TI = 0;
317 2 if(COM1.TX_read != COM1.TX_write)
318 2 {
319 3 SBUF = TX1_Buffer[COM1.TX_read];
320 3 if(++COM1.TX_read >= COM_TX1_Lenth) COM1.TX_read = 0;
321 3 }
322 2 else COM1.B_TX_busy = 0;
323 2 }
324 1 }
325 #endif
326
327 /********************* UART2 函数 ************************/
328 #ifdef UART2
void TX2_write2buff(u8 dat) //写入发送缓冲,指针+1
{
TX2_Buffer[COM2.TX_write] = dat; //装发送缓冲
if(++COM2.TX_write >= COM_TX2_Lenth) COM2.TX_write = 0;
if(COM2.B_TX_busy == 0) //空闲
{
COM2.B_TX_busy = 1; //标志忙
SET_TI2(); //触发发送中断
}
}
void PrintString2(u8 *puts)
{
for (; *puts != 0; puts++) TX2_write2buff(*puts); //遇到停止符0结束
}
void UART2_int (void) interrupt UART2_VECTOR
{
if(RI2)
{
CLR_RI2();
if(COM2.B_RX_OK == 0)
{
if(COM2.RX_Cnt >= COM_RX2_Lenth) COM2.RX_Cnt = 0;
RX2_Buffer[COM2.RX_Cnt++] = S2BUF;
COM2.RX_TimeOut = TimeOutSet2;
}
}
if(TI2)
{
C51 COMPILER V9.01 UART 12/16/2025 16:53:47 PAGE 7
CLR_TI2();
if(COM2.TX_read != COM2.TX_write)
{
S2BUF = TX2_Buffer[COM2.TX_read];
if(++COM2.TX_read >= COM_TX2_Lenth) COM2.TX_read = 0;
}
else COM2.B_TX_busy = 0;
}
}
#endif
371
372 /********************* UART3 函数 ************************/
373 #ifdef UART3
374 void TX3_write2buff(u8 dat) //写入发送缓冲,指针+1
375 {
376 1 TX3_Buffer[COM3.TX_write] = dat; //装发送缓冲
377 1 if(++COM3.TX_write >= COM_TX3_Lenth) COM3.TX_write = 0;
378 1
379 1 if(COM3.B_TX_busy == 0) //空闲
380 1 {
381 2 COM3.B_TX_busy = 1; //标志忙
382 2 SET_TI3(); //触发发送中断
383 2 }
384 1 }
385
386 void PrintString3(u8 *puts)
387 {
388 1 for (; *puts != 0; puts++) TX3_write2buff(*puts); //遇到停止符0结束
389 1 }
390
391 void Printbuffer3(u8 *puts,u8 len)
392 {
393 1 u8 i;
394 1 for (i=0; i<len;i++) TX3_write2buff(*puts++); //遇到停止符0结束
395 1 }
396
397 void UART3_int (void) interrupt UART3_VECTOR
398 {
399 1 if(RI3) //接收完成标志位
400 1 {
401 2 recv_start_flag=1;
402 2 recv_time=systick_1ms;
403 2 CLR_RI3();
404 2 if(COM3.B_RX_OK == 0)
405 2 {
406 3 if(COM3.RX_Cnt >= COM_RX3_Lenth) COM3.RX_Cnt = 0;
407 3 RX3_Buffer[COM3.RX_Cnt++] = S3BUF;
408 3 COM3.RX_TimeOut = TimeOutSet3;
409 3 }
410 2 }
411 1
412 1 if(TI3) //发送完成标志位
413 1 {
414 2 CLR_TI3();
415 2 if(COM3.TX_read != COM3.TX_write)
416 2 {
417 3 S3BUF = TX3_Buffer[COM3.TX_read];
418 3 if(++COM3.TX_read >= COM_TX3_Lenth) COM3.TX_read = 0;
419 3 }
420 2 else COM3.B_TX_busy = 0;
421 2 }
422 1 }
C51 COMPILER V9.01 UART 12/16/2025 16:53:47 PAGE 8
423 #endif
424
425 /********************* UART4 函数 ************************/
426 #ifdef UART4
void TX4_write2buff(u8 dat) //写入发送缓冲,指针+1
{
TX4_Buffer[COM4.TX_write] = dat; //装发送缓冲
if(++COM4.TX_write >= COM_TX4_Lenth) COM4.TX_write = 0;
if(COM4.B_TX_busy == 0) //空闲
{
COM4.B_TX_busy = 1; //标志忙
SET_TI4(); //触发发送中断
}
}
void PrintString4(u8 *puts)
{
for (; *puts != 0; puts++) TX4_write2buff(*puts); //遇到停止符0结束
}
void UART4_int (void) interrupt UART4_VECTOR
{
if(RI4)
{
CLR_RI4();
if(COM4.B_RX_OK == 0)
{
if(COM4.RX_Cnt >= COM_RX4_Lenth) COM4.RX_Cnt = 0;
RX4_Buffer[COM4.RX_Cnt++] = S4BUF;
COM4.RX_TimeOut = TimeOutSet4;
}
}
if(TI4)
{
CLR_TI4();
if(COM4.TX_read != COM4.TX_write)
{
S4BUF = TX4_Buffer[COM4.TX_read];
if(++COM4.TX_read >= COM_TX4_Lenth) COM4.TX_read = 0;
}
else COM4.B_TX_busy = 0;
}
}
#endif
469
470 /*************** 串口初始化函数 *****************/
471 void UART3_config(void)
472 {
473 1 COMx_InitDefine COMx_InitStructure; //结构定义
474 1 COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,U
-ART_9bit_BRTx
475 1 COMx_InitStructure.UART_BRT_Use = BRT_Timer3; //使用波特率, BRT_Timer2, BRT_Timer3 (注意: 串口2固定
-使用BRT_Timer2)
476 1 COMx_InitStructure.UART_BaudRate = 115200; //波特率, 110 ~ 115200
477 1 COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
478 1 COMx_InitStructure.UART_Interrupt = ENABLE; //中断允许, ENABLE或DISABLE
479 1 COMx_InitStructure.UART_Priority = Priority_0; //指定中断优先级(低到高) Priority_0,Priority_1,Priori
-ty_2,Priority_3
480 1 COMx_InitStructure.UART_P_SW = UART3_SW_P50_P51; //切换端口, UART3_SW_P00_P01,UART3_SW_P50_P51
481 1 UART_Configuration(UART3, &COMx_InitStructure); //初始化串口3 UART1,UART2,UART3,UART4
C51 COMPILER V9.01 UART 12/16/2025 16:53:47 PAGE 9
482 1
483 1 //PrintString3("STC8 UART3 Test Programme!\r\n"); //UART3发送一个字符串
484 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1347 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = 128 ----
PDATA SIZE = ---- ----
DATA SIZE = 20 27
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)