我最近购买了NUCLEO-F446RE主板(STM32F4产品),一个小问题一直困扰着我。我编写的所有代码都可以正常执行,但只有按下NUCLEO板上的重置按钮才能正常工作。
IDE:Keil v5
例如,我为闪烁的LED编写代码:
#include "stm32f446xx.h"
int main(void) {
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
GPIOA->MODER |= GPIO_MODER_MODE5_0;
GPIOA->ODR |= GPIO_ODR_OD5;
volatile int i;
while(1) {
for(i=0; i<100000; i++)
GPIOA->ODR |= GPIO_ODR_OD5;
for(i=0; i<100000; i++)
GPIOA->ODR &= ~GPIO_ODR_OD5;
}
}
运行并将代码下载到电路板上后,什么都不会发生。按下复位后,LED将按预期闪烁。
我非常肯定我的代码中没有包含这些内容,因为当我运行示例程序时,它会立即执行。
例如,KIEL提供的闪烁LED:
#include <stdio.h>
#include "Board_LED.h" // ::Board Support:LED
#include "Board_Buttons.h" // ::Board Support:Buttons
#include "stm32f4xx.h" // Device header
extern int stdout_init (void);
volatile uint32_t msTicks; /* counts 1ms timeTicks */
/*----------------------------------------------------------------------------
* SysTick_Handler:
*----------------------------------------------------------------------------*/
void SysTick_Handler(void) {
msTicks++;
}
/*----------------------------------------------------------------------------
* Delay: delays a number of Systicks
*----------------------------------------------------------------------------*/
void Delay (uint32_t dlyTicks) {
uint32_t curTicks;
curTicks = msTicks;
while ((msTicks - curTicks) < dlyTicks) { __NOP(); }
}
/*----------------------------------------------------------------------------
* SystemCoreClockConfigure: configure SystemCoreClock using HSI
(HSE is not populated on Nucleo board)
*----------------------------------------------------------------------------*/
void SystemCoreClockConfigure(void) {
RCC->CR |= ((uint32_t)RCC_CR_HSION); /* Enable HSI */
while ((RCC->CR & RCC_CR_HSIRDY) == 0); /* Wait for HSI Ready */
RCC->CFGR = RCC_CFGR_SW_HSI; /* HSI is system clock */
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI); /* Wait for HSI used as system clock */
FLASH->ACR = (FLASH_ACR_PRFTEN | /* Enable Prefetch Buffer */
FLASH_ACR_ICEN | /* Instruction cache enable */
FLASH_ACR_DCEN | /* Data cache enable */
FLASH_ACR_LATENCY_5WS ); /* Flash 5 wait state */
RCC->CFGR |= (RCC_CFGR_HPRE_DIV1 | /* HCLK = SYSCLK */
RCC_CFGR_PPRE1_DIV2 | /* APB1 = HCLK/2 */
RCC_CFGR_PPRE2_DIV1 ); /* APB2 = HCLK/1 */
RCC->CR &= ~RCC_CR_PLLON; /* Disable PLL */
/* PLL configuration: VCO = HSI/M * N, Sysclk = VCO/P */
RCC->PLLCFGR = ( 16ul | /* PLL_M = 16 */
(200ul << 6) | /* PLL_N = 200 */
( 0ul << 16) | /* PLL_P = 2 */
(RCC_PLLCFGR_PLLSRC_HSI) | /* PLL_SRC = HSI */
( 7ul << 24) | /* PLL_Q = 7 */
( 2ul << 28) ); /* PLL_R = 2 */
RCC->CR |= RCC_CR_PLLON; /* Enable PLL */
while((RCC->CR & RCC_CR_PLLRDY) == 0) __NOP(); /* Wait till PLL is ready */
RCC->CFGR &= ~RCC_CFGR_SW; /* Select PLL as system clock source */
RCC->CFGR |= RCC_CFGR_SW_PLL;
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL); /* Wait till PLL is system clock src */
}
/*----------------------------------------------------------------------------
* main: blink LED and check button state
*----------------------------------------------------------------------------*/
int main (void) {
int32_t max_num = LED_GetCount();
int32_t num = 0;
SystemCoreClockConfigure(); /* configure HSI as System Clock */
SystemCoreClockUpdate();
LED_Initialize();
Buttons_Initialize();
stdout_init(); /* Initializ Serial interface */
SysTick_Config(SystemCoreClock / 1000); /* SysTick 1 msec interrupts */
for (;;) {
LED_On(num); /* Turn specified LED on */
Delay(500); /* Wait 500ms */
while (Buttons_GetState() & (1 << 0)); /* Wait while holding USER button */
LED_Off(num); /* Turn specified LED off */
Delay(500); /* Wait 500ms */
while (Buttons_GetState() & (1 << 0)); /* Wait while holding USER button */
num++; /* Change LED number */
if (num >= max_num) {
num = 0; /* Restart with first LED */
}
printf ("Hello World\n\r");
}
}
示例代码似乎没有任何特殊功能可以立即运行。
非常感谢任何帮助。
答案 0 :(得分:3)
我的猜测是它在项目的flash工具设置中,可能是“run to main”或“Startup at Startup”(不是你的代码)。要检查,请将代码复制/粘贴到其中一个样本的顶部。
答案 1 :(得分:1)
我遇到了同样的问题。在Keil中,您需要转到“ Flash->配置Flash工具->实用工具”并打开“运行并重置”选项
关于:)
答案 2 :(得分:1)
在基尔,按照以下步骤操作:
上传代码,它无需单击重置按钮即可工作。