Monday, February 26, 2018

7-segment display Using PIC16F877A

Introduction:

      Seven segment displays are very common for electronic product to display numerical output. Many common devices like calculators, lift, watches, electronic weighing scales, ovens etc use them. A seven-segment display is so named because it is divided into seven different segments that can be switched on or off. It can display digits from 0 to 9 and quite a few characters like A, b, C, ., H, E, e, F, n, o, t, u, y, etc. Knowledge about how to interface a seven segment display to a micro controller is very essential in designing embedded systems.

The Pin Out and Picture of a 7-segment Display:



Two types of 7-segment display
Seven segment displays are of two types, common cathode and common anode. In common cathode type , the cathode of all LEDs are tied together to a single terminal which is usually labeled as ‘com‘   and the anode of all LEDs are left alone as individual pins labeled as a, b, c, d, e, f, g &  h (or dot) . In common anode type, the anode of all LEDs are tied together as a single terminal and cathodes are left alone as individual pins.

Interfacing 7 segment display

Below table shows the binary/hex values for displaying the digits on CommonCathode seven segment display

Below table shows the binary/hex values for displaying the digits on CommonAnode seven segment display

Multiplexing 7-Segment Display
         In multiplexing all displays are connected in parallel to one port and only one display is 
         allowed to turn ON at a time, for a short period. This cycle is repeated for at a fast rate and due           to the persistence of vision of human eye, all digits seems to glow. 
        The main advantages of this method are

–Fewer number of port pins are required .
–Consumes less power.

–More number of display units can be interfaced (maximum 24).


Connection Diagram of Multiple 7-segment Display with PIC16F877A

How it works
         Let us see how ‘16’ will be displayed in 2 digit display.Initially the first display is only activated by making  RC.0 low and then digit drive pattern for “1″ is loaded to  the Port D. This condition is maintained for around 1ms and then RC.0 is made high. Then the second display is activated by making RC.1 low and then the digit drive pattern for “6″ is loaded to the port D. This will make the second display to show “6″. This condition is maintained for another 1ms. This cycle is repeated and due to the persistence of vision you will feel it as “16″.

Code:
// two digit number using PIC16F877A // 12MHz
   void main() {

     unsigned int digit;
     unsigned int digit2;
     unsigned int  cnt=0;
     char seg[10]={0x3F,0x06,0x5B,0x4F,0x66,0x7D,0x07,0x7F,0x6F};
     TRISD = 0;  // CONfigure for portD is output
     PORTD = 0;   // all are PortD are low or zero
     TRISC.F0=0;  // PORTC-0 Pin is output
     TRISC.F1=0;  // PORTC-1 is output
     PORTC.F0=0;  // PIC IS low or zero
     PORTC.F1=0;  // Pin is low or zero


while(1){
         for (cnt=0; cnt<100; cnt++){
         digit=cnt/10;
         digit2=cnt%10;
         
         PORTD=seg[digit];
         portc.f0=1;
         portc.f1=0;
         delay_ms(100);
         PORTD=0x00;
         
         PORTD=seg[digit2];
         portc.f0=0;
         portc.f1=1;
         delay_ms(100);
         PORTD=0x00;
         }

}
}  


Circuit Diagram