7 segment Interfacing with 8051 Microcontroller

7 Segment Interface with 8051 Microcontroller

7 Segments are commonly used display devices which is capable of displaying numbers and some alphabets. It is more common in Embedded design applications where we needs to display some count or increment numerical values. This tutorial is focused to teach the interfacing of 7 segment interfacing with 8051 microcontroller and how to program it to display numerical or characters.


Common anode and Common cathode Display

There are two types of 7 segment  one is Common Cathode and another one is common Anode. Both of them vary with their mode of connections and programming. As we all know that 7 segment is made of series of LED’s  aligned together to perform the function of display.

Connection configuration of 7 segments

There is one common difference between these two segments, that is they both differ in power sourcing of LED’s in it. Common Anode segments are draw current directly from the power source using the Vcc pin and sink it using Microcontroller pins. But Common Cathode LED’s are sourced by pins of the microcontroller and sink by means of GND pin.


Digit formation in 7 Segment

In 7 segment the display of digits is done by means of turning on or turning off the LED’s in respective places. For example if we need to display the digit ‘0’ we need to enable LED’s  “a,b,c,d,e,f” , that is applying logic 1 or sourcing from microcontroller in case of Common cathode and applying logic 0 or sinking through pins in case of common anode. So for connecting 7 segment from P2.0 to a ………P2.7 to dp of 7 segment our hex code will be

0 – 0011 1111 – 0x3F

1 – 0000 0110 – 0x06

2 – 0101 1011 – 0x5B

Like this we can program to display any digits of our desire by feeding hex code to the ports of the Microcontroller.


The below code describes how to display 0 to 9 digits in the 7 segments with some specific time intervals between.

void delay(void);
unsigned char array[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //Array for digits 0 to 9
void main()                       //Main Program
      unsigned int i;
        P2=array[i];             //Sending hex values one by one
void delay(void)              //Delay Function
    unsigned int s,k;

For 7 Segment common anode displays you need to give logic 0 to the LED segments in order to light up.


  • Modify the line 12 in the above program as P2=~array[i]; to make it work for common anode segment.
  • “~” sign will complement all the values to apply logic 0 at desired pins of the 7 segment.
The following two tabs change content below.
Frank Donald

Frank Donald

Junior Network Engineer at Tata Communications
Frank Donald is an Electronics and Communication Engineer who loves building stuff in his free time. Video games, blogging and programming are the things he loves most

Ask your query

1 Comment on "7 segment Interfacing with 8051 Microcontroller"

newest oldest most voted
Notify of

sir in this ur wring loop for(i=0;i<=10;i++)
here we r using 0 to 9 its mean total 10
from ur code accepts 11 numbers 7 segment accepts only 0 to 9 in this segment cant shows 10
sir plz code modification loop for(i=0;i<10;i++) or for(i=0;i<=9;i++)