STM32Cube HAL: Blink Built-in LED with Timer Interrupts and Button Control
Here's an example code using STM32Cube HAL library to achieve the desired functionality:
#include "stm32f4xx_hal.h"
TIM_HandleTypeDef htim2;
GPIO_InitTypeDef GPIO_InitStruct;
uint8_t led_state = 0;
void SystemClock_Config(void);
void GPIO_Init(void);
void TIM2_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
GPIO_Init();
TIM2_Init();
while (1)
{
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
{
HAL_Delay(10);
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
{
HAL_TIM_Base_Stop_IT(&htim2); // Stop the timer and interrupts
if (led_state)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // Turn off the LED
led_state = 0;
}
else
{
HAL_TIM_Base_Start_IT(&htim2); // Start the timer and interrupts
led_state = 1;
}
while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
; // Wait until the button is released
}
}
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
void GPIO_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void TIM2_Init(void)
{
__HAL_RCC_TIM2_CLK_ENABLE();
htim2.Instance = TIM2;
htim2.Init.Prescaler = 64; // Prescaler value
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 23437; // Timer period for 1.5 seconds (1.5 * (16MHz/65))
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(&htim2);
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
}
void TIM2_IRQHandler(void)
{
HAL_TIM_IRQHandler(&htim2);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); // Toggle the LED
}
In this code, we use TIM2 as the timer to toggle the LED. The timer is configured to generate an interrupt every 1.5 seconds (1500 ms) using a prescaler value of 64 and a period value of 23437 (1.5 * (16MHz/65)).
The GPIOA_PIN_5 is used as the LED pin, and GPIOA_PIN_0 is used as the button pin. The button pin is configured as input with a pull-up resistor, and the LED pin is configured as output.
In the main loop, we continuously check if the button is pressed. If the button is pressed, we wait for a short delay to debounce the button. Then, we check again if the button is still pressed to avoid false triggers. If the button is still pressed, we stop the timer and interrupts using 'HAL_TIM_Base_Stop_IT' function. If the LED state is on, we turn off the LED and set the LED state to 0. Otherwise, we start the timer and interrupts using 'HAL_TIM_Base_Start_IT' function, and set the LED state to 1. We also wait until the button is released to avoid multiple triggers.
In the timer interrupt handler ('TIM2_IRQHandler'), we toggle the LED state using 'HAL_GPIO_TogglePin' function.
Note: This code assumes that the LED pin is connected to PA5 and the button pin is connected to PA0. You may need to modify the GPIO initialization and pin definitions according to your specific hardware setup.
原文地址: https://www.cveoy.top/t/topic/nSz 著作权归作者所有。请勿转载和采集!