How to interface 4×1 7segment display with PIC micro-controller

Published by MKDas on

Seven Segment display is one of the most common LED/LCD based display which is used in many meters, clocks, info-display type devices. In this article, we’ll learn how to interface Seven Segment display specially that one which have multiple digits in one line. The other name of this type of display is multiplexed 7segment display. Here we’ll learn how to use 4×1 7segment display.

Disclaimer: Electricity is always dangerous. Skill required to work with electricity. Do work with your own risk. Author will not be responsible for any misuse or harmful act. This website contents is also copyright protected. Anything copied and directly posted in your website and claiming it as yours is prohibited and nonsensical. Author published all the articles as opensource to help you making your project and learning purpose only. Learn and make one for yourself. If need any help feel free to ask the author. Author will be helpful to you. Thanks.

What is seven segment display?

Seven Segment display is a set of 8(7+1) LEDs in blocks forming a digit and molded in shape. Here, 7 LEDs are for digit/number display and 1 for dot(.) point. Internal configuration is something like this:

Seven Segment display (Common Cathode & Common Anode type)

As you can see, in one segment LEDs are common by its cathode which is called CC or Common Cathode. And other one have anodes in common which is called CA or Common Anode. These two types segments work almost similarly just the polarity of signals are different as like the LEDs.

What is multiplexed seven segment display?

In some 7segment block, there are several individual digits molded in one single piece of display. There may be 2,3,4 or even more digits in one line. These displays are internally connected, the 8 bits of the display are common for all of the displays in that line. Only the data bit which is equal to the digit numbers are kept individual. That means, the internal connection is something like this:

Internal connection of multiplexed 7segment display

In practical, the display is a set of digits in one line like this one:

4×1 seven segment display

How to drive multiple 7segments ?

Like driving a single 7segment display, we need to generate a set of outputs for digit and one data for each digit. Let take the segment as CC.

3×1 7segment display

As you can see we have 7 set of digit bit (a,b…g) and data bit of D1, D2, D3. Here if we set the digit array as 0x06 and D1 is connected to 0V/GND and D2, D3 are 5V then only digit 1 will display ‘1’ and rest of the digits will show nothing. Again, if D1 and D3 are kept open/set to 5V and D2 to 0V/GND the second digit will display ‘1’.

multiplexing 1
multiplexing 2

As you can see when the 4th data is 0V, it is displaying the 4th digit only. Rest of the digits are kept off. Now you may ask how can we then display 4 digits as a 4 digit number same time? Answer is, our have an response time to anything. When an object moves from its place and came back in same place before 0.25 second, our eye can not detect that change. That means, if we do the multiplexing of 4 individual digits within 0.25 second, our eye will see it as a 4 digit number.

Concept of multiplexing

So the concept of multiplexing in this case is we have to set the digits(a,b..g) of individual digit of a number at the same time the position of that digit will be set as the data. Same process for second or third or more digit will be done sequentially. Whatever the data is we have to complete this process within 10ms. Then we’ll see a complete multi-digit number in the display.

Circuit diagram:

Here is the circuit diagram of our project:

Circuit diagram

Coding:

Now we will do coding for this project with mikroC.

/*******************************************************************************
* Program for, "Seven Segment basic display"                                   *
* Program written by_ Engr. Mithun K. Das                                      *
* MCU:PIC16F73; Xtal:8MHz; mikroC pro for PIC v7.6.0                           *
* Date: 03-04-2020                                                             *
*******************************************************************************/


// array for segment digits, 0-9; CC;
char segment_array[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};//CC_non dot

//pin declearation for digits
sbit digit0 at RC0_bit;
sbit digit1 at RC1_bit;
sbit digit2 at RC2_bit;
sbit digit3 at RC3_bit;

char digits[5];
void display_7segment(int number)
{
   digits[3]=number/1000u;      //store 1000th digit
   digits[2]=(number/100u)%10u; //store 100th digit
   digits[1]=(number/10u)%10u;  //store 10th digit
   digits[0]=(number/1u)%10u;   //store 1st digit
}

void InitTimer0()    //intterupt for 5ms timer ISR
{
  OPTION_REG     = 0x85;
  TMR0           = 100;
  INTCON         = 0xA0;
}

int position=0;
void Interrupt() iv 0x0004 ics ICS_AUTO
{
  if (TMR0IF_bit)
  {
    TMR0IF_bit   = 0;
    TMR0         = 100;

    digit0 = 1;
    digit1 = 1;
    digit2 = 1;
    digit3 = 1;
    if(position>3)position=0;
    
    PORTB = segment_array[digits[position]];
    if(position==3)
    {
        digit0 = 0;
        digit1 = 1;
        digit2 = 1;
        digit3 = 1;
    }
    else if(position==2)
    {
        digit0 = 1;
        digit1 = 0;
        digit2 = 1;
        digit3 = 1;
    }
    else if(position==1)
    {
        digit0 = 1;
        digit1 = 1;
        digit2 = 0;
        digit3 = 1;
    }
    else if(position==0)
    {
        digit0 = 1;
        digit1 = 1;
        digit2 = 1;
        digit3 = 0;
    }
    position++;
  }
}



unsigned int adc_value=0;
void main()
{
 TRISA=0xFF;//all input
 TRISB=0x00;//all output
 TRISC=0x00;//all output
 PORTB=0x00;
 PORTC=0x00;//clear ports
 ADCON1=0x00;
 ADCON0=0x01;//AN0 selected
 InitTimer0();//5ms timer
 while(1)
 {

    adc_value = ADC_Read(0);//read adc value
    display_7segment(adc_value); //print in display
    
 }
}//end

Code explanation:

// array for segment digits, 0-9; CC;
char segment_array[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};//CC_non dot

In this block, we have an array for individual digits for Common Cathode display. You can easily edit this value to whatever you like to see on display.

7-segment edit

mikroC have this 7segment editing tool built in from where you can edit your number.

//pin declearation for digits
sbit digit0 at RC0_bit;
sbit digit1 at RC1_bit;
sbit digit2 at RC2_bit;
sbit digit3 at RC3_bit;

In this block, we simply set pins for each digit. It will help us in next coding part.

void display_7segment(int number)
{
   digits[3]=number/1000u;      //store 1000th digit
   digits[2]=(number/100u)%10u; //store 100th digit
   digits[1]=(number/10u)%10u;  //store 10th digit
   digits[0]=(number/1u)%10u;   //store 1st digit
}

In this sub-function, we simply separating each digit of a number in array. These arrays can be called individually and can be printed easily.

void Interrupt() iv 0x0004 ics ICS_AUTO
{
  if (TMR0IF_bit)
  {
    TMR0IF_bit   = 0;
    TMR0         = 100;

    digit0 = 1; //clear digits
    digit1 = 1;
    digit2 = 1;
    digit3 = 1;
    if(position>3)position=0;
    
    PORTB = segment_array[digits[position]];
    if(position==3)
    {
        digit0 = 0;
        digit1 = 1;
        digit2 = 1;
        digit3 = 1;
    }
    else if(position==2)
    {
        digit0 = 1;
        digit1 = 0;
        digit2 = 1;
        digit3 = 1;
    }
    else if(position==1)
    {
        digit0 = 1;
        digit1 = 1;
        digit2 = 0;
        digit3 = 1;
    }
    else if(position==0)
    {
        digit0 = 1;
        digit1 = 1;
        digit2 = 1;
        digit3 = 0;
    }
    position++;
  }
}

In this Interrupt Sub Routine we are utilizing our 5ms time timer to print each digit sequentially. Here, Timer0 is used to call a 5ms timer interrupt. When we come back in this routine, we print the port array which is found from digit array. And the according to the position, data bits are switched. As we are programming for common cathode (CC) display, so the digit will be activated when the data bit is 0. To clear digits, these data bits are set to 1.

adc_value = ADC_Read(0);//read adc value
display_7segment(adc_value); //print in display

And finally in while loop, we simply taking the ADC reading and displaying that value in our 7segment display.

Result:

In proteus, we can simulate this project and check what it displays.

Test result

I hope you can now make you own multiplexed 7segment display for yourself. If you need any help feel free to ask me. Thank you, Enjoy!


MKDas

Mithun K. Das; B.Sc. in EEE from KUET. Blog: https://labprojectsbd.com

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *