PIC18F4550 ADC - PIC Controllers
PIC18F4550 ADC - PIC Controllers
Introduction
When we interface sensors to the microcontroller, the output of the sensor many of the times is analog in nature. But
microcontroller processes digital signals.
Hence we use ADC in between sensors and microcontrollers. It converts an analog signal into digital and gives it to the
microcontroller.
There are a large number of applications of ADC like in a biometric application, Environment monitoring, Gas leakage
detection, etc.
GO/DONE’: A/D Conversion Status Bit
When ADON=1
1= Vref –
0= Vss
1= Vref +
0= Vdd
As the ADC pins in PIC18F4550 are multiplexed with many other functions. So these bits are used to de-multiplex them and use
them as an analog input pin.
ADCON2: A/D Control Register 2
110= FOSC/64
101= FOSC/16
100= FOSC/4
010= FOSC/32
001= FOSC/8
000= FOSC/2
The 10-bit result will be placed in ADRESH (8-bit) and ADRESL (8-bit).
1 = Right Justified. However, the lower 8-bits are kept in ADRESL, and the remaining MSB side two bits kept in ADRESH. And
remaining 6 bits (bit 2-7) of ADRESH filled with 0’s.
0 = Left Justified. However, the upper 8-bits are kept in ADRESH, and the remaining two lower bits are placed in ADRESL at bit 7-
6 location. And the remaining 6 lower bits of ADRESL filled with 0’s.
Following is the minimum wait time before the next acquisition can be started.
001= 2 TAD
010= 4 TAD
011= 6 TAD
100= 8 TAD
101= 12 TAD
110= 16 TAD
111= 20 TAD
1. Configure ADCON1 Register to select Reference voltage using VCFG1: VCFG0 bits and also configure port pins which we
require as an analog input using PCFG3: PCFG0 bits.
2. Configure ADCON2 Register to select A/D result format, A/D clock, and acquisition time.
void ADC_Init()
ADCON1 = 0x0E; /* Ref vtg is VDD and Configure pin as analog pin */
ADRESL=0;
1. Configure ADCON0 Register to select a channel that we require using CHS3: CHS0.
2. Start A/D conversion by setting ADON bit and Go/done’ bit of ADCON0 Register.
3. Wait for G0/done’ bit which is cleared when the conversion is completed.
4. Then copy Digital data which is stored in ADRESH and ADRESL Register.
int ADC_Read(int channel)
int digital;
while(ADCON0bits.GO_nDONE==1);
return(digital);
Application
Here we are going to develop a short application using the internal ADC of PIC18f4550.
In this, we will interface a 1K potentiometer which is used to vary the voltage.
This varying voltage is applied to AN0 (channel 0) of ADC and displays this value on 16x2 LCD.
Interfacing Diagram
#include <string.h>
#include <stdlib.h>
#include <P18F4550.h>
void ADC_Init();
int ADC_Read(int);
void main()
char data[10];
int digital;
float voltage;
LCD_String_xy(1,1,"Voltage is...");
while(1)
digital=ADC_Read(0);
voltage= digital*((float)vref/(float)1023);
sprintf(data,"%.2f",voltage);
void ADC_Init()
ADCON1 = 0x0e; /*Ref vtg is VDD & Configure pin as analog pin*/
ADRESL=0;
int digital;
while(ADCON0bits.GO_nDONE==1);
return(digital);