1

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 をチェック済み)、その間 (USART_GetFlagStatus...) で際限なくスタックします。Keil デバッガーを使用して観察していると、しばらくすると動かなくなることがわかります。

stm32f4xx_usart.c ドライバーをプロジェクトに含めています。

ありがとう!

4

1 に答える 1

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);
于 2012-12-11T12:46:53.763 に答える