STM32F103C6 — Finite State Machine, Button Input & Modular Traffic Light System
| Student | Dang Thai Khang — 2352464 |
| Course | C03010 — CC02 |
| Institution | Ho Chi Minh University of Technology (HCMUT) |
| Lecturer | Phan Van Sy |
| Semester | 2025 |
This lab implements a configurable 2-way traffic light system using a Finite State Machine (FSM) design pattern on the STM32F103C6. The project features modular code architecture with separate source files for input handling, display, timing, and processing logic. Users can adjust traffic light durations in real-time via 3 push buttons.
- MCU: STM32F103C6 (ARM Cortex-M3)
- IDE: STM32CubeIDE (HAL Library)
- Simulation: Proteus 8
- Language: C
- Key Concepts: FSM, software debouncing, modular architecture, software timers
VXL_VDK_Lab_3_2352464/
└── FULL_EXERCISE/
├── Lab3.c # Main program (entry point, ISR, GPIO/TIM init)
├── Lab_3.hex # Compiled firmware for Proteus
├── Lab_3.pdsprj # Proteus schematic & simulation
└── Core/
├── Inc/
│ ├── display7seg.h # Display & traffic light function headers
│ ├── input_processing.h # FSM input processing header
│ ├── input_reading.h # Button reading & debounce header
│ └── timer.h # Software timer header
└── Src/
├── display7seg.c # 7SEG multiplexing, traffic light FSM, LED blink
├── input_processing.c # Button FSM: mode switching & duration adjustment
├── input_reading.c # Hardware debouncing with double-buffer
└── timer.c # Software timer (blink, traffic, 1-second)
The system has 4 modes, cycled by pressing Button 1:
| Mode | Name | Display (7SEG) | LEDs | Description |
|---|---|---|---|---|
| 1 | Normal | Countdown timers for both directions | Traffic sequence | Normal traffic light operation |
| 2 | Set RED | 02 XX (XX = duration) |
RED blinks | Adjust RED light duration |
| 3 | Set AMBER | 03 XX |
YELLOW blinks | Adjust AMBER light duration |
| 4 | Set GREEN | 04 XX |
GREEN blinks | Adjust GREEN light duration |
| Button | Function |
|---|---|
| BTN 1 (PB7) | Cycle through modes: Normal → Set RED → Set AMBER → Set GREEN → Normal |
| BTN 2 (PB8) | Increment the selected duration (1–99 seconds) |
| BTN 3 (PB9) | Confirm and save the new duration |
State 0: Direction 1 = RED, Direction 2 = GREEN
│ (green_duration seconds)
▼
State 1: Direction 1 = RED, Direction 2 = YELLOW
│ (amber_duration seconds)
▼
State 2: Direction 1 = GREEN, Direction 2 = RED
│ (green_duration seconds)
▼
State 3: Direction 1 = YELLOW, Direction 2 = RED
│ (amber_duration seconds)
▼
(back to State 0)
Provides 3 independent countdown timers, all decremented in the TIM2 ISR (10ms tick):
timer_blink— controls LED blink rate (500ms)timer_traffic— controls traffic phase transitionstimer_1s— drives the countdown display (1-second tick)
Implements a double-buffer debounce algorithm:
- Two consecutive samples must agree before a button state is accepted
- Supports long-press detection (auto-increment after 1 second hold)
Processes debounced button events:
- BTN1: edge-detected mode transitions
- BTN2: increment temp duration values (with 1–99 wraparound)
- BTN3: commit temp values to active duration variables
Handles both the 7-segment display and traffic light LEDs:
- 4-digit multiplexed 7SEG (EN0–EN3 on PA6–PA9)
- Traffic light state machine with configurable durations
- Mode-dependent LED blinking patterns
- Open
FULL_EXERCISE/Lab_3.pdsprjin Proteus 8+ - Load
Lab_3.hexinto the STM32 component - Click Run — use the 3 buttons to interact
- Create STM32F103C6 project in STM32CubeIDE
- Copy
Lab3.casmain.cand allCore/files into the project - Configure: TIM2 (10ms interrupt), GPIO outputs (LEDs, 7SEG), GPIO inputs (buttons with pull-up)
- Build and flash via ST-Link
| Parameter | Default | Range |
|---|---|---|
| RED duration | 5 seconds | 1–99 |
| AMBER duration | 2 seconds | 1–99 |
| GREEN duration | 3 seconds | 1–99 |
The full lab report (PDF) with schematics, state diagrams, and source code explanations is available in the course submission.
This project is developed for educational purposes as part of the Microprocessors & Microcontrollers course at HCMUT.