Fork me on GitHub

Project Notes

#399 AT89C2051 LED Chaser

Run an LED chaser with an AT89C2051 using interrupts for timing; C source compiled with SDCC on MacOSX.

Build

Here’s a quick demo..

clip

Notes

This project is a simple extension of blinking LEDs with the 8051 to explore the use of timers and interrupts.

The AT89C2051 has two 16-bit timer/counters. The timers are highly configurable. I’m using Timer0 mode 3, which splits it into two 8 bit counters, and conigure to fire interrupt 1 on overflow.

Register usage:

  • TMOD sets the mode and gating for Timer0 and Timer1. In this case, Timer1 is ignored, and Timer0 set for:
    • Mode 3: uses TL0 as an 8-bit timer/counter.
    • Timer0 counts based on the divided-down system clock (clock / 12)
    • Timer0 is enabled when the TR0 bit is set
  • TL0 is the low 8 bits of the Timer0 register
  • ET0=1 enables Timer0 interrupt.
  • EA=1 enables global interrupt.
  • TR0=1 sets Timer0 running.
  • TF0 is the Timer0 flag set every time an overflow occurs. Flags are cleared before calling the timer interrupt routine.

Interrupt Service Routine

The interrupt service routine is declared with the non-standard __interrupt keyword, allowing the compiler/linker to correctly vector this function for interrupt 1:

void timer0_isr(void) __interrupt (1) {
  ...
}

The interrupt routine is setup in this program to count interrupts and trigger an LED state change at a frequency of 2Hz.

Driving LEDs with Port 1

Eight LEDs are used by this program in a simple “chase” sequence, which maps nicely to the 8 bits or Port 1. All the Port 1 output buffers can sink 20 mA, more than enough for the LEDs I’m using.

Only pins P1.2 to P1.7 provide internal pull-ups and can source current. P1.0 and P1.1 require external pull-ups for this, so the most convenient arrangement for this design is low-side control of the LEDs i.e. active-low logic.

Code and Compilation

This is a single-file program: LedChaser.c - with a Makefile to build with SDCC. I’m compiling this on MacOSX.

$ make
sdcc -mmcs51 --code-size 2048 LedChaser.c -o LedChaser.ihx
packihx LedChaser.ihx > LedChaser.hex
packihx: read 15 lines, wrote 19: OK.

Programming

LEAP#394 AT89C2051 Programmer and at89overlord

$ source ../Programmer/venv/bin/activate
$ at89overlord -p /dev/tty.usbmodem14531 -f LedChaser.hex
# Initializing the programmer...
# Initialized!
# Confirming chip ID...
# Confirmed!
# Erasing flash...
# Done!
# Writing flash...
# Done!
# Verifying...
# Done!

Construction

Breadboard

Schematic

Build

Credits and References

About LEAP#399 8051LED
Project Source on GitHub Project Gallery Return to the LEAP Catalog

This page is a web-friendly rendering of my project notes shared in the LEAP GitHub repository.

LEAP is just my personal collection of projects. Two main themes have emerged in recent years, sometimes combined:

  • electronics - usually involving an Arduino or other microprocessor in one way or another. Some are full-blown projects, while many are trivial breadboard experiments, intended to learn and explore something interesting
  • scale modelling - I caught the bug after deciding to build a Harrier during covid to demonstrate an electronic jet engine simulation. Let the fun begin..
To be honest, I haven't quite figured out if these two interests belong in the same GitHub repo or not. But for now - they are all here!

Projects are often inspired by things found wild on the net, or ideas from the many great electronics and scale modelling podcasts and YouTube channels. Feel free to borrow liberally, and if you spot any issues do let me know (or send a PR!). See the individual projects for credits where due.