8051-ELL库  beta 1.1.6
8051-ELL library API Document Center
STC8x_UART.h
浏览该文件的文档.
1 /*-----------------------------------------------------------------------
2 | FILE DESCRIPTION |
3 -----------------------------------------------------------------------*/
4 /*----------------------------------------------------------------------
5  - File name : STC8x_UART.h
6  - Author : zeweni
7  - Update date : 2020.04.27
8  - Copyright(C) : 2020-2021 zeweni. All rights reserved.
9 -----------------------------------------------------------------------*/
10 /*------------------------------------------------------------------------
11 | COPYRIGHT NOTICE |
12 ------------------------------------------------------------------------*/
13 /*
14  * Copyright (C) 2021, zeweni (17870070675@163.com)
15 
16  * This file is part of 8051 ELL low-layer libraries.
17 
18  * 8051 ELL low-layer libraries is free software: you can redistribute
19  * it and/or modify it under the terms of the Apache-2.0 License.
20 
21  * 8051 ELL low-layer libraries is distributed in the hope that it will
22  * be useful,but WITHOUT ANY WARRANTY; without even the implied warranty
23  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * Apache-2.0 License for more details.
25 
26  * You should have received a copy of the Apache-2.0 License.8051 ELL
27  * low-layer libraries. If not, see <http://www.apache.org/licenses/>.
28 **/
29 /*-----------------------------------------------------------------------
30 | INCLUDES |
31 -----------------------------------------------------------------------*/
32 #ifndef __STC8x_UART_H_
33 #define __STC8x_UART_H_
34 
35 /*--------------------------------------------------------
36 | @Description: ELL library core |
37 --------------------------------------------------------*/
38 #include "ELL_CORE.h"
39 
40 /*--------------------------------------------------------
41 | @Description: STC8x MCU Register |
42 --------------------------------------------------------*/
43 
44 #if (PER_LIB_MCU_MUODEL == STC8Ax)
45  #include "STC8Ax_REG.h"
46 #elif (PER_LIB_MCU_MUODEL == STC8Cx)
47  #include "STC8Cx_REG.h"
48 #elif (PER_LIB_MCU_MUODEL == STC8Fx)
49  #include "STC8Fx_REG.h"
50 #elif (PER_LIB_MCU_MUODEL == STC8Gx)
51  #include "STC8Gx_REG.h"
52 #elif (PER_LIB_MCU_MUODEL == STC8Hx)
53  #include "STC8Hx_REG.h"
54 #endif
55 
56 /*-----------------------------------------------------------------------
57 | DATA |
58 -----------------------------------------------------------------------*/
59 
60 #ifndef PER_LIB_MCU_MUODEL
63  #define PER_LIB_MCU_MUODEL STC8Ax
64 #endif
65 
66 
67 #ifndef PER_LIB_UART_CTRL
70  #define PER_LIB_UART_CTRL 1
71 #endif
72 
73 
74 #ifndef PER_LIB_UART_INIT_CTRL
77  #define PER_LIB_UART_INIT_CTRL 1
78 #endif
79 
80 
81 #ifndef PER_LIB_UART_NVIC_CTRL
84  #define PER_LIB_UART_NVIC_CTRL 1
85 #endif
86 
87 
88 #ifndef PER_LIB_UART_WORK_CTRL
91  #define PER_LIB_UART_WORK_CTRL 1
92 #endif
93 
94 
99 typedef enum
100 {
101  UART_SyncShift = 0x00,
102  UART_8bit_BRTx = 0x01,
104  UART_9bit_BRTx = 0x03
106 
107 
112 typedef enum
113 {
114  UART_BRT_TIM1 = 0x00,
115  UART_BRT_TIM2 = 0x01,
116  UART_BRT_TIM3 = 0x02,
117  UART_BRT_TIM4 = 0x03
119 
120 
125 typedef enum
126 {
127  UART_BRT_1T = 0x00,
128  UART_BRT_12T = 0x01
130 
131 
139 typedef struct
140 {
143  #if (PER_LIB_MCU_MUODEL == STC8Cx ||PER_LIB_MCU_MUODEL == STC8Gx || PER_LIB_MCU_MUODEL == STC8Hx)
145  #endif
152 } UART_InitType;
153 
154 /*-----------------------------------------------------------------------
155 | API FUNCTION |
156 -----------------------------------------------------------------------*/
157 #if ( PER_LIB_UART_CTRL == 1)
158 
159  #if (PER_LIB_UART_INIT_CTRL == 1)
160 
171 
172 
183 
184 
195 
196 
207 
208  #endif
209 
210  #if (PER_LIB_UART_NVIC_CTRL == 1)
211 
216  #define UART1_GET_RX_FLAG() (SCON & 0x01)
217 
218 
223  #define UART2_GET_RX_FLAG() (S2CON & 0x01)
224 
225 
230  #define UART3_GET_RX_FLAG() (S3CON & 0x01)
231 
232 
237  #define UART4_GET_RX_FLAG() (S4CON & 0x01)
238 
239 
244  #define UART1_CLEAR_RX_FLAG() do{SCON &= 0xFE;}while(0)
245 
246 
251  #define UART2_CLEAR_RX_FLAG() do{S2CON &= 0xFE;}while(0)
252 
253 
258  #define UART3_CLEAR_RX_FLAG() do{S3CON &= 0xFE;}while(0)
259 
260 
265  #define UART4_CLEAR_RX_FLAG() do{S4CON &= 0xFE;}while(0)
266 
267 
272  #define UART1_GET_TX_FLAG() (SCON & 0x02)
273 
274 
279  #define UART2_GET_TX_FLAG() (S2CON & 0x02)
280 
281 
286  #define UART3_GET_TX_FLAG() (S3CON & 0x02)
287 
288 
293  #define UART4_GET_TX_FLAG() (S4CON & 0x02)
294 
295 
300  #define UART1_CLEAR_TX_FLAG() do{SCON &= 0xFD;}while(0)
301 
302 
307  #define UART2_CLEAR_TX_FLAG() do{S2CON &= 0xFD;}while(0)
308 
309 
314  #define UART3_CLEAR_TX_FLAG() do{S3CON &= 0xFD;}while(0)
315 
316 
321  #define UART4_CLEAR_TX_FLAG() do{S4CON &= 0xFD;}while(0)
322 
323 
333 
334 
344 
345 
346  #if (PER_LIB_MCU_MUODEL == STC8Ax || PER_LIB_MCU_MUODEL == STC8Fx)
347 
356 
365 
366  #elif (PER_LIB_MCU_MUODEL == STC8Cx || PER_LIB_MCU_MUODEL == STC8Gx || PER_LIB_MCU_MUODEL == STC8Hx)
367 
377 
378 
388 
389  #endif
390 
391 
397  #define NVIC_UART1_CTRL(run) do{ ES = run; }while(0)
398 
399 
405  #define NVIC_UART2_CTRL(run) do{ IE2 = (IE2 & 0xFE) | (run); }while(0)
406 
407 
413  #define NVIC_UART3_CTRL(run) do{ IE2 = (IE2 & 0xF7) | (run << 3); }while(0)
414 
415 
421  #define NVIC_UART4_CTRL(run) do{ IE2 = (IE2 & 0xEF) | (run << 4); }while(0)
422 
423 
429  #define NVIC_UART1_PRI(pri) \
430  do{ \
431  IPH = (IPH & 0xEF) | ((pri & 0x02) << 3); \
432  IP = (IP & 0xEF) | ((pri & 0x01) << 4); \
433  }while(0)
434 
435 
441  #define NVIC_UART2_PRI(pri) \
442  do{ \
443  IP2H = (IP2H & 0xFE) | ((pri & 0x02) >> 1); \
444  IP2 = (IP2 & 0xFE) | (pri & 0x01); \
445  }while(0)
446 
447  #if (PER_LIB_MCU_MUODEL == STC8Cx || PER_LIB_MCU_MUODEL == STC8Gx || PER_LIB_MCU_MUODEL == STC8Hx)
448 
454  #define NVIC_UART3_PRI(pri) \
455  do{ \
456  IP3H = (IP3H & 0xFE) | ((pri & 0x02) >> 1); \
457  IP3 = (IP3 & 0xFE) | (pri & 0x01); \
458  }while(0)
459 
460 
466  #define NVIC_UART4_PRI(pri) \
467  do{ \
468  IP3H = (IP3H & 0xFD) | (pri & 0x02); \
469  IP3 = (IP3 & 0xFD) | (pri & 0x01) << 1; \
470  }while(0)
471 
472  #endif
473 
474  #endif
475 
476  #if (PER_LIB_UART_WORK_CTRL == 1)
477 
486 
487 
496 
497 
506 
507 
516 
517 
523  extern uint8_t G_Uart_Busy_Flg;
524 
525 
533  #define UART1_GET_BUSY_FLAG() (G_Uart_Busy_Flg & 0x01)
534 
535 
543  #define UART2_GET_BUSY_FLAG() (G_Uart_Busy_Flg & 0x02)
544 
545 
553  #define UART3_GET_BUSY_FLAG() (G_Uart_Busy_Flg & 0x04)
554 
555 
563  #define UART4_GET_BUSY_FLAG() (G_Uart_Busy_Flg & 0x08)
564 
571  #define UART1_SET_BUSY_FLAG() do{G_Uart_Busy_Flg |= 0x01;}while(0)
572 
573 
580  #define UART2_SET_BUSY_FLAG() do{G_Uart_Busy_Flg |= 0x02;}while(0)
581 
582 
589  #define UART3_SET_BUSY_FLAG() do{G_Uart_Busy_Flg |= 0x04;}while(0)
590 
591 
598  #define UART4_SET_BUSY_FLAG() do{G_Uart_Busy_Flg |= 0x08;}while(0)
599 
600 
607  #define UART1_CLEAR_BUSY_FLAG() do{G_Uart_Busy_Flg &= 0xFE;}while(0)
608 
609 
616  #define UART2_CLEAR_BUSY_FLAG() do{G_Uart_Busy_Flg &= 0xFD;}while(0)
617 
618 
625  #define UART3_CLEAR_BUSY_FLAG() do{G_Uart_Busy_Flg &= 0xFB;}while(0)
626 
627 
634  #define UART4_CLEAR_BUSY_FLAG() do{G_Uart_Busy_Flg &= 0xF7;}while(0)
635 
636 
642  #define UART1_RECEIVE_BYTE(byte) do{byte = SBUF;}while(0)
643 
644 
650  #define UART2_RECEIVE_BYTE(byte) do{byte = S2BUF;}while(0)
651 
652 
658  #define UART3_RECEIVE_BYTE(byte) do{byte = S3BUF;}while(0)
659 
660 
666  #define UART4_RECEIVE_BYTE(byte) do{byte = S4BUF;}while(0)
667 
678 
689 
690 
701 
702 
713 
714 
724  void UART1_Isr_Send_String(const uint8_t *str);
725 
726 
736  void UART2_Isr_Send_String(const uint8_t *str);
737 
738 
748  void UART3_Isr_Send_String(const uint8_t *str);
749 
750 
760  void UART4_Isr_Send_String(const uint8_t *str);
761 
772  void UART1_Isr_Send_Array(const uint8_t *str,uint16_t len);
773 
774 
785  void UART2_Isr_Send_Array(const uint8_t *str,uint16_t len);
786 
787 
798  void UART3_Isr_Send_Array(const uint8_t *str,uint16_t len);
799 
800 
811  void UART4_Isr_Send_Array(const uint8_t *str,uint16_t len);
812 
813  #endif
814 
815 #endif
816 
817 /*-----------------------------------------------------------------------
818 | END OF FLIE. (C) COPYRIGHT zeweni |
819 -----------------------------------------------------------------------*/
820 #endif
NVICPri_Type
Definition: ELL_CORE.h:99
BOOL
Definition: ELL_CORE.h:50
FSCSTATE
Definition: ELL_CORE.h:65
GPIOSWPort_Type
Definition: ELL_CORE.h:81
unsigned long uint32_t
Definition: ELL_TYPE.h:74
unsigned int uint16_t
Definition: ELL_TYPE.h:73
unsigned char uint8_t
Definition: ELL_TYPE.h:72
UART_BRTMode_Type
串口波特率发生器指令周期枚举体。
Definition: STC8x_UART.h:126
@ UART_BRT_12T
Definition: STC8x_UART.h:128
@ UART_BRT_1T
Definition: STC8x_UART.h:127
void UART4_Isr_Send_String(const uint8_t *str)
串口4发送一个字符串函数, 需要开启中断,并在中断服务函数中清除发送标志位。
void UART2_Isr_Send_Array(const uint8_t *str, uint16_t len)
串口2发送一个数组函数, 需要开启中断,并在中断服务函数中清除发送标志位。
FSCSTATE NVIC_UART1_Init(NVICPri_Type pri, BOOL run)
串口1中断初始化函数。
UART_BRTGen_Type
串口波特率发生器枚举体。
Definition: STC8x_UART.h:113
@ UART_BRT_TIM4
Definition: STC8x_UART.h:117
@ UART_BRT_TIM2
Definition: STC8x_UART.h:115
@ UART_BRT_TIM1
Definition: STC8x_UART.h:114
@ UART_BRT_TIM3
Definition: STC8x_UART.h:116
UARTMode_Type
串口工作模式枚举体。
Definition: STC8x_UART.h:100
@ UART_8bit_BRTx
Definition: STC8x_UART.h:102
@ UART_9bit_Fixed_BRTx
Definition: STC8x_UART.h:103
@ UART_SyncShift
Definition: STC8x_UART.h:101
@ UART_9bit_BRTx
Definition: STC8x_UART.h:104
void UART3_Isr_Send_Byte(uint8_t dat)
串口3发送一个字节函数, 需要开启中断,并在中断服务函数中清除发送标志位。
void UART1_Isr_Send_String(const uint8_t *str)
串口1发送一个字符串函数, 需要开启中断,并在中断服务函数中清除发送标志位。
void UART3_Isr_Send_String(const uint8_t *str)
串口3发送一个字符串函数, 需要开启中断,并在中断服务函数中清除发送标志位。
void UART1_Isr_Send_Byte(uint8_t dat)
串口1发送一个字节函数, 需要开启中断,并在中断服务函数中清除发送标志位。
FSCSTATE UART1_Init(const UART_InitType *uartx)
串口1初始化函数。
void UART3_Isr_Send_Array(const uint8_t *str, uint16_t len)
串口3发送一个数组函数, 需要开启中断,并在中断服务函数中清除发送标志位。
FSCSTATE UART4_Init(const UART_InitType *uartx)
串口4初始化函数。
void UART1_Isr_Send_Array(const uint8_t *str, uint16_t len)
串口1发送一个数组函数, 需要开启中断,并在中断服务函数中清除发送标志位。
void UART2_Isr_Send_String(const uint8_t *str)
串口2发送一个字符串函数, 需要开启中断,并在中断服务函数中清除发送标志位。
FSCSTATE GPIO_UART4_SWPort(GPIOSWPort_Type port)
串口4切换复用IO函数。
uint8_t G_Uart_Busy_Flg
串口忙标志位,这是ELL库自定义从防止多串口冲突的变量。
FSCSTATE NVIC_UART4_Init(BOOL run)
串口4中断初始化函数。
FSCSTATE NVIC_UART2_Init(NVICPri_Type pri, BOOL run)
串口2中断初始化函数。
void UART4_Isr_Send_Array(const uint8_t *str, uint16_t len)
串口4发送一个数组函数, 需要开启中断,并在中断服务函数中清除发送标志位。
void UART4_Isr_Send_Byte(uint8_t dat)
串口4发送一个字节函数, 需要开启中断,并在中断服务函数中清除发送标志位。
FSCSTATE UART2_Init(const UART_InitType *uartx)
串口2初始化函数。
void UART2_Isr_Send_Byte(uint8_t dat)
串口2发送一个字节函数, 需要开启中断,并在中断服务函数中清除发送标志位。
FSCSTATE GPIO_UART2_SWPort(GPIOSWPort_Type port)
串口2切换复用IO函数。
FSCSTATE NVIC_UART3_Init(BOOL run)
串口3中断初始化函数。
FSCSTATE GPIO_UART3_SWPort(GPIOSWPort_Type port)
串口3切换复用IO函数。
FSCSTATE GPIO_UART1_SWPort(GPIOSWPort_Type port)
串口1切换复用IO函数。
FSCSTATE UART3_Init(const UART_InitType *uartx)
串口3初始化函数。
串口初始化结构体句柄,初始化时请定义该句柄,并用其地址来传参。
Definition: STC8x_UART.h:140
BOOL RxEnable
Definition: STC8x_UART.h:151
BOOL BRTDouble
Definition: STC8x_UART.h:147
UART_BRTMode_Type BRTMode
Definition: STC8x_UART.h:146
BOOL Relay
Definition: STC8x_UART.h:150
uint32_t BaudRate
Definition: STC8x_UART.h:148
UARTMode_Type Mode
Definition: STC8x_UART.h:141
UART_BRTGen_Type BRTGen
Definition: STC8x_UART.h:142
uint8_t BRTGenClkDiv
Definition: STC8x_UART.h:144
BOOL MulitComm
Definition: STC8x_UART.h:149