Skip to main content

Seven Segment Display Interfacing with 8051 Microcontroller

Seven Segment Display Interfacing with 8051 Microcontroller

Seven segment displays are important display units in Electronics and widely used to display numbers from 0 to 9. It can also display some character alphabets like A,B,C,H,F,E etc. In this tutorial, we are going to learn how to interface a segment with 8051 microcontroller. We are using AT89S52 microcontroller.
Before interfacing, we should learn about 7 segment display. Itā€™s the simplest unit to display numbers and characters. It just consists 8 LEDs, each LED used to illuminate one segment of unit and the 8thLED used to illuminate DOT in 7 segment display. We can refer each segment as a LINE, as we can see there are 7 lines in the unit, which are used to display a number/character. We can refer each line/segment "a,b,c,d,e,f,g" and for dot character we will use "h". There are 10 pins, in which 8 pins are used to refer a,b,c,d,e,f,g and h/dp, the two middle pins are common anode/cathode of all he LEDs. These common anode/cathode are internally shorted so we need to connect only one COM pin.





There are two types of 7 segment displays: Common Anode and Common Cathode:
Common Anode: In this all the Negative terminals (cathode) of all the 8 LEDs are connected together (see diagram below), named as COM. And all the positive terminals are left alone.
Common Cathode: In this all the positive terminals (Anodes) of all the 8 LEDs are connected together, named as COM. And all the negative thermals are left alone.

Circuit Diagram and Working Expalation

Here we are using common anode type of 7 segment because we need to connect LEDs in reverse. As we know that microcontroller doesnā€™t provide enough power to glow the LED so we need to connect LEDā€™s cathode to microcontroller pin and LEDā€™s anode to power supply. You can understand this negative logic concept in this article ā€œLED Interfacing with 8051 Microcontrollerā€. You should also read this article to understand the basic connection of microcontroller like crystal and reset circuitry.


We have connected a,b,c,d,e,f,g,h to pins 2.0 to 2.7 means we are connecting 7 segment to port 2 of microcontroller. Now suppose we want to display 0, then we need to glow all the LEDs except LED which belongs to line ā€œgā€ (see diagram above), so pins 2.0 to 2.6 should be at 0 (should be 0 to TURN ON the LED as per negative logic) and pin 2.7 and 2.8 should be at 1 (should be 1 to TURN OFF the LED as per negative logic). So the LEDs connected to pins 2.0 to 2.6 (a,b,c,d,e,f) will be ON and LEDs connected to 2.7 and 2.8 (g and h) will be OFF, that will create a ā€œ0ā€ in 7 segment. So we need bit pattern 11000000 (Pin 8 is the highest bit so starting from P2.7 to P2.0), and the HEX code for binary 11000000 is ā€œC0ā€. Similarly we can calculate for all the digits. Here we should note that we are keeping ā€œdot/hā€ always OFF, so we need to give LOGIC ā€œ1ā€ to it every time. A table has been given below for all the numbers while using Common Anode 7 segment.
Digit to Display
h g f e d c b a
Hex code
0
11000000
C0
1
11111001
F9
2
10100100
A4
3
10110000
B0
4
10011001
99
5
10010010
92
6
10000010
82
7
11111000
F8
8
10000000
80
9
10010000
90

Code Explanation

We have created ms_delay function to provide the delay in milliseconds, this delay is usually provided in any microcontroller program so that microcontroller can complete its internal operation.

Then we have created an array of the hex codes for 0 to 9 (see table above), and finally we have sent the hex codes to the port 2, which is connected to common anode 7 segment. So in this way the numbers are shown on the 7 segment display.

Now we have only 4 ports in microcontroller and what if we want to show the data in more than four 7 segments?? To solve this problem, Multiplexing technique comes into picture. We need to multiplex multiple 7 segment units. Also read interfacing 7 segment display with AVR microcontroller.

Code: 
#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 no_code[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //Array for hex values (0-9) for common anode 7 segment
    int k;
    while(1)
    {
        for(k=0;k<10;k++)
        {
         P2=no_code[k]; 
         msdelay(100);
        }
    }
}





Comments

Popular posts from this blog

PUNCHING MACHINE

ACCIDENT AVOIDING SYSTEM FOR PUNCHING MACHINE SYNOPSIS The aim of our project is to take a system-wide approach to preventing the machine accident. The system includes not just the machine and the operator; but rather, it includes everything from the initial design of the machine to the training of everyone that is responsible for any aspect of it, to the documentation of all changes, to regular safety audits and a finally a corporate culture of safety-first. Design is the part of a machine's life where the greatest impact can be made in relation to avoiding accidents. The designer should ensure that the machine is safe to set up and operate, safe to install, safe to maintain, safe to repair, and safe to decommission. Although safe operation is usually at the forefront of a designer's mind, safe maintenance and repair should also be a high priority. Around 50% of fatal accidents involving industrial equipment are associated with maintenance activities, and design...

garbage monitoring using arduino code with gsm

#include <SoftwareSerial.h> #include <LiquidCrystal.h> //LiquidCrystal lcd(7, 6, 5, 4, 3, 2); LiquidCrystal lcd(13, 12, 11, 10, 9, 8); SoftwareSerial mySerial(0,1); #define trigPin 2 #define echoPin 3 #define PIR_sensor 4 #define m11 5 #define m12 6 void setup() {    lcd.begin(16, 2);    lcd.print("    Garbage    ");    Serial.println("garbage ");   lcd.setCursor(0,1);   lcd.print("   Open Close    ");   Serial.println(" open close");   delay(3000);   lcd.clear();   //lcd.print(" ");   delay(2000); mySerial.begin(9600); // Setting the baud rate of GSM Module Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT);  pinMode(m11, OUTPUT);   pinMode(m12, OUTPUT);   } void loop() {  readPIR();  pingDist();  SendMessage(); } void pingDist ()   {     long duration, distance;...