STM32F407 Uart Rx失误错过了一些字符

时间:2019-08-27 02:49:43

标签: uart stm32f4

UART在与所有其他设备通信时有时会丢失一些字符。

我尝试过与arduino,串行监视器(在PC上,带有串行USB转换器),HM-10蓝牙模块进行通信。

在每种情况下,我的stm32f407接收数据时都会丢失一些字符。 (我使用LCD进行了检查。我不使用uart发送任何数据。) 例如,当我从PC发送“这是测试字符串”时, 传入的字符串就像“ Ths is tt sring”。 没有坏字符。它只会丢失一些字符。

在处理程序中,我评论了USART_Cmd(...),移动了USART_ClearITPendingBit(USART3,USART_IT_RXNE);到底部,并将优先级更改为0、1、2、3。 但是,什么都没有改变。

我的时钟为168MHz,而uart使用9600或115200波特率。 两者都有相同的问题。

下面是我的代码。

  • 由于某些原因,我无法使用HAL驱动程序。
void Uart_Init(long uart_baudrate) {
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    /* GPIO Periph clock enable */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

    // USART1 Port Configuration (TX - 9, RX - 10)
    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    /* Connect USART pins to AF */
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

    USART_StructInit(&USART_InitStructure);
    USART_InitStructure.USART_BaudRate = uart_baudrate;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

    USART_Init(USART3, &USART_InitStructure);
    USART_Cmd(USART3, ENABLE);
}

#define UART_BUF_LEN 64
volatile static char uartBuffer[UART_BUF_LEN]; 
volatile static int  uartBufferIn = 0;
volatile static int  uartBufferOut = 0;

//Uart rx handler
void USART3_IRQHandler(void){
    unsigned int st1;
    st1 = __disable_interrupts();
    if(USART_GetITStatus(USART3, USART_IT_RXNE) == SET){
        USART_ClearITPendingBit(USART3, USART_IT_RXNE);
        uartBuffer[uartBufferIn] = USART_ReceiveData(USART3);       
        uartBufferIn++;
        if(uartBufferIn==uartBufferOut)uartBufferOut++;
        uartBufferIn&=0x3F;
        uartBufferOut&=0x3F;
    }
    USART_Cmd(USART3, ENABLE);
        __restore_interrupts(st1);
}

//Enable uart rx interrupt
void Uart_Start(void){
    unsigned int st1;
    st1 = __disable_interrupts();

    NVIC_InitTypeDef NVIC_InitStructure;
    VectorTable->USART3_IRQHANDLER = USART3_IRQHandler;
    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
    USART_Cmd(USART3, ENABLE);
    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

    __restore_interrupts(st1);
}

0 个答案:

没有答案