为什么我不能在Keil中为STM32F446定义一个可执行变量?

why I can not define an executable variable for STM32F446 in Keil?

我想为带寄存器的STM32F446 MCU 编写代码(无Hal 函数)。这是我的代码,但我无法在此代码中定义任何变量。我在此代码中定义的任何变量都不可执行。例如,我在代码的末尾定义了一个变量“timer”,它在无限循环中递增。但在调试时,指针从“timer++”行跳转,并没有执行。我该如何解决?

#include "stm32f446xx.h"                  // Device header

void sysClockConfig (void);
void GPIO_Config (void);


void sysClockConfig (void)
{
    
    #define PLL_M       8
    #define PLL_N       72
    #define PLL_P       2
    
    // 1. Enable HSE and wait for the HSE to be ready
    RCC->CR |= RCC_CR_HSION;
    while (!(   RCC->CR & RCC_CR_HSIRDY ));
    
    // 2.   Set the power enable clock and the voltage regulator
    RCC->APB1ENR |= RCC_APB1ENR_PWREN;
    PWR->CR |= PWR_CR_VOS;
    
    // 3. Configure the flash prefetch and the LATANCY related setting
    FLASH->ACR |= FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_PRFTEN | FLASH_ACR_LATENCY_2WS;
    
    // 4. Configure prescalar HCLK, PCLK1, PCLK2
    // AHB PR
    RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
    
    // APB1 PR
    RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;
    
    // APB2 PR
    RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
    
    // 5. Configure the main PLL
    RCC->PLLCFGR = (PLL_M << 0) | (PLL_N << 6) | (PLL_P << 16) | (RCC_PLLCFGR_PLLSRC_HSI);
    
    // 6. Enable PLL and wait for it to become ready
    RCC->CR |= RCC_CR_PLLON;
    while    (!(RCC->CR & RCC_CR_PLLRDY));
    
    // 7. Select the clock source and wait for it to be set
    RCC->CFGR |= RCC_CFGR_SW_PLL;
    while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL); 
}

void GPIO_Config (void)
{
    // 1. Enable the GPIO clock
    RCC->AHB1ENR |= (1<<0);
    
    // 2. Set the pin as output
    GPIOA->MODER |= (1<<10);                    // pin PA5(bits 11:10) as output (01) 
    
    // 3. Configure the output mode
    GPIOA->OTYPER = 0;
    GPIOA->OSPEEDR = 0; 
}



int main(void)
{
    int timer = 100 ; 
    GPIO_Config();
    sysClockConfig();
    

while(1)
    {
        GPIOA->BSRR |= (1<<5);          //set PA5
        timer++;
        GPIOA->BSRR |= ((1<<5) <<16);       //reset PA5
        
        }           
    }


除非你对可变的 timer 做一些事情(例如打印出来),否则编译器会告诉你不需要它并通过不真正访问它来节省时间。

如果您使用它的原因是浪费时间,请尝试声明它 volatile int timer

另外请注意,您是在设置和清除 PA5 之间浪费时间,而不是在清除和设置之间浪费时间。也许回到循环开始的跳转指令会浪费一些时间,但也许这不是你期望的地方。