USART收到废话(STM32F0)

时间:2018-03-14 13:26:48

标签: c interrupt stm32 baud-rate usart

我有一个从GPS模块到STM32F091微控制器的USART通信实现。 出于某种原因,我只收到废话。我已经尝试过设置不同的波特率,但没有一个给我一个合适的结果。

现在我不确定是否有预设软件设置或我遗忘的其他内容。

相关代码如下:

  

peripherals.c

/* Clock settings */
void clock_init(void)
{
    /* Enable HSI clock */
    RCC->CR |= RCC_CR_HSION;

    /* Enable clocks on Port A, B, F */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE);

    /* Enable USART clocks */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
}

/* Select Alternate Port Functions */
void alt_init(void)
{
    /* Port A: USART1 */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);

    /* Port B: USART3 */
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_4);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_4);
}
  

usart.c

USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

/* Baud rate */
uint32_t bd = 9600;

void usart_init(void)
{

    /* ##################### USART 1 ##################### */
    /* USART GPIO Settings */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* USART Settings */
    USART_InitStructure.USART_BaudRate = bd;
    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(USART1, &USART_InitStructure);

    /* Enable USART because it's disabled in USART_Init */
    USART_Cmd(USART1, ENABLE);

    /* ##################### USART 2 ##################### */

    /* ##################### USART 3 ##################### */
    /* Disable USART3 in order to use SWAP */
    USART_Cmd(USART3, DISABLE);

    /* Swap TX and RX pins */
    USART3->CR2 |= USART_CR2_SWAP;

    /* Enable USART3 */
    USART_Cmd(USART3, ENABLE);

    /* USART GPIO Settings */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    /* USART Settings */
    USART_InitStructure.USART_BaudRate = bd;
    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);

    /* Enable USART because it's disabled in USART_Init */
    USART_Cmd(USART3, ENABLE);

    /* Enable USART Interrupts */
    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
    USART_ITConfig(USART3, USART_IT_TXE, ENABLE);

    /* Enable external interrupts */
    NVIC_InitStructure.NVIC_IRQChannel = USART3_8_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    /* ##################### USART 4 ##################### */

}
  

stm32f0xx_it.c

#include <main.h>

void USART3_8_IRQHandler(void)
{
    char t[] = "U";
    uint16_t byte = 0;
    if (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) != RESET) // Data reception
    {
        byte = USART_ReceiveData(USART3);
        (*pRXD3).Buffer[(*pRXD3).wr] = byte;
        (*pRXD3).wr = ((*pRXD3).wr + 1);

        USART_SendData(USART1, t[0]);

        USART_GetFlagStatus(USART3, USART_IT_ORE);
        USART_ClearITPendingBit(USART3, USART_IT_RXNE);
    }
    return;
}

USART_SendData位用于与我的PC进行通信,因此我可以看到是否收到U或更多的字母。

我还检查了USART3的DR寄存器内容(通过检查缓冲区我写入的内容),所以我知道STM32F091接收(或者更确切地说是间隔)我的GPS输出错误。

GPS模块使用默认波特率9600,我不会更改。

如果有任何其他信息,请告诉我

1 个答案:

答案 0 :(得分:1)

经过大量的搜索和评论中每个人的建议,我发现系统时钟没有正确设置。 除了已经在&#39; clock_init&#39;中已经存在的内容之外,还需要以下代码。

  

peripherals.c

/* Clock settings */
void clock_init(void)
{
    ....

    /* Select System Clock to be HSI */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);

    /* Select USART clock source to be System Clock */
    RCC_USARTCLKConfig(RCC_USART1CLK_SYSCLK);
    RCC_USARTCLKConfig(RCC_USART3CLK_SYSCLK);

    ....

}

感谢大家的帮助。