C51 COMPILER V9.59.0.0 UART 03/09/2021 14:02:25 PAGE 1 C51 COMPILER V9.59.0.0, COMPILATION OF MODULE UART OBJECT MODULE PLACED IN .\list\UART.obj COMPILER INVOKED BY: C:\Keil_v5\C51\BIN\C51.EXE UART.C OPTIMIZE(8,SPEED) BROWSE DEBUG OBJECTEXTEND PRINT(.\list\UART.lst -) TABS(2) OBJECT(.\list\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 15 16 #ifdef UART1 COMx_Define COM1; u8 xdata TX1_Buffer[COM_TX1_Lenth]; //发送缓冲 u8 xdata RX1_Buffer[COM_RX1_Lenth]; //接收缓冲 #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 COMx_Define COM3; u8 xdata TX3_Buffer[COM_TX3_Lenth]; //发送缓冲 u8 xdata RX3_Buffer[COM_RX3_Lenth]; //接收缓冲 #endif 31 #ifdef UART4 32 COMx_Define COM4; 33 u8 xdata TX4_Buffer[COM_TX4_Lenth]; //发送缓冲 34 u8 xdata RX4_Buffer[COM_RX4_Lenth]; //接收缓冲 35 #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 if(UARTx == UART1) { COM1.id = 1; COM1.TX_read = 0; COM1.TX_write = 0; COM1.B_TX_busy = 0; COM1.RX_Cnt = 0; COM1.RX_TimeOut = 0; COM1.B_RX_OK = 0; for(i=0; iUART_Polity > Polity_3) return 2; //错误 UART1_Polity(COMx->UART_Polity); //指定中断优先级(低到高) Polity_0,Polity_1,Polity_2,Polity_3 if(COMx->UART_Mode > UART_9bit_BRTx) return 2; //模式错误 SCON = (SCON & 0x3f) | COMx->UART_Mode; if((COMx->UART_Mode == UART_9bit_BRTx) || (COMx->UART_Mode == UART_8bit_BRTx)) //可变波特率 { j = (MAIN_Fosc / 4) / COMx->UART_BaudRate; //按1T计算 if(j >= 65536UL) return 2; //错误 j = 65536UL - j; if(COMx->UART_BRT_Use == BRT_Timer1) { TR1 = 0; AUXR &= ~0x01; //S1 BRT Use Timer1; TMOD &= ~(1<<6); //Timer1 set As Timer TMOD &= ~0x30; //Timer1_16bitAutoReload; AUXR |= (1<<6); //Timer1 set as 1T mode TH1 = (u8)(j>>8); TL1 = (u8)j; ET1 = 0; //禁止中断 TMOD &= ~0x40; //定时 INT_CLKO &= ~0x02; //不输出时钟 TR1 = 1; } else if(COMx->UART_BRT_Use == BRT_Timer2) { AUXR &= ~(1<<4); //Timer stop AUXR |= 0x01; //S1 BRT Use Timer2; 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; //错误 } else if(COMx->UART_Mode == UART_ShiftRight) { if(COMx->BaudRateDouble == ENABLE) AUXR |= (1<<5); //固定波特率SysClk/2 else AUXR &= ~(1<<5); //固定波特率SysClk/12 } else if(COMx->UART_Mode == UART_9bit) //固定波特率SysClk*2^SMOD/64 { if(COMx->BaudRateDouble == ENABLE) PCON |= (1<<7); //固定波特率SysClk/32 else PCON &= ~(1<<7); //固定波特率SysClk/64 } if(COMx->UART_Interrupt == ENABLE) ES = 1; //允许中断 else ES = 0; //禁止中断 if(COMx->UART_RxEnable == ENABLE) REN = 1; //允许接收 else REN = 0; //禁止接收 P_SW1 = (P_SW1 & 0x3f) | (COMx->UART_P_SW & 0xc0); //切换IO return 0; } #endif 109 1 #ifdef UART2 if(UARTx == UART2) { COM2.id = 2; COM2.TX_read = 0; COM2.TX_write = 0; COM2.B_TX_busy = 0; COM2.RX_Cnt = 0; C51 COMPILER V9.59.0.0 UART 03/09/2021 14:02:25 PAGE 3 COM2.RX_TimeOut = 0; COM2.B_RX_OK = 0; for(i=0; iUART_Mode == UART_9bit_BRTx) ||(COMx->UART_Mode == UART_8bit_BRTx)) //可变波特率 { if(COMx->UART_Polity > Polity_3) return 2; //错误 UART2_Polity(COMx->UART_Polity); //指定中断优先级(低到高) Polity_0,Polity_1,Polity_2,Polity_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 if(UARTx == UART3) { COM3.id = 3; COM3.TX_read = 0; COM3.TX_write = 0; COM3.B_TX_busy = 0; COM3.RX_Cnt = 0; COM3.RX_TimeOut = 0; COM3.B_RX_OK = 0; for(i=0; iUART_Mode == UART_9bit_BRTx) || (COMx->UART_Mode == UART_8bit_BRTx)) //可变波特率 { if(COMx->UART_Polity > Polity_3) return 2; //错误 UART3_Polity(COMx->UART_Polity); //指定中断优先级(低到高) Polity_0,Polity_1,Polity_2,Polity_3 if(COMx->UART_Mode == UART_9bit_BRTx) S3_9bit(); //9bit else S3_8bit(); //8bit j = (MAIN_Fosc / 4) / COMx->UART_BaudRate; //按1T计算 if(j >= 65536UL) return 2; //错误 j = 65536UL - j; if(COMx->UART_BRT_Use == BRT_Timer3) { S3_BRT_UseTimer3(); //S3 BRT Use Timer3; TH3 = (u8)(j>>8); TL3 = (u8)j; T4T3M &= 0xf0; T4T3M |= 0x0a; //Timer3 set As Timer, 1T mode, Start timer3 } else if(COMx->UART_BRT_Use == BRT_Timer2) C51 COMPILER V9.59.0.0 UART 03/09/2021 14:02:25 PAGE 4 { AUXR &= ~(1<<4); //Timer stop S3_BRT_UseTimer2(); //S3 BRT Use Timer2; 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; //错误 } else return 2; //模式错误 if(COMx->UART_Interrupt == ENABLE) S3_Int_Enable(); //允许中断 else S3_Int_Disable(); //禁止中断 if(COMx->UART_RxEnable == ENABLE) S3_RX_Enable(); //允许接收 else S3_RX_Disable(); //禁止接收 P_SW2 = (P_SW2 & ~2) | (COMx->UART_P_SW & 0x02); //切换IO return 0; } #endif 200 1 #ifdef UART4 201 1 if(UARTx == UART4) 202 1 { 203 2 COM4.id = 3; 204 2 COM4.TX_read = 0; 205 2 COM4.TX_write = 0; 206 2 COM4.B_TX_busy = 0; 207 2 COM4.RX_Cnt = 0; 208 2 COM4.RX_TimeOut = 0; 209 2 COM4.B_RX_OK = 0; 210 2 for(i=0; iUART_Mode == UART_9bit_BRTx) || (COMx->UART_Mode == UART_8bit_BRTx)) //可变波特率 214 2 { 215 3 if(COMx->UART_Polity > Polity_3) return 2; //错误 216 3 UART4_Polity(COMx->UART_Polity); //指定中断优先级(低到高) Polity_0,Polity_1,Polity_2,Polity_3 217 3 if(COMx->UART_Mode == UART_9bit_BRTx) S4_9bit(); //9bit 218 3 else S4_8bit(); //8bit 219 3 j = (MAIN_Fosc / 4) / COMx->UART_BaudRate; //按1T计算 220 3 if(j >= 65536UL) return 2; //错误 221 3 j = 65536UL - j; 222 3 if(COMx->UART_BRT_Use == BRT_Timer4) 223 3 { 224 4 S4_BRT_UseTimer4(); //S4 BRT Use Timer4; 225 4 TH4 = (u8)(j>>8); 226 4 TL4 = (u8)j; 227 4 T4T3M &= 0x0f; 228 4 T4T3M |= 0xa0; //Timer4 set As Timer, 1T mode, Start timer4 229 4 } 230 3 else if(COMx->UART_BRT_Use == BRT_Timer2) 231 3 { 232 4 AUXR &= ~(1<<4); //Timer stop 233 4 S4_BRT_UseTimer2(); //S4 BRT Use Timer2; 234 4 AUXR &= ~(1<<3); //Timer2 set As Timer 235 4 AUXR |= (1<<2); //Timer2 set as 1T mode 236 4 TH2 = (u8)(j>>8); 237 4 TL2 = (u8)j; 238 4 IE2 &= ~(1<<2); //禁止中断 239 4 AUXR |= (1<<4); //Timer run enable 240 4 } C51 COMPILER V9.59.0.0 UART 03/09/2021 14:02:25 PAGE 5 241 3 else return 2; //错误 242 3 } 243 2 else return 2; //模式错误 244 2 if(COMx->UART_Interrupt == ENABLE) S4_Int_Enable(); //允许中断 245 2 else S4_Int_Disable(); //禁止中断 246 2 if(COMx->UART_RxEnable == ENABLE) S4_RX_Enable(); //允许接收 247 2 else S4_RX_Disable(); //禁止接收 248 2 P_SW2 = (P_SW2 & ~4) | (COMx->UART_P_SW & 0x04); //切换IO 249 2 return 0; 250 2 } 251 1 #endif 252 1 return 2; //错误 253 1 } 254 255 /*********************************************************/ 256 257 /********************* UART1 函数 ************************/ 258 #ifdef UART1 void TX1_write2buff(u8 dat) //写入发送缓冲,指针+1 { TX1_Buffer[COM1.TX_write] = dat; //装发送缓冲 if(++COM1.TX_write >= COM_TX1_Lenth) COM1.TX_write = 0; if(COM1.B_TX_busy == 0) //空闲 { COM1.B_TX_busy = 1; //标志忙 TI = 1; //触发发送中断 } } void PrintString1(u8 *puts) { for (; *puts != 0; puts++) TX1_write2buff(*puts); //遇到停止符0结束 } void UART1_int (void) interrupt UART1_VECTOR { if(RI) { RI = 0; if(COM1.B_RX_OK == 0) { if(COM1.RX_Cnt >= COM_RX1_Lenth) COM1.RX_Cnt = 0; RX1_Buffer[COM1.RX_Cnt++] = SBUF; COM1.RX_TimeOut = TimeOutSet1; } } if(TI) { TI = 0; if(COM1.TX_read != COM1.TX_write) { SBUF = TX1_Buffer[COM1.TX_read]; if(++COM1.TX_read >= COM_TX1_Lenth) COM1.TX_read = 0; } else COM1.B_TX_busy = 0; } } #endif 301 302 /********************* UART2 函数 ************************/ C51 COMPILER V9.59.0.0 UART 03/09/2021 14:02:25 PAGE 6 303 #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) { 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 346 347 /********************* UART3 函数 ************************/ 348 #ifdef UART3 void TX3_write2buff(u8 dat) //写入发送缓冲,指针+1 { TX3_Buffer[COM3.TX_write] = dat; //装发送缓冲 if(++COM3.TX_write >= COM_TX3_Lenth) COM3.TX_write = 0; if(COM3.B_TX_busy == 0) //空闲 { COM3.B_TX_busy = 1; //标志忙 SET_TI3(); //触发发送中断 } } void PrintString3(u8 *puts) { for (; *puts != 0; puts++) TX3_write2buff(*puts); //遇到停止符0结束 } C51 COMPILER V9.59.0.0 UART 03/09/2021 14:02:25 PAGE 7 void UART3_int (void) interrupt UART3_VECTOR { if(RI3) { CLR_RI3(); if(COM3.B_RX_OK == 0) { if(COM3.RX_Cnt >= COM_RX3_Lenth) COM3.RX_Cnt = 0; RX3_Buffer[COM3.RX_Cnt++] = S3BUF; COM3.RX_TimeOut = TimeOutSet3; } } if(TI3) { CLR_TI3(); if(COM3.TX_read != COM3.TX_write) { S3BUF = TX3_Buffer[COM3.TX_read]; if(++COM3.TX_read >= COM_TX3_Lenth) COM3.TX_read = 0; } else COM3.B_TX_busy = 0; } } #endif 391 392 /********************* UART4 函数 ************************/ 393 #ifdef UART4 394 void TX4_write2buff(u8 dat) //写入发送缓冲,指针+1 395 { 396 1 TX4_Buffer[COM4.TX_write] = dat; //装发送缓冲 397 1 if(++COM4.TX_write >= COM_TX4_Lenth) COM4.TX_write = 0; 398 1 399 1 if(COM4.B_TX_busy == 0) //空闲 400 1 { 401 2 COM4.B_TX_busy = 1; //标志忙 402 2 SET_TI4(); //触发发送中断 403 2 } 404 1 } 405 406 void PrintString4(u8 *puts) 407 { 408 1 for (; *puts != 0; puts++) TX4_write2buff(*puts); //遇到停止符0结束 409 1 } 410 411 void UART4_int (void) interrupt UART4_VECTOR 412 { 413 1 if(RI4) 414 1 { 415 2 CLR_RI4(); 416 2 if(COM4.B_RX_OK == 0) 417 2 { 418 3 if(COM4.RX_Cnt >= COM_RX4_Lenth) COM4.RX_Cnt = 0; 419 3 RX4_Buffer[COM4.RX_Cnt++] = S4BUF; 420 3 COM4.RX_TimeOut = TimeOutSet4; 421 3 } 422 2 } 423 1 424 1 if(TI4) 425 1 { 426 2 CLR_TI4(); C51 COMPILER V9.59.0.0 UART 03/09/2021 14:02:25 PAGE 8 427 2 if(COM4.TX_read != COM4.TX_write) 428 2 { 429 3 S4BUF = TX4_Buffer[COM4.TX_read]; 430 3 if(++COM4.TX_read >= COM_TX4_Lenth) COM4.TX_read = 0; 431 3 } 432 2 else COM4.B_TX_busy = 0; 433 2 } 434 1 } 435 #endif 436 437 /*********************************************************/ 438 /* 439 void COMx_write2buff(COMx_Define *COMx, u8 dat) //写入发送缓冲,指针+1 440 { 441 if(COMx->id == 1) TX1_write2buff(dat); 442 if(COMx->id == 2) TX2_write2buff(dat); 443 if(COMx->id == 3) TX3_write2buff(dat); 444 if(COMx->id == 4) TX4_write2buff(dat); 445 } 446 447 void PrintString(COMx_Define *COMx, u8 *puts) 448 { 449 for (; *puts != 0; puts++) COMx_write2buff(COMx,*puts); //遇到停止符0结束 450 } 451 */ MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 615 ---- CONSTANT SIZE = ---- ---- XDATA SIZE = 256 ---- PDATA SIZE = ---- ---- DATA SIZE = 7 7 IDATA SIZE = ---- ---- BIT SIZE = ---- ---- END OF MODULE INFORMATION. C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)