Print ADC Values on 7-Segment Display with PIC18F46K22

This PIC18F46K22 microcontroller project shows how to read analog voltages from an analog channel (using ADC module) 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 (2SA10152S90152N3906 …)
  • 10k ohm potentiometer
  • 7 x 100 ohm resistor
  • 4 x 4.7k ohm resistor
  • 5V source
  • Breadboard
  • Jumper wires
  • PIC MCU Programmer (PICkit 3, PICkit 4…)

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.

The PIC18F46K22 ADC (Analog-to-Digital Converter)module  reads analog voltage applied to analog channel AN0 (#2) and convert it to a digital representation which is then printed on the 7-segment display.
The PIC18F46K22 microcontroller has a built-in ADC module with 10-bit resolution. With a positive reference voltage of VCC (+5V), a 0V is represented by 0 and 5V is represented by a digital value of 1023.

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:


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top