【问题标题】:UART with STM32F407 (F4Discovery)UART 与 STM32F407 (F4Discovery)
【发布时间】:2012-03-26 13:47:42
【问题描述】:

我在尝试使用我的 F4Discovery 板(基于 STM32F407)上的 UART (USART1) 时遇到问题。我对 STM32 和 Keil(我正在使用的 IDE)很陌生。这是我的代码:

#include "stm32f4_discovery.h"
#include "stm32f4xx_usart.h"
#include "stm32f4xx.h"

void usartSetup(void);
void USART_PutChar(uint8_t ch);

int main (void) {
    usartSetup();

    USART_PutChar(0);   //I realise this won't send a 0
    USART_PutChar(8);   //I realise this won't send an 8
    USART_PutChar(255);   //I realise this won't send a 255

    while (1) {
        //loop forever
    }
}

void usartSetup (void) {
    USART_InitTypeDef USART_InitStructure;
    //USART_StructInit(&USART_InitStructure);
    USART_InitStructure.USART_BaudRate = 9600;
    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);
    USART_Cmd(USART1, ENABLE);
}

void USART_PutChar(uint8_t ch) {
    USART_SendData(USART1, (uint16_t) 0x49);
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
    USART_SendData(USART1, (uint16_t) 0x49);
}

如果有人能提供帮助,我将不胜感激。它永远不会从 UART1 TX 发送 0x49(已检查引脚 PA9 和 PB6),然后无休止地卡在 while(USART_GetFlagStatus ...)上。我正在使用 Keil 调试器进行观察,发现它在一段时间内卡住了。

我正在将 stm32f4xx_usart.c 驱动程序包含到项目中。

谢谢!

【问题讨论】:

  • github.com/dwelch67/stm32f4d 有一些 uart 例程,低级嵌入。也许他们会有所帮助,也许不会。
  • 谢谢,一个非常有用的链接。看看 uart01,代码离我的太远了,我很难弥合差距。
  • 问题在于未配置 GPIO 和时钟。我在ST论坛上找到了一些不错的信息。

标签: arm stm32


【解决方案1】:

您尚未将输出引脚配置为使用 USART 和时钟。如果您使用 USART1 或 USART6,则必须针对 APB1(低速)设置 APB2 时钟(高速)

/* For output on GPIOA */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2);

/* Output pins configuration, change M and N index! */
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_N | GPIO_Pin_M;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // Push - pull
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOA, GPIO_PinSourceN, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSourceM, GPIO_AF_USART2);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 2018-06-08
    • 1970-01-01
    • 2020-07-20
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多