#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stm32l0xx_hal.h>
// Define ports and pins
#define LED_PORT GPIOB
#define RED_PIN GPIO_PIN_3 // D13
#define YELLOW_PIN GPIO_PIN_4 // D12
#define GREEN_PIN GPIO_PIN_5 // D11
#define LED_PORT_CLK_ENABLE __HAL_RCC_GPIOB_CLK_ENABLE
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
void HAL_SYSTICK_Callback(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
while (1)
{
// Nothing here, control is interrupt-driven
}
return 0;
}
// ============================================================
// GPIO Initialization
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
LED_PORT_CLK_ENABLE();
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
// Configure all three pins
GPIO_InitStruct.Pin = RED_PIN | YELLOW_PIN | GREEN_PIN;
HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
}
// ============================================================
// SysTick interrupt handler
void HAL_SYSTICK_Callback(void)
{
static uint32_t counter = 0;
static uint8_t state = 0;
counter++;
if (counter >= 10000) // 1000 ms = 1 second
{
counter = 0;
// Turn off all LEDs first
HAL_GPIO_WritePin(LED_PORT, RED_PIN | YELLOW_PIN | GREEN_PIN, GPIO_PIN_RESET);
// Control LED state machine
switch (state)
{
case 0: // RED
HAL_GPIO_WritePin(LED_PORT, RED_PIN, GPIO_PIN_SET);
state = 1;
break;
case 1: // GREEN
HAL_GPIO_WritePin(LED_PORT, GREEN_PIN, GPIO_PIN_SET);
state = 2;
break;
case 2: // YELLOW
HAL_GPIO_WritePin(LED_PORT, YELLOW_PIN, GPIO_PIN_SET);
state = 0;
break;
}
}
}
// SystemClock_Config remains unchanged (from your image)