【问题标题】:why I can not define an executable variable for STM32F446 in Keil?为什么我不能在 Keil 中为 STM32F446 定义可执行变量?
【发布时间】:2021-08-04 08:12:15
【问题描述】:

我想为带有寄存器的 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
        
        }           
    }


【问题讨论】:

    标签: keil stm32f4


    【解决方案1】:

    除非您对可变的timer 执行某些操作(例如将其打印出来),否则编译器会告诉您您不需要它并通过不真正访问它来节省时间。

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

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

    【讨论】:

    • 非常感谢。是的,我想浪费时间并用 volatile int 解决问题。
    猜你喜欢
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 2014-01-17
    • 1970-01-01
    • 2021-03-30
    相关资源
    最近更新 更多