使用stm32扩展库开发L072系列时发现,初始化LPUART1后,所得到得波特率与电脑实际接受不符,且只有波特率在9600以上,串口才不会进入溢出中断。但接受数据仍然不对,初始化如下:
void Usart_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
HAL_NVIC_DisableIRQ(RNG_LPUART1_IRQn);
HAL_NVIC_SetPriority(RNG_LPUART1_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(RNG_LPUART1_IRQn);
GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_11;//RS485-LPUART1
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Alternate = GPIO_AF4_LPUART1;
GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
Usart1_Init(115200);
}
void Usart1_Init(uint32_t BaudRate)
{
UART_HandleTypeDef USART_InitStructure;
__HAL_RCC_LPUART1_RELEASE_RESET();
__HAL_RCC_LPUART1_CLK_ENABLE();
LPUART1->CR1 &=0;
LPUART1->CR1 |= 3<<2;
LPUART1->BRR = BaudRate;
LPUART1->CR1 |= USART_CR1_UE;//使能串口
LPUART1->CR1 &= ~(uint32_t)BIT7;//禁止发送中断
LPUART1->CR1 |= BIT5;//使能接受中断
LPUART1->ICR &= ~(uint32_t)UART_CLEAR_OREF;
}
测试发现,发送一个字节数据时,电脑接受到三个字节数据,求大神指点! |