最近有个老项目需要串口使用300波特率操作,但是将单片机配置好后,兴冲冲的去测试。
得到的数据有问题,例如,左边是调试打印的,右边的发送的。
但是600波特率收发都是正常的,我使用32.768K的内部时钟和32.768K的外部时钟效果是一样的。
600波特率
是需要算出波特率
然后配置寄存器吗?不太懂
串口配置代码
void Usart2_Init(void)
{
GPIO_InitType GPIO_InitStructure;
USART_InitType USART_InitStructure;
NVIC_InitType NVIC_InitStructure;
//ns_sleep_lock_acquire();
/* Enable GPIO and USART clock */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_USART2, ENABLE);
/* Configure GPIOx_PIN in output push-pull mode */
GPIO_InitStruct(&GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_3 | GPIO_PIN_12;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_HIGH;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_PIN_3); //RE=1
GPIO_ResetBits(GPIOB, GPIO_PIN_12); //DE=0
GPIO_InitStruct(&GPIO_InitStructure);
/* Configure USARTy Tx as alternate function push-pull */
GPIO_InitStructure.Pin = GPIO_PIN_4;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF3_USART2;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
/* Configure USARTx Rx as alternate function push-pull */
GPIO_InitStructure.Pin = GPIO_PIN_5;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF3_USART2;
GPIO_InitStructure.GPIO_Pull = GPIO_PULL_UP; //RX需要加上拉
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
/* Enable the USARTy Interrupt */
NVIC_DisableIRQ(USART2_IRQn);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// ModuleIrqRemoval(USART2_IRQn);
// ModuleIrqRegister(USART2_IRQn,RS485_IRQHandler);
/* USARTy and USARTz configuration ------------------------------------------------------*/
USART_InitStructure.BaudRate = 600;
USART_InitStructure.WordLength = USART_WL_8B;//USART_WL_9B
USART_InitStructure.StopBits = USART_STPB_1;
USART_InitStructure.Parity = USART_PE_NO; //USART_PE_ODD; USART_PE_EVEN; USART_PE_NO;
USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
USART_InitStructure.Mode = USART_MODE_RX | USART_MODE_TX;
/* Configure USARTy and USARTz */
USART_Init(USART2, &USART_InitStructure);
/* Enable USARTy Receive and Transmit interrupts */
USART_ConfigInt(USART2, USART_INT_RXDNE, ENABLE);
//USART_ConfigInt(USART2, USART_INT_TXDE, ENABLE);
/* Enable the USARTy and USARTz */
USART_Enable(USART2, ENABLE);
GPIO_ResetBits(GPIOB, GPIO_PIN_3); //RE=0
GPIO_ResetBits(GPIOB, GPIO_PIN_12); //DE=0
}
|