hc32l13x uart串口模块配置示例代码
时间: 2023-07-25 18:15:01 浏览: 27
以下是HC32L13X芯片的UART串口模块配置示例代码:
```c
#include "hc32l13x.h"
void UartInit(void)
{
stc_uart_config_t stcUartConfig;
stc_uart_multimode_t stcMulti;
stc_uart_baud_t stcBaud;
//使能UART外设时钟
Sysctrl_SetPeripheralGate(SysctrlPeripheralUart0, TRUE);
//配置PD5为UART0_TX,PD6为UART0_RX
Gpio_SetFunc_UART0TX_P05();
Gpio_SetFunc_UART0RX_P06();
//UART配置结构体清零
Mem_Zero(&stcUartConfig, sizeof(stcUartConfig));
Mem_Zero(&stcMulti, sizeof(stcMulti));
Mem_Zero(&stcBaud, sizeof(stcBaud));
//UART基本配置
stcUartConfig.enRunMode = UartMode1;
stcUartConfig.enStopBit = Uart1bit;
stcUartConfig.enMmdorCk = UartMmd;
stcUartConfig.u32Baudrate = 9600;
//UART多主机模式配置
stcMulti.enMulti_mode = UartNormal;
stcMulti.u8Slave_addr = 0x00u;
stcMulti.enBitDirection = UartDataLsbFirst;
stcMulti.enBitLength = UartEightBits;
stcMulti.enDetectMode = UartSingleEnd;
stcMulti.enPeforDetect = UartMmd;
stcMulti.enRtOrTxDetect = UartMmd;
stcMulti.enAddressMatch = UartAddMatch0;
stcMulti.enBitType = UartData;
//UART波特率配置
stcBaud.u32Pclk = Sysctrl_GetPClkFreq();
stcBaud.enRunMode = UartMode1;
stcBaud.u32Baud = stcUartConfig.u32Baudrate;
//配置UART外设
Uart_Init(UARTCH0, &stcUartConfig, &stcMulti);
Uart_SetBaudrate(UARTCH0, &stcBaud);
Uart_EnableFunc(UARTCH0, UartRx);
}
```
该代码中,首先使能了UART外设时钟,然后配置了PD5为UART0_TX,PD6为UART0_RX。接着定义了三个结构体变量,分别用于UART的基本配置、多主机模式配置和波特率配置。最后通过调用`Uart_Init()`、`Uart_SetBaudrate()`和`Uart_EnableFunc()`函数来完成UART外设的配置。
需要注意的是,该代码仅作为示例,具体的配置根据实际需求进行调整。同时,配置UART时需要确保时钟配置正确,并且使能对应的GPIO引脚功能,否则可能会导致UART无法正常工作。