/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h" // Includes the HAL (Hardware Abstraction Layer) library definitions for STM32.
uint8_t button; // Variable to hold the button state (pressed or not).
// Function prototypes
void SystemClock_Config(void); // Configures the system clock to set up the microcontroller's clock frequency.
static void MX_GPIO_Init(void); // Configures the GPIO peripherals (pins for the button and LEDs).
int main(void) {
button = 0; // Initialize the button variable to 0 (indicating not pressed).
HAL_Init(); // Initializes the HAL Library. This sets up the MCU, including the SysTick timer for delays.
SystemClock_Config(); // Configures the clock system for proper timing.
MX_GPIO_Init(); // Initializes GPIO pins for input (button) and output (LEDs).
while (1) { // Infinite loop: Keeps the microcontroller running continuously.
// Read the state of the button connected to GPIO pin PC7.
button = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_7);
if (button == 0) { // If the button is pressed (logic 0 due to pull-down configuration):
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); // Turn ON LED2 (PA1).
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET); // Turn OFF LED1 (PA0).
} else { // If the button is not pressed (logic HIGH):
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); // Turn OFF LED2 (PA1).
// Implement the SOS Morse code pattern on LED1 (PA0).
// SOS pattern consists of three short flashes, three long flashes, and three short flashes.
// Three short flashes
for (int i = 0; i < 3; i++) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET); // Turn ON LED1.
HAL_Delay(200); // Short ON delay (200ms).
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET); // Turn OFF LED1.
HAL_Delay(200); // Short OFF delay (200ms).
}
// Three long flashes
for (int i = 0; i < 3; i++) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET); // Turn ON LED1.
HAL_Delay(600); // Long ON delay (600ms).
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET); // Turn OFF LED1.
HAL_Delay(200); // Short OFF delay (200ms).
}
// Three short flashes
for (int i = 0; i < 3; i++) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET); // Turn ON LED1.
HAL_Delay(200); // Short ON delay (200ms).
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET); // Turn OFF LED1.
HAL_Delay(200); // Short OFF delay (200ms).
}
HAL_Delay(1000); // Pause for 1 second before repeating the SOS sequence.
}
}
}
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0}; // Configures the internal high-speed oscillator (HSI).
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; // Sets up the clock source and dividers.
// Enable the High-Speed Internal Oscillator (HSI) and configure it as the clock source.
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON; // Enable HSI.
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1; // No clock division.
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
while (1); // Error handling: Stay in an infinite loop if configuration fails.
}
// Configure the clock source and dividers for the system clock, AHB, and APB buses.
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; // Use HSI as the system clock source.
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1; // No division for system clock.
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1; // No division for AHB clock.
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1; // No division for APB1 clock.
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
while (1); // Error handling: Stay in an infinite loop if configuration fails.
}
}
static void MX_GPIO_Init(void) {
GPIO_InitTypeDef GPIO_InitStruct = {0}; // Structure to configure GPIO pins.
// Enable GPIOC and GPIOA clocks.
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
// Reset output pins PA0 and PA1 (LEDs OFF initially).
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0 | GPIO_PIN_1, GPIO_PIN_RESET);
// Configure GPIO pins PA0 and PA1 as output pins for LEDs.
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; // Select pins PA0 and PA1.
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; // Configure as push-pull output.
GPIO_InitStruct.Pull = GPIO_NOPULL; // No pull-up or pull-down resistors.
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; // Low speed to minimize power consumption.
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // Apply configuration to GPIOA.
// Configure GPIO pin PC7 as input pin for the button.
GPIO_InitStruct.Pin = GPIO_PIN_7; // Select pin PC7.
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // Configure as input.
GPIO_InitStruct.Pull = GPIO_PULLDOWN; // Enable pull-down resistor for stable LOW state.
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); // Apply configuration to GPIOC.
}