簡體   English   中英

具有STM32F407的UART(F4Discovery)

[英]UART with STM32F407 (F4Discovery)

我在嘗試在我的F4Discovery板上使用UART(USART1)時遇到問題(基於STM32F407)。 我對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);
}

如果有人可以幫助我,我將非常感激。 它從不發送0x49到UART1 TX(已經檢查了引腳PA9和PB6),然后無休止地停留在while(USART_GetFlagStatus ...)。 我正在觀察使用Keil調試器,發現它在一段時間內卡住了。

我將stm32f4xx_usart.c驅動程序包含到項目中。

謝謝!

您尚未將輸出引腳配置為使用USART和時鍾。 如果使用USART1或USART6,則必須將APB2時鍾(高速)相對於APB1(低速)設置

/* 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM