C51 COMPILER V9.59.0.0 STC8A_UART 06/08/2021 16:39:18 PAGE 1 C51 COMPILER V9.59.0.0, COMPILATION OF MODULE STC8A_UART OBJECT MODULE PLACED IN .\list\STC8A_UART.obj COMPILER INVOKED BY: C:\Keil_v5\C51\BIN\C51.EXE STC8A_UART.c OMF2 OPTIMIZE(8,SPEED) BROWSE DEBUG PRINT(.\list\STC8A_UART -.lst) TABS(2) OBJECT(.\list\STC8A_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 "STC8A_UART.h" 14 15 //======================================================================== 16 // 本地变量声明 17 //======================================================================== 18 19 #ifdef UART1 20 COMx_Define COM1; 21 u8 xdata TX1_Buffer[COM_TX1_Lenth]; //发送缓冲 22 u8 xdata RX1_Buffer[COM_RX1_Lenth]; //接收缓冲 23 #endif 24 #ifdef UART2 COMx_Define COM2; u8 xdata TX2_Buffer[COM_TX2_Lenth]; //发送缓冲 u8 xdata RX2_Buffer[COM_RX2_Lenth]; //接收缓冲 #endif 29 #ifdef UART3 COMx_Define COM3; u8 xdata TX3_Buffer[COM_TX3_Lenth]; //发送缓冲 u8 xdata RX3_Buffer[COM_RX3_Lenth]; //接收缓冲 #endif 34 #ifdef UART4 COMx_Define COM4; u8 xdata TX4_Buffer[COM_TX4_Lenth]; //发送缓冲 u8 xdata RX4_Buffer[COM_RX4_Lenth]; //接收缓冲 #endif 39 40 //======================================================================== 41 // 函数: UART_Configuration 42 // 描述: UART初始化程序. 43 // 参数: UARTx: UART组号, COMx结构参数,请参考UART.h里的定义. 44 // 返回: none. 45 // 版本: V1.0, 2012-10-22 46 //======================================================================== 47 u8 UART_Configuration(u8 UARTx, COMx_InitDefine *COMx) 48 { 49 1 #if defined( UART1 ) || defined( UART2 ) || defined( UART3 ) || defined( UART4 ) 50 1 u8 i; 51 1 u32 j; 52 1 #else UARTx = NULL; COMx = NULL; C51 COMPILER V9.59.0.0 STC8A_UART 06/08/2021 16:39:18 PAGE 2 #endif 56 1 57 1 #ifdef UART1 58 1 if(UARTx == UART1) 59 1 { 60 2 COM1.id = 1; 61 2 COM1.TX_read = 0; 62 2 COM1.TX_write = 0; 63 2 COM1.B_TX_busy = 0; 64 2 COM1.RX_Cnt = 0; 65 2 COM1.RX_TimeOut = 0; 66 2 COM1.B_RX_OK = 0; 67 2 68 2 for(i=0; iUART_Mode; //模式设置 72 2 if((COMx->UART_Mode == UART_9bit_BRTx) || (COMx->UART_Mode == UART_8bit_BRTx)) //可变波特率 73 2 { 74 3 j = (MAIN_Fosc / 4) / COMx->UART_BaudRate; //按1T计算 75 3 if(j >= 65536UL) return FAIL; //错误 76 3 j = 65536UL - j; 77 3 if(COMx->UART_BRT_Use == BRT_Timer2) 78 3 { 79 4 AUXR &= ~(1<<4); //Timer stop 80 4 AUXR |= 0x01; //S1 BRT Use Timer2; 81 4 AUXR &= ~(1<<3); //Timer2 set As Timer 82 4 AUXR |= (1<<2); //Timer2 set as 1T mode 83 4 TH2 = (u8)(j>>8); 84 4 TL2 = (u8)j; 85 4 IE2 &= ~(1<<2); //禁止中断 86 4 AUXR |= (1<<4); //Timer run enable 87 4 } 88 3 else 89 3 { 90 4 TR1 = 0; 91 4 AUXR &= ~0x01; //S1 BRT Use Timer1; 92 4 TMOD &= ~(1<<6); //Timer1 set As Timer 93 4 TMOD &= ~0x30; //Timer1_16bitAutoReload; 94 4 AUXR |= (1<<6); //Timer1 set as 1T mode 95 4 TH1 = (u8)(j>>8); 96 4 TL1 = (u8)j; 97 4 ET1 = 0; //禁止中断 98 4 TMOD &= ~0x40; //定时 99 4 INT_CLKO &= ~0x02; //不输出时钟 100 4 TR1 = 1; 101 4 } 102 3 } 103 2 else if(COMx->UART_Mode == UART_ShiftRight) 104 2 { 105 3 if(COMx->BaudRateDouble == ENABLE) AUXR |= (1<<5); //固定波特率SysClk/2 106 3 else AUXR &= ~(1<<5); //固定波特率SysClk/12 107 3 } 108 2 else if(COMx->UART_Mode == UART_9bit) //固定波特率SysClk*2^SMOD/64 109 2 { 110 3 if(COMx->BaudRateDouble == ENABLE) PCON |= (1<<7); //固定波特率SysClk/32 111 3 else PCON &= ~(1<<7); //固定波特率SysClk/64 112 3 } 113 2 UART1_RxEnable(COMx->UART_RxEnable); //UART接收使能 114 2 115 2 return SUCCESS; 116 2 } C51 COMPILER V9.59.0.0 STC8A_UART 06/08/2021 16:39:18 PAGE 3 117 1 #endif 118 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; 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_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 FAIL; //错误 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 FAIL; //模式错误 UART2_RxEnable(COMx->UART_RxEnable); //UART接收使能 return SUCCESS; } #endif 153 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_Mode == UART_9bit_BRTx) S3_9bit(); //9bit else S3_8bit(); //8bit j = (MAIN_Fosc / 4) / COMx->UART_BaudRate; //按1T计算 if(j >= 65536UL) return FAIL; //错误 j = 65536UL - j; if(COMx->UART_BRT_Use == BRT_Timer2) { 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 C51 COMPILER V9.59.0.0 STC8A_UART 06/08/2021 16:39:18 PAGE 4 TH2 = (u8)(j>>8); TL2 = (u8)j; IE2 &= ~(1<<2); //禁止中断 AUXR |= (1<<4); //Timer run enable } else { 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 return FAIL; //模式错误 UART3_RxEnable(COMx->UART_RxEnable); //UART接收使能 return SUCCESS; } #endif 199 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; iUART_Mode == UART_9bit_BRTx) || (COMx->UART_Mode == UART_8bit_BRTx)) //可变波特率 { 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 FAIL; //错误 j = 65536UL - j; 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); TL2 = (u8)j; IE2 &= ~(1<<2); //禁止中断 AUXR |= (1<<4); //Timer run enable } else { 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 return FAIL; //模式错误 UART4_RxEnable(COMx->UART_RxEnable); //UART接收使能 C51 COMPILER V9.59.0.0 STC8A_UART 06/08/2021 16:39:18 PAGE 5 return SUCCESS; } #endif 245 1 return FAIL; //错误 246 1 } 247 248 /*********************************************************/ 249 250 /********************* UART1 函数 ************************/ 251 #ifdef UART1 252 void TX1_write2buff(u8 dat) //写入发送缓冲,指针+1 253 { 254 1 SBUF = dat; 255 1 COM1.B_TX_busy = 1; //标志忙 256 1 while(COM1.B_TX_busy); 257 1 } 258 259 void PrintString1(u8 *puts) 260 { 261 1 for (; *puts != 0; puts++) TX1_write2buff(*puts); //遇到停止符0结束 262 1 } 263 264 #endif 265 266 /********************* UART2 函数 ************************/ 267 #ifdef UART2 void TX2_write2buff(u8 dat) //写入发送缓冲,指针+1 { S2BUF = dat; COM2.B_TX_busy = 1; //标志忙 while(COM2.B_TX_busy); } void PrintString2(u8 *puts) { for (; *puts != 0; puts++) TX2_write2buff(*puts); //遇到停止符0结束 } #endif 281 282 /********************* UART3 函数 ************************/ 283 #ifdef UART3 void TX3_write2buff(u8 dat) //写入发送缓冲,指针+1 { S3BUF = dat; COM3.B_TX_busy = 1; //标志忙 while(COM3.B_TX_busy); } void PrintString3(u8 *puts) { for (; *puts != 0; puts++) TX3_write2buff(*puts); //遇到停止符0结束 } #endif 297 298 /********************* UART4 函数 ************************/ 299 #ifdef UART4 void TX4_write2buff(u8 dat) //写入发送缓冲,指针+1 { S4BUF = dat; C51 COMPILER V9.59.0.0 STC8A_UART 06/08/2021 16:39:18 PAGE 6 COM4.B_TX_busy = 1; //标志忙 while(COM4.B_TX_busy); } void PrintString4(u8 *puts) { for (; *puts != 0; puts++) TX4_write2buff(*puts); //遇到停止符0结束 } #endif 313 314 /*********************************************************/ 315 /* 316 void COMx_write2buff(COMx_Define *COMx, u8 dat) //写入发送缓冲,指针+1 317 { 318 if(COMx->id == 1) TX1_write2buff(dat); 319 if(COMx->id == 2) TX2_write2buff(dat); 320 if(COMx->id == 3) TX3_write2buff(dat); 321 if(COMx->id == 4) TX4_write2buff(dat); 322 } 323 324 void PrintString(COMx_Define *COMx, u8 *puts) 325 { 326 for (; *puts != 0; puts++) COMx_write2buff(COMx,*puts); //遇到停止符0结束 327 } 328 */ 329 330 /********************* Printf 函数 ************************/ 331 #if(PRINTF_SEL == 1) 332 333 char putchar(char c) 334 { 335 1 TX1_write2buff(c); 336 1 return c; 337 1 } 338 339 #elif(PRINTF_SEL == 2) char putchar(char c) { TX2_write2buff(c); return c; } #elif(PRINTF_SEL == 3) char putchar(char c) { TX3_write2buff(c); return c; } #elif(PRINTF_SEL == 4) char putchar(char c) { TX4_write2buff(c); return c; } #endif C51 COMPILER V9.59.0.0 STC8A_UART 06/08/2021 16:39:18 PAGE 7 MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 398 ---- CONSTANT SIZE = ---- ---- XDATA SIZE = 256 ---- PDATA SIZE = ---- ---- DATA SIZE = 7 7 IDATA SIZE = ---- ---- BIT SIZE = ---- ---- EDATA SIZE = ---- ---- HDATA SIZE = ---- ---- XDATA CONST SIZE = ---- ---- FAR CONST SIZE = ---- ---- END OF MODULE INFORMATION. C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)