View Course Path

Seven segment interfacing with 8051 – Single and Quad module

There were probably several occasions when you looked up at the count down timer beside the traffic signal or the digital clock or even the displays at railway stations and airports and wondered what those displays are. Those displays are known as seven-segment displays. In this tutorial, we will learn about the seven-segment display, its constituent LEDs, how to turn it ON, and how to display digits by interfacing the seven-segment display with an 8051 microcontroller.

Components required

What is a seven-segment display?

seven-segment display

  • A seven-segment display is a combination of eight LEDs that are connected in such a way that each LED represents a particular segment of the display.
  • These segments are named as a, b, c, d, e, f, g, DP.
  • One side of these 8 LEDs is connected to the 8051 microcontrollers, to any of its I/O ports.
  • Whereas the other side of these LEDs is shorted and connected to a cathode or an anode.
  • There are two types of seven-segment displays based on the principle of operation.
    • Common Anode (CA) 7 Segment Display
    • Common Cathode (CC) 7 Segment Display

Seven-segment displays may use a liquid crystal display (LCD), a light-emitting diode (LED), an electrochromic display, or other light-generating techniques such as cold cathode gas discharge (Panaplex), etc. for each segment.

These displays are readily available in the market with various specifications like:

  • Maximum current input in the range of 10mA – 30mA.
  • Sizes: 1.2″, 1.5″, 1.8″, 2.3″, 4″, or 5″.
  • They are also available in the form of 4 digit seven segment displays (ATA8041AB) with four control pins which decide which digit to light up or even two digits like ACDA02-41SURKWA-F01.

Pin Diagram of the seven-segment LED

Pin diagram of 7 segment display

Pin Number Pin Name Description
1 e Controls the left bottom LED of the 7-segment display
2 d Controls the bottom-most LED of the 7-segment display
3 com Common pin connected to Ground/Vcc based on the type of display
4 c Controls the right bottom LED of the 7-segment display
5 DP Controls the decimal point LED of the 7-segment display
6 b Controls the top right LED of the 7-segment display
7 a Controls the topmost LED of the 7-segment display
8 com Common pin connected to Ground/Vcc based on the type of display
9 f Controls the top left LED of the 7-segment display
10 g Controls the middle LED of the 7-segment display

How does a seven-segment display work?

There are two main constructions of the seven-segment display modules. Let’s understand both of them.

Common Anode (CA) 7 Segment Display

In the common anode type of 7 segment display, the anodes of all the LEDs are joined together to VCC supply with a maximum of 10mA current. Whereas the cathodes of these LEDs are connected to the I/O ports of the microcontroller. We know that an LED turns on when forward biased and off when reverse biased. Now since the anode is connected to +5V to make the LED forward biased, the cathode should be at 0V, i.e., the LED turns ON when a low pulse from the microcontroller is given.

In summary, to switch on a common anode type’s segment, you’ve to write a “0” to its corresponding pin.

Common Anode 7 Segment display

Common Cathode (CC) 7 Segment Display

Even though the internal structure of both common anode and cathode appears the same, as the name suggests, in common cathode seven segment display one side, i.e., the cathode part of all the eight light-emitting diodes is shorted together and connected to the ground/GND. The other side i.e., the anode part, is connected to the microcontroller I/O pins. When we give a high pulse through the pins of the microcontroller, the LED turns ON. Hence, we can say that a common cathode type display is active HIGH.

In summary, to switch on a common cathode type’s segment, you’ve to write a “1” to its corresponding pin.

Common Cathode 7 Segment display

Seven segment display – Single Module

First, we connect port P2 of the 8051 to the seven-segment display such that P2.0 is connected to segment a, P2.1 is connected to b, … , P2.6 is connected to g, and P2.7 is connected to DP (decimal point).

Now to display a digit, say ‘0’ on the seven-segment display, we must turn on a, b, c, d, e, f, and turn off g. This operation can be done in two ways, either by creating a lookup table used for active low outputs (turns on when 0 is given) or creating a lookup table used for active high outputs (turns on when 1 is provided).

We’ll stick to the latter throughout this tutorial and complement it whenever we use the common anode 7-segment display.

Note: We will have two lookup tables that have values that are the complement of each other depending on whether we are using common anode/common cathode type seven-segment display. The table below is for the common cathode type. 

Let us take another example, to display the digit ‘5’, a, c, d, f, g, should be HIGH, and b, e should be LOW, which means we have to output to port 2 a value = 01101101B = 0x6DH while using the common cathode mode. Hence, we come up with this table to display every digit on a single-module common cathode seven segment display.

It’s fine to keep this table handy as the values won’t change for similar configurations. You don’t need to make the look-up table from scratch every time.
* P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0 *
Character CC g f e d c b a HEX
0 0 0 1 1 1 1 1 1 0x3F
1 0 0 0 0 0 1 1 0 0x06
2 0 1 0 1 1 0 1 1 0x5B
3 0 1 0 0 1 1 1 1 0x4F
4 0 1 1 0 0 1 1 0 0x66
5 0 1 1 0 1 1 0 1 0x6D
6 0 1 1 1 1 1 0 1 0x7D
7 0 0 0 0 0 1 1 1 0x07
8 0 1 1 1 1 1 1 1 0x7F
9 0 1 1 0 1 1 1 1 0x6F

Circuit diagram to interface seven segment display with 8051 (single module)

Circuit diagram to interface 7 segment display to 8051 (1 Module)

C code to interface seven segment display (0-9) – Single module

// Code to interface 7 Segment Display 1 module withh 8051 

#include<reg51.h>
void msdelay(unsigned int time)  // Function for creating delay in milliseconds.
{
    unsigned i,j ;
    for(i=0;i<time;i++)    
    for(j=0;j<1275;j++);
}

void main()
{
    unsigned char to_disp[]={0x3F,0x06,0x5B,0x4F,0x66,        // Array for hex values (0-F) for
			     0x6D,0x7D,0x07,0x7F,0x6F}        // common anode 7 segment
    int k;
    while(1)
    {
        for(k=0;k<10;k++)
        {
         P2=to_disp[k]; 
         msdelay(100);
        }
    }
}

Assembly language program to interface seven segment display (0-9) – Single Module

ORG 4000H
DB 3FH, 06H, 5BH, 4FH, 66H, 6DH, 7DH, 07H, 7FH, 6FH, 0
; Lookup table for digits 0 to 9

ORG 0000H
main:   MOV DPTR, #4000H
repeat: CLR A
        MOVC A, @A+DPTR       ; Copy data from external location to accumulator
        MOV P2, A             ; Move the pattern of the digit into port P2
        ACALL delay           ; Call a delay to so that the transition is visible
        INC DPTR              ; Point to the next pattern
        CJNE A, 0, repeat     ; Repeat till 0 (Stop bit) is received
        SJMP main             ; Run this forever till externally stopped

; generate a decent enough delay between transitions
delay: 
        MOV R0, #08H
LP2:    MOV R1, #0FFH
LP1:    MOV R2, #0FFH
LP3:    DJNZ R2, LP3 
        DJNZ R1, LP1
        DJNZ R0, LP2
        RET
END

Seven segment display – Four Modules

Now to interface more than one module at once with the 8051 microcontroller, we’ll have to come up with a selector logic. Hence, we connect a 2:4 decoder, whose inputs are given by pin P3.3 and pin P3.4. Based on the status of these two pins, we come up with four modes that correspond to the display selected.

Mode P3.4 P3.3 Active Module
1 0 0 1
2 0 1 2
3 1 0 3
4 1 1 4

Apart from the decoder or a demultiplexer circuit, we also need an NPN bipolar junction transistor whose one end is connected to the COM pin of the LED and the second pin to VCC.

Here, we’re using a common anode type of display; hence we’ll have to complement the data taken from the lookup table as we’ve already discussed that the common anode display is active low operated (turns ON when a low pulse is provided). And the table above is for common cathode type.

Another thing to look for is: since we are selecting the display one at a time, it is possible that all the numbers do not appear simultaneously on the four modules.

Now, as the refresh rate of our eyes is roughly 100ms, we have to provide a delay between the transitions that is less than 100ms. Fortunately, that’s pretty easy as the 8051 microcontroller’s clock frequency is in MHz, allowing us to create delays of the magnitude of microseconds to create an illusion that all the four modules are switched on at the same time.

Circuit diagram to interface seven segment display with 8051 (four modules)

Circuit diagram to interface 7 segment display to 8051 (4 Modules)

C Program to interface quad seven segment displays to 8051

// Code for 7 Segment Display 4 module

#include<reg51.h>
sbit sw1=P3^3;
sbit sw2=P3^4;

void msdelay(unsigned int time) // Function for creating delay in milliseconds.
{
    unsigned i,j ;
    for(i=0;i<time;i++) 
    for(j=0;j<1275;j++);
}

void main()
{
    unsigned char to_disp[]={0x06,0x5B,0x4F,0x66}; 
    while(1)
    {
        sw1 = 0; //Mode 1
        sw2 = 0;
        P2 = ~to_disp[0]; //Complement the pattern (as 7SEG disp is common anode) 
        msdelay(1);

        sw1 = 1; //Mode 2
        sw2 = 0;
        P2 = ~to_disp[1]; 
        msdelay(1);

        sw1 = 0; //Mode 3
        sw2 = 1;
        P2 = ~to_disp[2]; 
        msdelay(1);

        sw1 = 1; //Mode 4
        sw2 = 1;
        P2 = ~to_disp[3]; 
        msdelay(1);
    }
}

Assembly language program to interface quad seven segment displays to 8051

ORG 4000H
DB 06H, 5BH, 4FH, 66H

ORG 0000H
main:  MOV DPTR, #4000H
       MOV P3, #00H ; clear the port 3 to use it as output port
       CLR P3.3 ; Mode 1 
       CLR P3.4 ; 00
       CLR A
       MOVC A, @A+DPTR
       CPL A
       MOV P2, A ; P2 = 00000110b = pattern of digit ‘1’
       ACALL delay
       INC DPTR

       SETB P3.3 ; Mode 2
       CLR P3.4 ; 01
       CLR A
       MOVC A, @A+DPTR
       CPL A
       MOV P2, A ; P2 = 01011011b = pattern of digit ‘2’
       ACALL delay
       INC DPTR

       CLR P3.3 ; Mode 3
       SETB P3.4 ; 10
       CLR A
       MOVC A, @A+DPTR
       CPL A
       MOV P2, A ; P2 = 01001111b = pattern of digit ‘3’
       ACALL delay
       INC DPTR

       SETB P3.3 ; Mode 4
       SETB P3.3 ; 11
       CLR A
       MOVC A, @A+DPTR
       CPL A
       MOV P2, A ; P2 = 01100110b = pattern of digit ‘4’
       ACALL delay

       SJMP main ; Run this program forever

; To generate a very small delay so that we won’t be able
; to notice the transition from one module to another
delay: 
       MOV R0, #01H
LP2:   MOV R1, #010H
LP1:   MOV R2, #010H
LP3:   DJNZ R2, LP3 
       DJNZ R1, LP1 
       DJNZ R0, LP2
       RET
END

In summary, in this post, we got familiar with the way I/O ports of 8051 can be used for different applications. We will use these ports for various other projects in this 8051 course.

The seven-segment display can be used in any application where you need to display some numbers. Some examples are a water level indicator or a temperature indicator. Also, you’ve learned how the LED works and what are its different configurations.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.