Lab 2: Timer Interrupt & LED Scanning
Overview​
Timers let us run code at precise intervals without blocking. This lab configures a 10 ms Timer 2 interrupt and uses it to scan multiple 7-segment displays, build a digital clock, and create a non-blocking software timer. An optional 8×8 LED matrix extends the idea.
Objectives​
- Configure a Timer 2 interrupt firing every 10 ms.
- Multiplex (scan) several 7-segment displays so they appear lit simultaneously.
- Replace blocking
HAL_Delaywith a software timer driven by the interrupt.
Timer setup​
With an 8 MHz clock, set Prescaler = 7999 and Counter Period = 9:
8 MHz / (7999+1) = 1000 Hz, divided by 10 → 100 Hz → a 10 ms interrupt. Enable the TIM2
global interrupt (NVIC), start it with HAL_TIM_Base_Start_IT(&htim2), and put your periodic
code in the callback.
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == TIM2) {
// runs every 10 ms
}
}
LEDs scanned faster than ~30 Hz appear continuously on; on Proteus a low rate (e.g. 1 Hz) is used because the simulator can't run at full speed.
Exercises​
- Display "1" and "2" on two 7-segments, switching every 0.5 s.
- Extend to four 7-segments + two DOT LEDs (PA4); blink the DOT every second.
- Implement
update7SEG(int index)driven by a 4-element buffer. - Set the 4-digit scanning frequency to 1 Hz; keep the DOT blinking.
- A digital clock (hour/minute) via
updateClockBuffer(). - Create a software timer (
setTimer0,timer_run,timer0_flag) to removeHAL_Delay. - Rewrite the clock from Ex. 5 using the software timer; move the DOT to
main. - Move
update7SEG()tomain; the interrupt only services software timers. - (Extra) Add an 8×8 LED matrix (MATRIX-8X8-RED + ULN2803); show character "A".
- Animate the matrix (e.g. scroll the character left).
Report & Submission​
Present the Proteus schematic and the relevant source (callback / main). For the software
timer, explain what happens for setTimer0(1), setTimer0(10) and a missing initial call.