[STM32F4] 代码仅在调试模式下能运行

[复制链接]
8|0
我的牙白 发表于 2026-11-19 08:51 | 显示全部楼层 |阅读模式
STM32F407 Discovery 开发板,外接 TTL 模块。IDE为 Keil。串口使用 PB6、PB7 引脚;默认 PA9 引脚在 USB 电路中用作 VBUS,若配置使用该引脚会打印乱码。

目前测试循环打印字符 'A',等待用户输入;但串口监视器无输出,仅开启调试模式后才能正常输出。
#include <stdint.h>
#include <stdio.h>
#include "stm32f4xx.h"

volatile uint8_t rxdata = 0;

int main(void)
{
SystemInit();
SystemCoreClockUpdate();

// clock configutation
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // TX: PB6 RX: PB7
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); // LED LED4: PD12 LED3: PD13
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);

// gpio initialization
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);

GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);

// usart initialization
USART_InitTypeDef USART_InitStruct;
USART_StructInit(&USART_InitStruct);
USART_InitStruct.USART_BaudRate = 115200;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &USART_InitStruct);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE); // enable usart1

NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);


char msg[64];
while(1){
USART_SendData(USART1, 'A');
if (rxdata!= 0) {
int idx = 0;
int len = snprintf(msg, sizeof(msg), "Recieved: %c", (char)rxdata);
for (int idx = 0; idx < len; idx++) {
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, (uint16_t)msg[idx]);
}
rxdata = 0;
}
}
}

void USART1_IRQHandler(void) {
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
rxdata = (uint8_t)USART_ReceiveData(USART1); // echo
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}

您需要登录后才可以回帖 登录 | 注册

本版积分规则

248

主题

249

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部