C51 COMPILER V9.59.0.0 STC8A_UART 06/08/2021 17:52:48 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 COMx_Define COM1; u8 xdata TX1_Buffer[COM_TX1_Lenth]; //发送缓冲 u8 xdata RX1_Buffer[COM_RX1_Lenth]; //接收缓冲 #endif 24 #ifdef UART2 25 COMx_Define COM2; 26 u8 xdata TX2_Buffer[COM_TX2_Lenth]; //发送缓冲 27 u8 xdata RX2_Buffer[COM_RX2_Lenth]; //接收缓冲 28 #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 17:52:48 PAGE 2 #endif 56 1 57 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_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 FAIL; //错误 j = 65536UL - j; 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 { 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_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 } UART1_RxEnable(COMx->UART_RxEnable); //UART接收使能 return SUCCESS; } C51 COMPILER V9.59.0.0 STC8A_UART 06/08/2021 17:52:48 PAGE 3 #endif 118 1 #ifdef UART2 119 1 if(UARTx == UART2) 120 1 { 121 2 COM2.id = 2; 122 2 COM2.TX_read = 0; 123 2 COM2.TX_write = 0; 124 2 COM2.B_TX_busy = 0; 125 2 COM2.RX_Cnt = 0; 126 2 COM2.RX_TimeOut = 0; 127 2 COM2.B_RX_OK = 0; 128 2 129 2 for(i=0; iUART_Mode == UART_9bit_BRTx) ||(COMx->UART_Mode == UART_8bit_BRTx)) //可变波特率 133 2 { 134 3 if(COMx->UART_Mode == UART_9bit_BRTx) S2CON |= (1<<7); //9bit 135 3 else S2CON &= ~(1<<7); //8bit 136 3 j = (MAIN_Fosc / 4) / COMx->UART_BaudRate; //按1T计算 137 3 if(j >= 65536UL) return FAIL; //错误 138 3 j = 65536UL - j; 139 3 AUXR &= ~(1<<4); //Timer stop 140 3 AUXR &= ~(1<<3); //Timer2 set As Timer 141 3 AUXR |= (1<<2); //Timer2 set as 1T mode 142 3 TH2 = (u8)(j>>8); 143 3 TL2 = (u8)j; 144 3 IE2 &= ~(1<<2); //禁止中断 145 3 AUXR |= (1<<4); //Timer run enable 146 3 } 147 2 else return FAIL; //模式错误 148 2 UART2_RxEnable(COMx->UART_RxEnable); //UART接收使能 149 2 150 2 return SUCCESS; 151 2 } 152 1 #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 17:52:48 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 17:52:48 PAGE 5 return SUCCESS; } #endif 245 1 return FAIL; //错误 246 1 } 247 248 /*********************************************************/ 249 250 /********************* UART1 函数 ************************/ 251 #ifdef UART1 void TX1_write2buff(u8 dat) //写入发送缓冲,指针+1 { SBUF = dat; COM1.B_TX_busy = 1; //标志忙 while(COM1.B_TX_busy); } void PrintString1(u8 *puts) { for (; *puts != 0; puts++) TX1_write2buff(*puts); //遇到停止符0结束 } #endif 265 266 /********************* UART2 函数 ************************/ 267 #ifdef UART2 268 void TX2_write2buff(u8 dat) //写入发送缓冲,指针+1 269 { 270 1 S2BUF = dat; 271 1 COM2.B_TX_busy = 1; //标志忙 272 1 while(COM2.B_TX_busy); 273 1 } 274 275 void PrintString2(u8 *puts) 276 { 277 1 for (; *puts != 0; puts++) TX2_write2buff(*puts); //遇到停止符0结束 278 1 } 279 280 #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 17:52:48 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) char putchar(char c) { TX1_write2buff(c); return c; } #elif(PRINTF_SEL == 2) 340 341 char putchar(char c) 342 { 343 1 TX2_write2buff(c); 344 1 return c; 345 1 } 346 347 #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 17:52:48 PAGE 7 MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 288 ---- 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)