Home / 8051 Tutorial / Interfacing Seven Segment Displays with 8051
Interfacing seven segment displays with 8051

Interfacing Seven Segment Displays with 8051

In my previous post, I showed you how to set up an 8051 microcontroller for programming using C. This tutorial will be about how to interface with seven-segment displays.

Review on Seven Segment Displays

A seven-segment display consists of LEDs in parallel. This way, each LED or "segment" can be turned on or off. Recall that an LED emits light when the anode voltage is more positive than the cathode voltage.

In a common cathode seven-segment display, each anode of the LEDs is accessible while all LED cathodes connect into one pin. The anode pins are given labels A to G. The opposite is true for a common anode display. Here, we will limit ourselves to common cathode displays.

Interface a Seven Segment Display to an Arduino - Projects

To display a number in a seven-segment display, the correct LEDs should light up. Hence if each anode connects to a pin in the 8051 microcontroller, digits 0 to 9 corresponds to an 8-bit value of a port.

7 seg to 8-bit value

Here’s a table showing all 8-bit values for digits 0 to 9, assuming segment A wires to the port’s LSB.

Based on the table above, we create a function that accepts a digit as a parameter and returns the corresponding 8-bit value.

The function returns an integer and so we can pass it directly to any port.

8051 Code for Interfacing with One Display

The example code below shows the digits 0 to 9 in succession with a second interval in between.

8051 with single seven segment display

8051 Code for Interfacing with Multiple Displays

We can use multiple seven-segment displays by connecting the common cathode pin to a microcontroller pin. This way, we can multiplex the displays using the common cathode pin as the switch. The switching should be fast enough to trick our eyes that all displays are on simultaneously instead of alternating. This is also called persistence of vision.

Here is a code that uses two seven-segment displays to count from 00 to 99.

8051 with two seven segment displays

The SWITCH_SPEED and COUNT_SPEED for the code above should be chosen after testing the actual physical circuit. SWITCH_SPEED is the time interval between turning on one display and then turning on the other. COUNT_SPEED is the speed of the counting.

For questions regarding this tutorial, please drop a comment below!

Leave a Reply

Your email address will not be published. Required fields are marked *