Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

🔬 Microprocessors & Microcontrollers — Lab 3

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

📌 Overview

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.

Tech Stack

  • MCU: STM32F103C6 (ARM Cortex-M3)
  • IDE: STM32CubeIDE (HAL Library)
  • Simulation: Proteus 8
  • Language: C
  • Key Concepts: FSM, software debouncing, modular architecture, software timers

📂 Repository Structure

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)

🧪 System Architecture

Operating Modes

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 Functions

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

Traffic Light FSM (Normal Mode)

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)

📝 Module Descriptions

timer.c — Software Timer Engine

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 transitions
  • timer_1s — drives the countdown display (1-second tick)

input_reading.c — Button Debouncing

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)

input_processing.c — FSM Input Handler

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

display7seg.c — Display & Traffic Control

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

⚙️ How to Run

Proteus Simulation

  1. Open FULL_EXERCISE/Lab_3.pdsprj in Proteus 8+
  2. Load Lab_3.hex into the STM32 component
  3. Click Run — use the 3 buttons to interact

Flash to Hardware

  1. Create STM32F103C6 project in STM32CubeIDE
  2. Copy Lab3.c as main.c and all Core/ files into the project
  3. Configure: TIM2 (10ms interrupt), GPIO outputs (LEDs, 7SEG), GPIO inputs (buttons with pull-up)
  4. Build and flash via ST-Link

📝 Default Configuration

Parameter Default Range
RED duration 5 seconds 1–99
AMBER duration 2 seconds 1–99
GREEN duration 3 seconds 1–99

📄 Report

The full lab report (PDF) with schematics, state diagrams, and source code explanations is available in the course submission.


📜 License

This project is developed for educational purposes as part of the Microprocessors & Microcontrollers course at HCMUT.