[N32WBxxx] N32WB031的串口使用300波特率误差有一点点大,怎么改善比较好呢?大佬们

[复制链接]
 楼主| huangwentao 发表于 2024-10-18 19:01 | 显示全部楼层 |阅读模式
最近有个老项目需要串口使用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

}


yangxiaor520 发表于 2024-10-23 21:00 来自手机 | 显示全部楼层
300波特率也太低了吧,估计时钟分频后没有合适的分频系数,导致误差过大。

评论

是的,有误码率,但是参考手册有说明使用32.768K的外部低速时钟下的低功耗串口可以到达300波特率,但试了一下还是一样的效果。  发表于 2024-10-28 16:58
chenqianqian 发表于 2024-10-24 07:42 来自手机 | 显示全部楼层
应该是300波特率对应下来的分频系数误差比较大
 楼主| huangwentao 发表于 2024-10-28 16:59 | 显示全部楼层
chenqianqian 发表于 2024-10-24 07:42
应该是300波特率对应下来的分频系数误差比较大

有误码率,但是参考手册有说明使用32.768K的外部低速时钟下的低功耗串口可以到达300波特率,但试了一下还是一样的效果,现在换芯片搞了
stormwind123 发表于 2024-11-7 12:56 | 显示全部楼层
检查并优化串口配置
probedog 发表于 2024-11-7 14:00 | 显示全部楼层
检查串口线路,考虑信号放大或转换
classroom 发表于 2024-11-7 15:00 | 显示全部楼层
300波特率较低,可能导致时钟分频后没有合适的分频系数,从而产生误差。
laocuo1142 发表于 2024-11-7 17:00 | 显示全部楼层
可以考虑使用其他更高速的通信接口,如USB、以太网等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

5

主题

14

帖子

0

粉丝
快速回复 返回顶部 返回列表