kh

ghi
ghi

Thursday, November 26, 2015

HOW TO DRIVE MULTIPLE SEVEN SEGMENTS WITH PIC






7-segments could come in different forms and different pin configurations, but of all these varied forms are some simple schematics which will always be available for every one form. These are the a to g LED and a common. This tutorial will not bother about the specific pin configuration of every different type of 7-segment, but will concentrate on how to concatenate several 7-segments of a type, based on the a to g configuration.

The above image shows a typical pin configuration of most 7-segments. Simulation of this project will be done on Proteus and proteus has its own way of representing the above 7-segment.


The first 7 pins from left are the pins to the LEDs 'a' through 'g' and the eighth is 'dot'. The last pin is the common( could be common CATHODE or common ANODE depending on the type of 7-segment).


Displaying a number on a 7-segment is basically combining the corresponding LEDs in the set a to g that when lit will show that number. This is very easy as we will only need to manipulate the pins of the microcontroller that are connected to the the LEDs. But when we have to connect more than one seven segment-- on the same microcontroller port, things become a little more difficult. To be able to perform a task like this, we will have to use the method of scanning. Where we will scan through the 7-segments and display on each one its own digit.   The code below shows how to do this in C using MikroC pro for PIC as compiler.


/*
 * Project name:
     Displaying count from 0 to 999 on three 7-segments

 * Description:
     This code dislays count from 0 to 999 on three 7-segments
 * Test configuration:
     MCU:             PIC16F877a
     Oscillator:      HS, 04.0000 MHz
 * NOTES:
     - Displays count from 0 to 999
*/




/// 7-segment select pins
sbit q1 at RD0_bit;
sbit q2 at RD1_bit;
sbit q3 at RD2_bit;

/// database contains a set of combined LEDs that form the digits 0 to 9.
const char database[] = {0b00111111, 0b000000110, 0b01011011, 0b01001111, 0b01100110, 0b01101101, 0b01111101, 0b00000111, 0b01111111, 0b01101111};
char datum[3] = {' ', ' ', ' '};    //temporary storage for current number to be displayed on screen

void displayNum(unsigned int i, int times){                   //function to display a number a couple of frames.
 unsigned short j;
 unsigned short index;
 int repeat;
 char buf[7];
 if((i>999))
  i = 999;


  inttostr(i, buf);

 for(j = 5; j >= 3; j--){
  if(buf[j] == 32)
   buf[j] = '0';
  index = buf[j] - 48;
  datum[j-3] = database[index];
  //datum[j-3] = ~database[index];
  }

  for(repeat = 0; repeat < times; repeat++){
  //scan num
  //NOTE*** for real life connection, change logic of selectors, because definitely you will be using a current source transistor(if working
  //with common ANODE ), or a current sink transistor (if working with common CATHODE)
  q1 = 0;
  q2 = 1;
  q3 = 1;
  PORTB = datum[0];
  delay_ms(10);
  q1 = 1;
  q2 = 0;
  q3 = 1;
  PORTB = datum[1];
  delay_ms(10);
  q1 = 1;
  q2 = 1;
  q3 = 0;
  PORTB = datum[2];
  delay_ms(10);
  }
 }
void main() {
 unsigned int i;
 TRISD = 0;
 TRISB = 0;
 PORTD = PORTB = 0;
 while(1){
 for(i=0; i<999; i++){
  displayNum(i, 20);
  }
  }
}

Click here to download source code and proteus file

1 comment:

  1. nice job sir!
    could you please post a complete mikroC code for multiplexing 3x 7seg displays when an external interrupt occurs?
    that is, a bi-directional counter
    :-):-D

    ReplyDelete