Print ADC Values on 7-Segment Display with PIC18F46K22

This PIC18F46K22 microcontroller project shows how to read analog voltages from an analog channel and print the correspondent digital values on a multiplexed 4-digit 7-segment display. A potentiometer is used to get a voltage that varies between 0 and 5V. A common anode 7-segment display is used in this example.
The compiler used in this project is CCS C.

To see how to interface PIC18F46K22 microcontroller with 7-segment display (4-digit counter example), visit the following post:
Interfacing PIC18F46K22 with 7-segment display | 4-Digit counter example

Components Required:

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

Print ADC values on 7-segment display with PIC18F46K22 circuit:
The following image shows example circuit diagram.

PIC18F46K22 ADC 7-segment example circuit

All the grounded terminals are connected together.

The four transistors used in this example are of the same type (PNP).
The potentiometer has 3 pins VCC , output and GND where:
VCC is connected to +5V
The output pin is connected to PIC18F46K22 analog channel 0 (AN0)
GND is connected to circuit ground.

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

Print ADC values on 7-segment display with PIC18F46K22 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:

Leave a Reply

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