STM32F303K8的USART激活未成功

时间:2019-05-06 14:22:14

标签: interrupt stm32 uart usart visualgdb

正在尝试激活并利用STM32F303K8上的USART1外设。在VisualGDB中使用StdPeriph运行它。但是,我无法使用中断模式启动USART。即使我探测针脚,它们也都保持沉默。我想念什么。花了很多天。

#include <stm32f30x_gpio.h>
#include <stm32f30x_rcc.h>
#include "stm32f30x_usart.h"
#include "stm32f30x_misc.h"

void SysTick_Handler()
{
    msTicks++;
}

void setSysTick()
{
    if (SysTick_Config(SystemCoreClock / 1000))
    {
        while (1) {}
    }
}

void Delay()
{
    int i;
    for (i = 0; i < 1000000; i++)
        asm("nop");
}

void init_peripherals(void)
{
    GPIO_InitTypeDef    GPIO_InitStructure;
    USART_InitTypeDef   USART_InitStructure; 
    NVIC_InitTypeDef    NVIC_InitStructure;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_9 | GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9 | GPIO_PinSource10, GPIO_AF_7);

    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_Init(USART1, &USART_InitStructure);

    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

    USART_Cmd(USART1, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_Init(&NVIC_InitStructure);
    USART_Cmd(USART1, ENABLE);
}

void USART_puts(USART_TypeDef* usar_tx, volatile char* str)
{
    while (*str)
    {
        while (USART_GetFlagStatus(usar_tx, USART_FLAG_TC) == RESET) {}
        USART_SendData(usar_tx, *str);
        str++;
    }
}

void  USART1_IRQHandler(void) 
{
    if (USART1->ISR & USART_ISR_RXNE)
    {
        const char ch = USART_ReceiveData(USART1);

        if (ch != '\n' && ch != '\r')
        {
            rcvd_str[cnt++] = ch;
        }
        else
        {
            rcvd_str[cnt] = '\r';
            cnt = 0;
            str_buildup = true;
        }
    }
}

int main()
{

    setSysTick();
    init_peripherals();
    char* strr = "Hello World\n";
    USART_puts(USART1, strr);
    return 0;
}

预期结果是像realterm一样在串行终端中打印helloWorld。我还没到那儿。

2 个答案:

答案 0 :(得分:1)

一旦中断到达,它将在上面的代码处理程序中相应地开始执行其处理程序,该程序正在检查终止字符并将该字符存储在字符串中,但是在退出中断处理程序之前需要清除中断。 NVIC初始化后,再次添加USART_cmd并错过了NVIC_init进行检查。

答案 1 :(得分:0)

GPIO_PinSourceX不应该是位掩码。尽管很难知道哪些操作不需要位屏蔽