There are 2 types of 7 segment displays , Common Anode & Common Cathode.In this post we shall see how to interface a Common Cathode display with 89S52 Microcontroller.

As the name indicates the cathode points of all segments are made common to GND .To make a segment Glow , the corresponding segment pin is made HIGH.

 

                Image 3

The PORT pins P2.0 to P2.6  are  connected to segments a through  g. The dot pin can be left out.

A NPN transistor is used at the common cathode point . When the base of transistor is made HIGH , it conducts & applies Ground to the Common Cathode point.The transistor is controlled by port pin P1.0

Image 1

 

Following table explains how to arrive at the HEX value for displaying each Digit.

For e.g , to display digit 1 , the segments b & c should be High & all others Low.This corresponds to 0x06 in Hex format.

Similarly to display digit 0 , all segments , except g should be made High, which is hex 0x3F.

For this demo , we do not use the Dot .Hence it is 0 for calculating all other digits.

 

Image 2

 

To start with create a folder say , F:\7segment  to store project files.

Open KEIL click on Project –> New uVision Project & browse to the location of the folder created.Provide a filename & Save.

Select the Target Device as ATMEL AT89S52.

Target1 is created in Project file window of KEIL.

Right click Target 1 & click on “Options for Target Target1”

Image 4

On the Options Window select the tab “ OUTPUT”

Check mark the box “Create HEX file” . This step is important & if you forget check marking this box , no HEX file will be generated.

 

Image 5

 

Now to add the C file to project , Right click Source Group 1 & click on “Add new item to Group “l

Image 6

 

Select the first option C File & provide a Name .Click on ADD

Image 8

 

You can see the .C file added to the project .

Image9

Type in the following  code :

—————————————————————

// Port pin P1.0 is used to Enable the 7 segment CC display
//Port pins of P2 are connected to each segment of CC display

#include<reg52.h>
sbit ledenable=P1^0;
void delay(void);

void main()
{
    P2 = 0x00;
    ledenable=0;
while(1)                  //indefinite loop
  {
     ledenable=1;           // Enable pin for Common cathode display
   P2=0x3f;                          //Display digit 0
   delay();
   P2=0x06;                          //Display digit 1
   delay();
   P2=0x5b;                          //Display digit 2
   delay();
   P2=0x4f;                          //Display digit 3
   delay();
   P2=0x66;                          //Display digit 4
   delay();
   P2=0x6d;                          //Display digit 5
   delay();
   P2=0x7c;                          //Display digit 6
   delay();
   P2=0x07;                          //Display digit 7
   delay();
   P2=0x7f;                          //Display digit 8
   delay();
   P2=0x6f;                          //Display digit 9
   delay();
  }
}
void delay(void)
{
    int j;
    int i;
    for(i=0;i<10;i++)
    {
        for(j=0;j<10000;j++)
        {
        }
    }
}

—————————————–

Instead of a long code you can also use Array as below

First initialize all the segment hex values of the digits in an array.

 

unsigned char digit[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};

Now take for loop and assign array values to the PORT2 with some time delay.

Here is the code using Array :

——————————————————-

#include<reg52.h>

sbit ledenable=P1^0;
void delay();
unsigned char digit[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
unsigned int k;
void main()
{
    P2 = 0x00;
    ledenable=0;
   
  while(1)                  //indefinite loop
   {
    ledenable=1;         // Enable pin for Common cathode display
    
       for(k=0;k<10;k++)
        {
           P2=digit[k];
           delay();
        }
   }
}

void delay()
{
    int j;
    int i;
    for(i=0;i<10;i++)
    {
        for(j=0;j<10000;j++)
        {
        }
    }
}

———————————————————–

 

After Feeding in the code click on SAVE button & then F7 to build the project.

 

Image10

 

If all syntax are correct , compiling will be successful.

 

Image 11

Inside the Project folder you can see the HEX file created.This is the file we’re going to fuse on the Target chip.

Image13

 

Connect the ISP board to the Development board & finally connect the USB cable to pc.

Open the PROGISP software .The PRG ISP & USB ASP buttons should be activated.If any one is greyed out then check for the drivers to be installed for the ISP board.

Click on FILE –> LOAD FLASH & browse to the location of the project folder where the HEX file was generated.

Select the HEX file  .Click on AUTO button to perform the programming.

image12

Once the HEX file is fused , you can see the result on the 7 segment display counting from 0 to 9.

 

7SEG21