//#include "main.h"
// Define button and LED pins
#define BUTTON_PIN GPIO_PIN_13 // For user button (on Nucleo boards, it is usually connected to PC13)
#define BUTTON_GPIO_PORT GPIOC // Change to appropriate GPIO port if needed
#define LED_PIN GPIO_PIN_5 // For user LED (on Nucleo boards, it is often connected to PA5)
#define LED_GPIO_PORT GPIOA // Change to appropriate GPIO port if needed
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
int main(void) {
// Initialize the HAL Library
HAL_Init();
// Configure the system clock
SystemClock_Config();
// Initialize all configured peripherals (GPIO in this case)
MX_GPIO_Init();
// Main loop
while (1) {
// Check if the button is pressed
if (HAL_GPIO_ReadPin(BUTTON_GPIO_PORT, BUTTON_PIN) == GPIO_PIN_RESET) {
// Turn on the LED
HAL_GPIO_WritePin(LED_GPIO_PORT, LED_PIN, GPIO_PIN_SET);
} else {
// Turn off the LED
HAL_GPIO_WritePin(LED_GPIO_PORT, LED_PIN, GPIO_PIN_RESET);
}
}
}
void SystemClock_Config(void) {
// System clock configuration code (auto-generated by STM32CubeMX for your specific setup)
// This part should remain as generated if using STM32CubeIDE or STM32CubeMX
}
static void MX_GPIO_Init(void) {
// GPIO Initialization for button and LED
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Enable GPIO clocks
__HAL_RCC_GPIOC_CLK_ENABLE(); // Enable clock for GPIOC (button port)
__HAL_RCC_GPIOA_CLK_ENABLE(); // Enable clock for GPIOA (LED port)
// Configure GPIO pin for Button (input with pull-up resistor)
GPIO_InitStruct.Pin = BUTTON_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(BUTTON_GPIO_PORT, &GPIO_InitStruct);
// Configure GPIO pin for LED (output)
GPIO_InitStruct.Pin = LED_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
}