Interfacing PIC18F46K22 with 7-segment display | 4-Digit counter example

This example shows how to interface PIC18F46K22 microcontroller with 7-segment display in order to build a simple 4-digit counter that counts from 0 to 9999. A push button connected to the PIC18F46K22 MCU is used to increment the displayed number.
The compiler used in this project is CCS C.

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:

  • PIC18F46K22 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 PIC18F46K22 with 7-segment display circuit:
Project circuit schematic diagram is shown below.

PIC18F46K22 7-segment display counter circuit

All the grounded terminals 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 a single digit. That means each transistor supplies one digit segments.
The 4 transistors are used to supply the display LEDs with sufficient current because the PIC18F46K22 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 PIC18F46K22 through 4.7k resistor as follows:
digit 1 (most left) transistor base is connected to PIC18F46K22 pin RB3 (#36)
digit 2 transistor base is connected to PIC18F46K22 pin RB2 (#35)
digit 3 transistor base is connected to PIC18F46K22 pin RB1 (#34)
digit 4 (most right) transistor base is connected to PIC18F46K22 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 PIC18F46K22 pin RB4 (#37) is used to increment the displayed number (internal weak pull-up is enabled in the C code).
Common anode 7-segment display is used in this example.

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

Interfacing PIC18F46K22 with 7-segment display C code:
The following C code is for CCS C compiler, it was tested with version 5.051.

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 Timer2 module (8-bit timer) interrupt with 1:16 prescaler and 1:2 postoscaler, this means Timer2 overflows every 2048 microseconds { 256/[8/(4 x 16)] = 256 x 8 = 2048 microseconds } and its interrupt occurred every 4096 microseconds (postoscaler = 2).

Timer2 module configuration is shown below (255 is the preload value):

CCS C Code:

Hardware circuit of this example should be as the one shown below where Arduino uno board is used:

And Proteus simulation also should be the same:

Proteus simulation file download:
PIC18F46K22 7-segment display counter

Other PIC18F46K22 projects where 7-segment display is used:
Print ADC Values on 7-Segment Display with PIC18F46K22
Print rotary encoder values on 7-segment display with PIC18F46K22
Interfacing PIC18F46K22 with LM335 sensor and 7-segment display
Interfacing PIC18F46K22 with 7-segment display and LM35 sensor

Leave a Reply

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