#include "stm32c0xx_hal.h"
bool led_enabled = false;
GPIO_PinState prev_button_state = GPIO_PIN_SET;
void setup() {
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_LedStruct = {0};
GPIO_LedStruct.Pin = GPIO_PIN_6;
GPIO_LedStruct.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(GPIOA, &GPIO_LedStruct);
GPIO_InitTypeDef GPIO_ButtonStruct = {0};
GPIO_ButtonStruct.Pin = GPIO_PIN_0;
GPIO_ButtonStruct.Mode = GPIO_MODE_INPUT;
GPIO_ButtonStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_ButtonStruct);
}
void loop() {
const uint32_t DEBOUNCE_MS = 30;
GPIO_PinState curr_button_state = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
if (curr_button_state != prev_button_state) {
HAL_Delay(DEBOUNCE_MS);
curr_button_state = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
if (curr_button_state != prev_button_state) {
if (curr_button_state == GPIO_PIN_RESET) {
led_enabled = !led_enabled;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, led_enabled ? GPIO_PIN_SET : GPIO_PIN_RESET);
}
prev_button_state = curr_button_state;
}
}
}