1、芯片问题
CLM32L003 串口波特率115200 设置无效
2、手册描述
DBAUD 寄存器需要设置为双波特率
3、代码修改
/**
* @brief UART Init Structure definition
*/
typedef struct
{
uint32_t BaudRate; /*!< This member configures the UART communication baud rate.
The baud rate is computed using the following formula:
- IntegerDivider = ((PCLKx) / (16 * (huart->Init.BaudRate)))
- FractionalDivider = ((IntegerDivider - ((uint32_t) IntegerDivider)) * 16) + 0.5 */
uint32_t BaudDouble; /*!< Specifies whether baudrate is doubled.
This parameter can be a value of @ref UART_BaudDouble */
uint32_t HalfDuplexMode; /*!< Specifies whether halfduplex mode is enabled.
This parameter can be a value of @ref UART_HalfDuplex */
uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
This parameter can be a value of @ref UART_Word_Length */
uint32_t Parity; /*!< Specifies the parity mode.
This parameter can be a value of @ref UART_Parity
@note When parity is enabled, the computed parity is set
at the MSB position of the transmitted data (9th bit only when
the word length is set to 9 data bits;*/
uint32_t Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
This parameter can be a value of @ref UART_Mode */
}UART_InitTypeDef;
BaudDouble :指定波特率是否加倍
#define UART_BAUDDOUBLE_ENABLE ((uint32_t)UART_SCON_DBAUD)
#define UART_BAUDDOUBLE_DISABLE (0x00000000U)
可以使用这两个宏函数
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock to HIRC 24MHz*/
SystemClock_Config();
/* Peripheral clock enable */
__HAL_RCC_UARTx_CLK_ENABLE();
sUartxHandle.Instance = UARTx;
sUartxHandle.Init.BaudRate = BAUDRATE;
sUartxHandle.Init.BaudDouble = UART_BAUDDOUBLE_ENABLE;
sUartxHandle.Init.WordLength = UART_WORDLENGTH_8B;
sUartxHandle.Init.Parity = UART_PARITY_NONE;
sUartxHandle.Init.Mode = UART_MODE_TX_RX;
HAL_UART_Init(&sUartxHandle);
HAL_UART_Transmit_IT(&sUartxHandle, ucSendData, sizeof(ucSendData));
HAL_UART_Receive_IT(&sUartxHandle, ucReceiveData, sizeof(ucReceiveData));
HAL_NVIC_EnableIRQ(UARTx_IRQn);
while (1)
{
/* UART receive LENGTH datas then transmit the received datas */
if(ucRxCompleteFlag == 1)
{
ucRxCompleteFlag = 0;
HAL_UART_Transmit_IT(&sUartxHandle, ucReceiveData, sizeof(ucReceiveData));
HAL_UART_Receive_IT(&sUartxHandle, ucReceiveData, sizeof(ucReceiveData));
}
}
}