Class 11 – Digital Clock Using Interrupt

June 12, 2025

Hi Buddies!
In our last class, we built a digital clock without using interrupts — and yes, it worked well!
But you might have noticed a small flicker when you pressed a button. Today, let’s solve that using interrupts. But before that, let’s understand what interrupts are.

Blocking Code Method

In earlier classes, especially when blinking LEDs, we used a delay inside the main loop. That delay blocks the system — during this time, the microcontroller doesn’t do anything else.

This method is called the blocking method. The microcontroller is “stuck” in the delay and can’t respond to anything else until it finishes.

This is why our display flickered when buttons were pressed in the last version. Let’s fix it now!

What is an Interrupt?

Instead of waiting and blocking, we want the microcontroller to do its usual work. But when something important (like a timer tick or button press) happens, the system should pause its normal job, handle the event, and then resume.

That’s exactly what an interrupt does.

Each interrupt has a special vector address in memory where its code is stored. When an interrupt occurs, the microcontroller jumps to that address, runs the code, and returns to the main program.

Interrupts in 8051

The 8051 has five types of interrupts:

  1. External Interrupt 0 (INT0)
  2. Timer 0 Interrupt (TIMER0)
  3. External Interrupt 1 (INT1)
  4. Timer 1 Interrupt (TIMER1)
  5. Serial Interrupt (UART)

These interrupts have default priority:

  • INT0 (Highest)
  • TIMER0
  • INT1
  • TIMER1
  • SERIAL (Lowest)

If two interrupts occur at the same time, the microcontroller handles the higher-priority one first.

What is a Vector Address?

Each interrupt has a fixed vector address:

InterruptVector AddressISR Number
INT00003H0
TIMER0000BH1
INT10013H2
TIMER1001BH3
SERIAL0023H4

In SDCC, you use the __interrupt(n) keyword to define an interrupt function.

You don’t need to handle jump/return manually; the compiler takes care of it.

Enabling Interrupts

To use interrupts, you must:

  1. Enable the specific interrupt (like ET0 for Timer 0).
  2. Enable the global interrupt flag (EA = 1;).

Timer 0 for 4ms Interrupt

We want to refresh our display every 4ms using Timer 0 in Mode 1 (16-bit).
Here’s the math:

  • 1 tick ≈ 1.085 μs
  • 4 ms = 4000 μs → 4000 / 1.085 ≈ 3686 ticks
  • 65536 – 3686 = 61850 = 0xF1DA

So we preload TH0 = 0xF1 and TL0 = 0xDA.

Final Clock Code (Using Timer Interrupt)

Summary

  • We moved the display scanning to a timer interrupt.
  • This removes flicker and allows other logic (RTC read, key press) to run smoothly.
  • This is the final version of our 7-segment clock project using interrupts.

If you want the previous non-interrupt version, check out Class 10.
Next class, we’ll go deeper into optimizing or customizing interrupt priorities!

Happy Building, Buddies!
Let’s keep learning and creating!

Leave a reply
Class 10 – Digital Clock Project using 7 Segment

Leave Your Reply