8051 and 7 segment displays

Its fairly straightforward to connect  a 7 segment display to your 8051 processor, in this example we will do this and show a code example which cycles through the numbers 0 to 9.

First of all a 7 segment display is basically a collection of LED’s, 1 LED per segment and a decimal point.

Here is the pinout of a fairly typical display, you should always refer to the datasheet of your display to verify the pinout.

7 segment
7 segment

 

7 segment display
7 segment display

Displaying a number

Displaying a number on a 7 segment display is as simple as sending a logic low or high (depending on the display type) to a sequence of pin(s) to light up the segment. The table below shows the segments that would be required to be enabled for a common cathode, in that case a 1 will light a segment. Invert the numbers in columns a – g if you have a common anode 7 segment display

So to display a 1 we need to activate segments b and c.

Digit a b c d e f g
0 1 1 1 1 1 1 0
1 0 1 1 0 0 0 0
2 1 1 0 1 1 0 1
3 1 1 1 1 0 0 1
4 0 1 1 0 0 1 1
5 1 0 1 1 0 1 1
6 1 0 1 1 1 1 1
7 1 1 1 0 0 0 0
8 1 1 1 1 1 1 1
9 1 1 1 1 0 1 1

Schematic

This is the basic 8051 connected up to the 7 segment display, other circuitry such as crystal and capacitors and power is not shown here.

The display is a common anode type

 

8051 and 7 segment
8051 and 7 segment

Code

This code is for a common anode 7 segment display, the code was written using the mikroC PRO for 8051 compiler

void main()
{
do
{

P0 = 0xFF;
//activate segments 1 at a time. Number 1 first
P0 = 0xF9;
Delay_ms(1000);
//number 2
P0 = 0xA4;
Delay_ms(1000);
//number 3
P0 = 0xB0;
Delay_ms(1000);
//number 4
P0 = 0x99;
Delay_ms(1000);
//number 5
P0 = 0x92;
Delay_ms(1000);
//number 6
P0 = 0x82;
Delay_ms(1000);
//number 7
P0 = 0xF8;
Delay_ms(1000);
//number 8
P0 = 0x80;
Delay_ms(1000);
//number 9
P0 = 0x98;
Delay_ms(1000);
//number 0
P0 = 0xC0;
Delay_ms(1000);
P0 = 0xff;

} while(1); // Endless loop
}