STM32F4程序仅在按下复位按钮后运行

问题描述 投票:-1回答:3

我最近购买了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");
  }

}

示例代码似乎没有任何特殊的东西可以立即运行。

任何帮助是极大的赞赏。

c microcontroller reset stm32f4
3个回答
3
投票

我的猜测是它在项目的flash工具设置中,可能是“run to main”或“Startup at Startup”(不是你的代码)。要检查,请将代码复制/粘贴到其中一个示例的顶部。


1
投票

我遇到了同样的问题。在Keil中,您需要转到“Flash - >配置Flash工具 - >实用程序”并打开“运行并重置”选项

问候:)


1
投票

在基尔跟随步骤跟随:

  1. 转到“Flash”选项。
  2. 在闪存中,转到“配置Flash工具”。
  3. 转到最后一个栏/选项“Utilities”
  4. 在实用程序中选择“设置”选项。
  5. 最后,勾选“运行并重置”选项。

上传代码,它没有点击重置按钮。

© www.soinside.com 2019 - 2024. All rights reserved.