ASeven Segment Display circuit diagram

                 Figure 1: 7-Segment display connected to PIC microcontroller

The 7-segment display is the earliest type of an electronic display that uses 7 LEDs bars arranged in a way that can be used show the numbers 0 – 9. (actually 8 segments if you count the decimal point, but the generic name adopted is 7-segment display.) These devices are commonly used in digital clocks, electronic meters, counters, signalling, and other equipment for displaying numeric only data. 
It is not different from an LED in terms of interfacing, by turning the appropriate segments ON and OFF we can display easily the numbers 0 to 9 and optionally the decimal point (DP). 
                                                  

  Figure 2: 7-Segment displaying 8 with decimal point         Figure 3: 7-Segment displaying 3

The segments of the displays are normally referred to by letters ‘a’ to ‘g’.
Figures 2 and 3 show how a 7-segment display can display digits. 
In figure 2, all the segments (LEDs) are switched on to display the digit “8” with the decimal point. On the other hand, in figure 3, segments a, b, c, d and g are switched on to display the digit “3”.  any combination can be used to display any desired digit.
The segments can also be used to display some letters, but this is limited. For example, the letter “b” can be displayed by switching on the segments  c, d, e and f and the letter “F” by switching on segments a, e, f and g.
7-segment displays are available in two configurations: common anode and common cathode. As shown in Figures 4 and 5 below.

       

    Figure 4: Common Anode 7-Segment              Figure 5: Common Cathode 7-Segment

In figure 4, the anode pins of all the segments are connected together and this pin is usually connected to the power supply. Individual segments are turned ON by grounding the required segment pin through the microcontroller by sending a “0” to the pin output. 
In figure 5 as well as in figure 1, all the cathodes of all the segments are connected together and this pin is usually connected to ground. Individual segments are turned ON by applying voltage to the required segment pin through the microcontroller by sending a “1” to the pin output. 

A PIC can source or sink 25mA of current per Input/Output pin. When designing an LED circuit, we have to know the typical voltage drop as we have learnt from the Blinking an LED Connected to a PIC microcontroller article. 
As with standard LEDs, it is required to use current limiting resistors in each segment of the display to limit the current as shown in figure 1. 

The easiest way to display a number on the 7-segment is to find a way to determine or look up the pattern corresponding to the digit to be displayed . This can be something like a table showing the numbers and the corresponding segment that should be turned ON or OFF to display something and the required number (this can be in decimal, hexadecimal or in binary format) to be sent to the port where the display is connected to in order to display a specific number.

Table 1 is an example to show the hexadecimal values of a common cathode 7-Segment display connected to a port of a microcontroller from bit 0 to bit 7. 
            
Table 1: 7-segment lookup table for hexadecimal values of a common cathode 7-Segment display

The mikroC Pro for PIC language includes a 7-segment LED editor that can be used to find the decimal or hexadecimal codes to be sent to a port to display a given pattern. All you need to do is to click on the LED segments and the corresponding value in Hex or decimal will be generated for common anode and common cathode. The editor is invoked from the drop-down menu by clicking Tools -> Seven Segment Editor.

Figure 6: Invoking the Seven Segment Editor

Figure 7 below shows a typical display of the editor where the pattern for number ‘3’ is created. The code for this pattern is displayed as 0×4F for common cathode displays

Figure 7: Seven Segment Editor

To display more digits, more 1-digit 7-segment display can be joined together as needed.

A 1-digit 7-segment display can only show numbers from 0 to 9, a 2-digit display can show numbers between 0 and 99, a 3-digit between 0 and 999, a 4-digit between 0 and 9999, and so on. We will learn how to display more than one digits in the Multiplexing of 7-Segment Displays with PIC Microcontroller article.

MikroC Code

The circuit in figure 1 shows a digit counter counting from 0 to 9 with 1 second delay in between when switch  SW1 is closed. If the count reaches 9, it resets and starts again from 0. when the switch is open, the counter stops and resume counting from where it stopped once closed again.

You can download the full project files (MikroC Pro for PIC source code and Proteus Schematic design) below here.  All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).  

Download Seven Segment MikroC

Download Seven Segment Circuit Diagram