你好。首先,我要感谢您抽出时间阅读这篇文章。当我创建用于闪烁 LED 的裸机程序时,它不起作用。LED 不闪烁,但是当我使用 KEIL uVision 进入调试模式时,GPIOC 的 ODR 寄存器中的位 0 每 1 秒切换一次,但是即使设置了 ODR 位 0,LED 也根本不会亮。
使用 CubeMX 制作的相同程序(由 CubeMX 生成的时钟初始化但没有为 GPIO 生成代码)同样的问题。
让 LED 闪烁的唯一方法是当我使用 HAL 库和 CubeMX 来配置特定的 GPIOC 位时。
下面我附上了代码。如果你能帮助我,我将不胜感激。我很抱歉我的英语不好。
- #include "STM32f10x.h"
- void Config(void);
- void Delay_in_ms(uint16_t valoare); //timer count on each 1 ms
- int main()
- {
- Config(); //Peripheral configuration
- while(1)
- {
- GPIOC->BSRR |= 1<<0; // Set pin0 of GPIOC
- Delay_in_ms(1000);
- GPIOC->BSRR |= 1<<16; // Cleat pin0 of GPIOC
- Delay_in_ms(1000);
- }
- }
- void Delay_in_ms(uint16_t valoare)
- {
- TIM6->ARR = (valoare)*10; // Load ARR register with the value needed in delay(ms)
- TIM6->CR1 |= 1<<0; // Enable TIM6 (now TIM6 start counting)
- while(!(TIM6->SR)); // Stays here until UEV has been set so the counter reached the desired value in ms)
- TIM6->SR = 0; // Clear flag
- }
- void Config(void)
- {
- // Flash Config
- FLASH->ACR |= 1<<4; // Enable prefetch buffer
- while( !((FLASH->ACR)&(1<<5)) ); // Stays here until prefetch buffer is enabled.
- FLASH->ACR |= 1<<1; // Set LATENCY to 2 wait states
- // RCC Config
- RCC->CR |= 1<<16; // Enable high-speed external clock (HSE)
- while( !((RCC->CR)&(1<<17)) ); // Stays here until HDE Clock is ready to use
- RCC->CFGR |= ((1<<20)|(1<<16)|(1<<10)); // Configure the PLL (multiply input of PLL by 4, select HSE as PLL input clock, and divide the APB1 clock by 2)
- RCC->CR |= 1<<24; // Enable PLL
- while( !((RCC->CR)&(1<<25)) ); // Stays here until PLL PLL get locked (when PLL locked, exit the loop)
- RCC->CFGR |= 1<<1; // Select PLL_Clock as System Clock (SYSCLK)
- while( !((RCC->CFGR)&(1<<3)) ); // Stays here until PLL is used as system clock (if PLL drives the SYSCLK then exit loop)
- // TIM6 Config (Basic timer config used to generate delays in ms)
- RCC->APB1ENR |= 1<<4; // Enable clock for TIM6_Basic_Timer
- TIM6->CR1 |= ((1<<7)|(1<<3)|(1<<2)); // Enable buffering for ARR, Counter stops counting at the next UEV, only under/over flow generates UEV
- TIM6->PSC = 7199; // Timer count each 100us which is equivalent to 0.1 ms
- // GPIOC Config ( PC0, PC1, PC2 output with PU )
- RCC->APB2ENR |= ((1<<4)|(1<<2)|(1<<5)); // Activeaza ceasul pentru PORT_C
- GPIOC->CRL |= ((1<<1)|(1<<5)|(1<<9)); // PC0 & PC1 & PC2 output push-pull
- }