Interfacing PIC microcontroller with 7-segment display | mikroC Projects

This post shows how to interface PIC16F887 microcontroller with 7-segment display in order to build a simple 4-digit counter which counts from 0 to 9999. A push button connected to the PIC16F887 MCU is used to increment the displayed number.
The compiler used in this project is mikroC PRO for PIC.

There are two types of the seven-segment displays: common anode and common cathode.
In the common anode type all the 7 LED anode terminals are connected together whereas in the common cathode all cathode terminals are connected together.

The common terminal is connected to +VCC (+5V, +3.3V …) or GND (0V) depending on the type of the 7-segment display (common anode or common cathode respectively).

Basically for each 7-segment digit there are 8 pins: one for the common terminal (anode or cathode) and 7 pins for the 7 segments (A, B, C, D, E, F and G). Another pin may be used for the decimal point (DP).

one-digit seven-segment display

In multi-digit 7-segment display (for example 4-digit) all pins of the same segment are connected together (segment A of digit 1 with segment A of digit 2 …), and each digit has its common pin alone. This is called multiplexing technique. This technique minimizes number of pins used.

So, for a 4-digit display we’ll have 7 pins of the 7 segments, 4 pins of the 4 digits (common terminals) and 1 pin for the decimal point (DP) which means a total of 12 pins.

Hardware Required:

  • PIC16F887 microcontroller   —->  datasheet
  • 4-digit common anode 7-segment display
  • 4 x PNP transistor (2SA1015, 2S9015, 2N3906 …)
  • 7 x 100 ohm resistor
  • 4 x 4.7k ohm resistor
  • Push button
  • 5V source
  • Breadboard
  • Jumper wires

Interfacing PIC16F887 MCU with 7-segment display circuit:
Project circuit schematic diagram is shown below.

PIC16F887 7 segment display circuit

All the grounded terminal are connected together.

In the circuit there are 4 transistors of the type PNP, the collector of each transistor is connected to common anode pin of 1 digit. That means each transistor supplies one digit segments.
The 4 transistors are used to supply the display LEDs with sufficient current because the PIC16F887 microcontroller may not be able to do that (maximum output current is 25 mA).

Each transistor emitter pin is connected to +5V and each transistor base is connected to the PIC16F887 through 4.7k resistor as follows:
digit 1 (most left) transistor base is connected to PIC16F887 pin RB3 (#36)
digit 2 transistor base is connected to PIC16F887 pin RB2 (#35)
digit 3 transistor base is connected to PIC16F887 pin RB1 (#34)
digit 4 (most right) transistor base is connected to PIC16F887 pin RB0 (#33)

Each 100 ohm resistor is used for limiting the current that passes through the segment LED.

The push button which is connected to PIC16F887 pin RB4 (#37) is used to increment the displayed number.
A common anode 7-segment display is used in this example.

In this project the PIC16F887 microcontroller runs with its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.

Interfacing PIC16F887 MCU with 7-segment display C code:
The following C code is for mikroC PRO for PIC compiler, it was tested with version 7.2.0.

Since the 4 digits are multiplexed we need to refresh the display very quickly (display one digit at a time, others are off), for that I used Timer0 module (8-bit timer) interrupt with 1:16 prescaler, this means Timer0 overflows every 2048 microseconds { 256/[8/(4 x 16)] = 256 x 8 = 2048 microseconds }.

Full mikroC code:
Configuration words:
CONFIG1 = 0x2CD4
CONFIG2 = 0x0700

The following picture shows my breadboard circuit:

PIC16F887 with 7-segment display, 4-digit counter

Other PIC16F887 projects where 7-segment display is used:
Print ADC Values on 7-Segment Display with PIC MCU | mikroC Projects
PIC16F887 with 7-Segment Display and Rotary Encoder | mikroC Projects
7-Segment Display with 74HC595 Shift Register | mikroC Projects

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.