Here is an example code that implements the desired functionality using timers, external and internal interrupts in STM32CubeIDE:

#include "main.h"
#include "stm32f4xx_hal.h"

/* Private variables */
TIM_HandleTypeDef htim2;
GPIO_InitTypeDef GPIO_InitStruct;
uint8_t buttonState = 0;
uint8_t ledState = 0;

/* Function prototypes */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);

int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_TIM2_Init();

  while (1)
  {
    if (buttonState == 1 && ledState == 0)
    {
      HAL_TIM_Base_Start_IT(&htim2); // Start the timer and enable interrupts
      ledState = 1;
    }
    else if (buttonState == 1 && ledState == 1)
    {
      HAL_TIM_Base_Stop_IT(&htim2); // Stop the timer and disable interrupts
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // Turn off the LED
      ledState = 0;
    }
  }
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  if (GPIO_Pin == GPIO_PIN_0)
  {
    buttonState = 1 - buttonState; // Toggle the button state
  }
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  if (htim->Instance == TIM2)
  {
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); // Toggle the LED state
  }
}

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  __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();
  }
}

static void MX_GPIO_Init(void)
{
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);

  /*Configure GPIO pin : PA0 */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* EXTI interrupt init */
  HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI0_IRQn);
}

void EXTI0_IRQHandler(void)
{
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
}

static void MX_TIM2_Init(void)
{
  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 15999; // Set the prescaler value for a 1ms tick
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 1499; // Set the counter period to achieve a 1.5s delay
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
}

Key Components:

  • Timer: TIM2 is used to generate a 1.5-second delay.
  • Prescaler: Set to 15999 to achieve a 1ms tick (based on the system clock frequency).
  • Counter Period: Set to 1499 to achieve a 1.5-second delay (1500ms).
  • External Interrupt: The button press is configured as an external interrupt on GPIO pin PA0.
  • Internal Interrupt: The timer interrupt is used to toggle the LED state every 1ms.

Code Explanation:

  1. Initialization: The code initializes the timer, GPIO pins, and sets up interrupts.
  2. Button Press Detection: The HAL_GPIO_EXTI_Callback() function is triggered on a rising edge from the button (PA0). It toggles the buttonState flag.
  3. Timer Control:
    • When the button is pressed and the LED is off (buttonState == 1 && ledState == 0), the timer is started (HAL_TIM_Base_Start_IT()), and the LED state is set to on (ledState = 1).
    • When the button is pressed again and the LED is on (buttonState == 1 && ledState == 1), the timer is stopped (HAL_TIM_Base_Stop_IT()), and the LED is turned off.
  4. LED Toggling: The HAL_TIM_PeriodElapsedCallback() function, triggered every 1ms by the timer, toggles the LED state (HAL_GPIO_TogglePin()).

Remember:

  • Ensure to connect the button to PA0 and the LED to PA5. Adjust these pins based on your hardware connections.
  • Adapt the code for other STM32 microcontrollers by modifying the HAL library, header files, and clock configuration.
  • This example uses the HSI (High-Speed Internal) oscillator as the system clock. You can adjust the prescaler value if you are using a different clock source.
STM32CubeIDE: Blink LED for 1.5 Seconds with Button Press using Timers & Interrupts

原文地址: https://www.cveoy.top/t/topic/o0o 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录