STM32 Timer and Interrupt Based LED Blink: 1.5 Second Toggle with Button Press
#include "stm32f4xx.h"
// Function prototypes void SystemClock_Config(void); void EXTI15_10_IRQHandler(void);
// Global variables volatile uint8_t button_pressed = 0;
int main(void) { // Configure system clock SystemClock_Config();
// Enable GPIO clock for PC13 (button)
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;
// Enable GPIO clock for PB0 (LED)
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
// Configure PC13 as input with pull-up
GPIOC->MODER &= ~GPIO_MODER_MODE13;
GPIOC->PUPDR |= GPIO_PUPDR_PUPD13_0;
// Configure PB0 as output
GPIOB->MODER |= GPIO_MODER_MODE0_0;
// Enable SYSCFG clock
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
// Connect PC13 to EXTI13
SYSCFG->EXTICR[3] |= SYSCFG_EXTICR4_EXTI13_PC;
// Configure EXTI13 to trigger on rising edge
EXTI->RTSR |= EXTI_RTSR_TR13;
// Enable EXTI13 interrupt
EXTI->IMR |= EXTI_IMR_IM13;
// Enable NVIC interrupt for EXTI15_10_IRQn
NVIC_EnableIRQ(EXTI15_10_IRQn);
while (1)
{
if (button_pressed)
{
button_pressed = 0;
// Blink the LED for 1.5 seconds
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
HAL_Delay(1500);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
}
}
}
// System Clock Configuration void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct;
// Enable Power Control clock
__HAL_RCC_PWR_CLK_ENABLE();
// The voltage scaling allows optimizing the power consumption when the device is
// clocked below the maximum system frequency, to update the voltage scaling value
// regarding system frequency refer to product datasheet.
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
// Enable HSE Oscillator and activate PLL with HSE as source
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
// Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
// Configure the Systick interrupt time
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
// Configure the Systick
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
// SysTick_IRQn interrupt configuration
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
// EXTI interrupt handler void EXTI15_10_IRQHandler(void) { // Check if EXTI13 interrupt occurred if (EXTI->PR & EXTI_PR_PR13) { // Clear EXTI13 interrupt flag EXTI->PR |= EXTI_PR_PR13;
// Toggle button_pressed variable
button_pressed = !button_pressed;
}
}
原文地址: http://www.cveoy.top/t/topic/n4t 著作权归作者所有。请勿转载和采集!