【发布时间】: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
}
}
【问题讨论】: