#include "stm32l0xx_hal.h"
// Define LED pin and port
#define LED_GPIO_PORT GPIOB
#define RED_PIN GPIO_PIN_3
#define YELLOW_PIN GPIO_PIN_4
#define GREEN_PIN GPIO_PIN_5
#define BUTTON GPIO_PIN_6
#define LED_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
// Function prototypes
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
void HAL_SYSTICK_Callback(void);
// Main program
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
// Infinite loop (LED blinking is interrupt-driven)
while (1) {
// Do nothing, LED toggling handled by SysTick interrupt
}
}
// SysTick interrupt callback (every 1 ms)
void HAL_SYSTICK_Callback(void) {
static uint32_t counter = 0;
static uint8_t state = 0;
counter++;
if (counter >= 1450) { // 1000 ms = 1 second
counter = 0;
// Turn off all LEDs first
HAL_GPIO_WritePin(GPIOB, RED_PIN | YELLOW_PIN | GREEN_PIN, GPIO_PIN_RESET);
if (state == 0){
HAL_GPIO_WritePin(GPIOB, RED_PIN, GPIO_PIN_RESET);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
state = 1;
}
else if (state == 1){
HAL_GPIO_WritePin(GPIOB, GREEN_PIN, GPIO_PIN_RESET);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_4);
state = 3;
}
else{
HAL_GPIO_WritePin(GPIOB, YELLOW_PIN, GPIO_PIN_RESET);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);
state = 0;
}
}
}
// Initialize GPIO pin for LED
static void MX_GPIO_Init(void) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Enable clock for GPIO port
LED_GPIO_CLK_ENABLE();
// Configure GPIO pin Output Level
HAL_GPIO_WritePin(GPIOB, RED_PIN | YELLOW_PIN | GREEN_PIN, GPIO_PIN_RESET);
// Configure GPIO pin : PB3,4,5 as output
GPIO_InitStruct.Pin = RED_PIN | YELLOW_PIN | GREEN_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);
}
// Configure system clock to 32 MHz (internal oscillator + PLL)
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
// Enable power controller and set voltage scaling
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
// Configure PLL to generate 32 MHz from internal 16 MHz HSI oscillator
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
// Configure clock dividers
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
}
PB_3
PB_5
PB_4