You are on page 1of 10

O Home

O 9mel '# Tu9orials


o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
O !roduc9s
o
o
o
o
o
o
o
o
O 3li3e S9ore
O orum
O i3s
O o39ac9 Us
o
o
Jul-21s9-2010
Using Analog to Digital Converter (ADC) - PIC Microcontroller Tutorial

Ma3 elec9rical sig3als arou3d us are 3alog i3 3a9ure. Tha9 mea3s a qua39i9 varies
direc9l wi9h some o9her qua39i9. The Iirs9 qua39i9 is mos9l vol9age while 9ha9 seco3d
qua39i9 ca3 be a39hi3g lie 9empera9ure, pressure, ligh9, Iorce or accelera9io3. or
example i3 M35 temperature sensor 9he ou9pu9 vol9age varies accordi3g 9o 9he
9empera9ure, so iI we could measure vol9age, we ca3 measure 9empera9ure.
Bu9 mos9 oI our compu9er (or Microco39rollers) are digi9al i3 3a9ure. The ca3 o3l
diIIere39ia9e be9wee3 HIGH or level o3 i3pu9 pi3s. or example iI i3pu9 is more 9ha3
2.5v i9 will be read as 1 a3d iI i9 is below 2.5 9he3 i9 will be read as 0 (i3 case oI 5v
ss9ems). So we ca33o9 measure vol9age direc9l Irom MUs. To solve 9his problem mos9
moder3 MUs have a3 u3i9. s9a3ds Ior a3alog 9o digi9al co3ver9er. I9 will
co3ver9 a vol9age 9o a 3umber so 9ha9 i9 ca3 be processed b a digi9al ss9ems lie MU.
This e3ables us 9o easil i39erIace all sor9 oI a3alog devices wi9h MUs. Some reall
helpIul example oI a3alog devices are
1. igh9 Se3sors.
2. Tempera9ure Se3sors.
3. ccelerome9ers.
4. Touch Scree3s.
5. Micropho3e Ior udio #ecordi3g.
3d possibl ma3 more.
I3 9his 9u9orials we will lear3 9o use 9he i39er3al oI !I18 devices (Example is Ior
!I184520 which is a 40 !IN device).
$pecifications of ADCs
Mos9 impor9a39 speciIica9io3 oI s is 9he resolu9io3. This speciIies how accura9el 9he
measures 9he a3alog i3pu9 sig3als. ommo3 s are 8 bi9, 10 bi9 a3d 12 bi9. or
example iI 9he reIere3ce vol9age(explai3ed la99er) oI is 0 9o 5v 9he3 a 8 bi9 will
brea i9 i3 256 divisio3s so i9 ca3 measure i9 accura9el up 9o 5/256 v 19m' approx.
hile 9he 10 bi9 will brea 9he ra3ge i3 5/1024 4.8m' approx. So ou ca3 see 9ha9
9he 8 bi9 ca39 9ell 9he diIIere3ce be9wee3 1m' a3d 18m'. The i3 !I18 are 10
bi9.
9her speciIica9io3 i3clude (bu9 3o9 limi9ed 9o) 9he sampli3g ra9e, 9ha9 mea3s how Ias9 9he
ca3 9ae readi3gs. Microchip claims 9ha9 pic18I4520s ca3 go as high as 100K
samples per seco3d.
ADC Terminology
Reference Voltage: The reIere3ce vol9age speciIies 9he mi3imum a3d maximum vol9age
ra3ge oI a3alog i3pu9. I3 !I 18 9here are 9wo reIere3ce vol9age, o3e is 9he 'reI- a3d o3e is
'reI. The 'reI- speciIies 9he mi3imum i3pu9 vol9age oI a3alog i3pu9 while 9he 'reI
speciIies 9he maximum. or example iI 9he i3pu9 sig3al 'reI- is applied 9o a3alog i3pu9
cha33el 9he3 9he resul9 oI co3versio3 will be 0 a3d iI vol9age equal 9o 'reI is applied 9o
9he i3pu9 cha33el 9he resul9 will be 1023 (max value Ior 10bi9 ).

ig.: ADC Reference Voltage.
The 'reI a3d 'reI- pi3s are available i3 !IN5 a3d !IN4 oI 9he !I184520 chip. So ou
ca3 co33ec9 9he reIere3ce vol9age here. or a simple desig3 9he 'reI- is GN a3d 'reI is
'cc. s 9his is such a commo3 co3Iigura9io3 9ha9 9he ca3 be se9 up 9o use 9hese
reIere3ce i39er3all. ThereIore ou do 3o9 3eed 9o co33ec9 9hese o3 9he ex9er3al 'reI pi3s,
so ou ca3 use 9hem Ior o9her purpose.
ADC Channels: The module is co33ec9ed 9o several cha33els via a mul9iplexer. The
mul9iplexer ca3 co33ec9 9he i3pu9 oI 9he 9o a3 oI 9he available cha33els. This allows
ou 9o co33ec9 ma3 a3alog sig3als 9o 9he MU (sa 3 9empera9ure se3sors). I3
!I184520 9here are 13 a3alog i3pu9 cha33els, 9he are 3amed N0, N1 e9c. You ca3
have a loo a9 9he pi3 co3Iigura9io3 i3 9he pic18I4520s da9ashee9 9o loca9e 9heir posi9io3.
Acquisition Time: he3 a3 speciIic cha33el is selec9ed 9he vol9age Irom 9ha9 i3pu9
cha33el is s9ored i3 a3 i39er3al holdi3g capaci9or. I9 9aes some 9ime Ior 9he capaci9or 9o ge9
Iull charged a3d become equal 9o 9he applied vol9age. This 9ime is called acquisi9io3 9ime.
The !I184520s provides a programmable acquisi9io3 9ime, so ou ca3 se9up 9he
acquisi9io3 9ime. 3ce acquisi9io3 9ime is over 9he i3pu9 cha33el is disco33ec9ed Irom 9he
source a3d 9he co3versio3 begi3. The acquisi9io3 9imes depe3ds o3 several Iac9or lie 9he
source impeda3ce, 'dd oI 9he ss9em a3d 9empera9ure. You ca3 reIer 9o 9he page 227 a3d
228 i3 9he da9ashee9 Ior de9ails o3 i9s calcula9io3. saIe value is 2.45u$, so acquisi9io3
9ime mus9 be se9 9o a3 value more 9ha3 9his.
ADC Clock: #equires a cloc source 9o do i9s co3versio3, 9his is called loc.
The 9ime period oI 9he loc is called T

. I9 is also 9he 9ime required 9o ge3era9e 1


bi9 oI co3versio3. The requires 11 T

9o do a 10 bi9 co3versio3. I9 ca3 be derived


Irom 9he !U cloc (called T
S
) b dividi3g i9 b a sui9able divisio3 Iac9or. There are
Seve3 possible op9io3.
O 2 x T
S

O 4 x T
S

O 8 x T
S

O 16 x T
S

O 32 x T
S

O 64 x T
S

O I39er3al #
or orrec9 / o3versio3, 9he / co3versio3 cloc (T

) mus9 be as shor9 as possible


bu9 grea9er 9ha3 9he mi3imum T

. See 9able 26-25 i3 !I184520 da9ashee9 (or 9able 28-


29 i3 !I184550/!I182550 da9ashee9). I9 is 0.7uS Ior !I18 device a3d 1.4uS
Ior !I18 device.
e are ru33i3g a9 20MHz i3 our PIC Development board so we se9 prescaler oI 32
TS.
Jur F
JSC
= 20MHz
Therefore our F
JSC
= 1/20MHz
= 50nS

32 T
JSC
= 32 x 50 nS
= 1600nS
= 1.6uS

1.6 uS is more than the minimum requirement.

You ca3 calcula9e 9he value Ior divisio3 Iac9or usi3g 9he above example i3 case ou are
usi3g crs9al oI o9her Ireque3c. lso 3ow we have 9he T

we ca3 calcula9e 9he divisio3


Iac9or Ior acquisi9io3 9ime. cquisi9io3 9ime ca3 be speciIied i3 9erms oI T

. I9 ca3 be se9
9o o3e oI 9he Iollowi3g values.
O 20 x T


O 16 x T


O 12 x T


O 8 x T


O 6 x T


O 4 x T


O 2 x T


O 0 x T


s we saw i3 above paragraph 9ha9 9he saIe acquisi9io3 9ime is 2.45uS, so we selec9 2 x T


as acquisi9io3 9ime.

T
ACQ
=2 x T
AD

=2 x 1.6uS (Replacing T
AD
= 1.6uS)
=3.2uS


3.2uS is more 9ha3 required 2.45uS so i9s o.
Programming ADC in HI-TECH C for MPAB
is co33ec9 9o 9he !I !U b 3 co39rol regis9er a3d 2 da9a regis9er. The co39rol
regis9ers are used 9o se9up a3d give comma3ds 9o 9he . The also provides 9he s9a9us oI
. The 9wo da9a regis9ers holds 9he 10 bi9 oI co3ver9ed da9a. Si3ce each resis9er i3
!I18 is oI 8 bi9s 9hereIore 2 regis9ers are required 9o hold 9he 10bi9 da9a.
e will develop 9wo Iu3c9io3s 9o suppor9 i3 our projec9s. 3e will help i3i9ialize 9he
module a3d o9her will help us selec9 a3 oI 9he 13 cha33els a3d s9ar9 9he co3versio3. I9er
9he co3versio3 is do3e i9 will re9ur3 us 9he resul9s.
I am 3o9 givi3g here 9he descrip9io3 oI 9he co39rol a3d da9a regis9ers as 9he are ver clearl
explai3ed i3 !I184520s da9ashee9 o3 page 223 9o 225. I reques9 ou 9o dow3load 9he
da9ashee9 a3d read 9he descrip9io3 so 9ha9 ou will have a3 Idea oI wha9 ever bi9 i3 9he
regis9ers do. s I 9old beIore, is co33ec9ed 9o 9he !U via 9hree co39rol regis9er a3d
9wo da9a regis9ers. The 9hree co39rol regis9ers are :-
O ADCON0 - Used 9o selec9 a3alog i3pu9 cha33el,s9ar9 9he co3versio3, chec iI 9he
co3versio3 is do3e a3d 9o swi9ch o3/oII 9he module.(e use 9his i3 #ead()
Iu3c9io3.)
O ADCON1 - Used 9o Selec9 'ol9age reIere3ce, a3d 9o co3Iigure por9s as 3alog oI
digi9al. (e leave 9hese 9o deIaul9s)
O ADCON2 - Used 9o selec9 da9a Iorma9, Se9 acquisi9io3 9ime, cloc se9up
(e se9up 9hese i3 I3i9() Iu3c9io3)
irs9 we co3Iigure 9he 9o our 3eeds i3 9he ADCInit() Iu3c9io3.


//Function to Initialise the ADC Module
void ADCInit()
,
//We use default value for +/- Vref

//VCFG0=0,VCFG1=0
//That means +Vref = Vdd (5v) and -Vref=GEN

//Port Configuration
//We also use default value here too
//All ANx channels are Analog

/*
ADCON2

*ADC Result Right Justified.
*Acquisition Time = 2TAD
*Conversion Clock = 32 Tosc
*/

ADCJN2=0b10001010;
,



You ca3 see 9ha9 we o3l se9 up 9he N2 regis9er.e se9up 9he as Iollows
O #esul9 Iorma9 as #igh9 Jus9iIied(Explai3ed la99er).
O cquisi9io3 9ime 2T

(s alcula9ed bove)
O o3versio3 loc as 32 T
S
(s alcula9ed bove)
e also leave N1 9o deIaul9s, which implies 9he Iollowi3g
O '#E is 5v (ur 'cc)
O -'#E is GN
O ll Nx cha33els are 3alog. II ou 3eed some oI 9hem 9o do digi9al I/ 9he3 se9up
9hem accordi3gl.
Now we have our Module se9up, whe3 ever ou wa39 9o do 9he o3versio3 i3
a3 cha33el, simpl call #ead(). or example 9o do o3versio3 o3 channel 0
a3d s9ore 9he resul9 i3 variable ;, call 9he Iu3c9io3 i3 9he Iollowi3g wa.
;al=ADCRead(0);
Tha9s i9 ! 9he a3alog value prese39 o3 N0 will be co3ver9ed 9o a digi9al value a3d s9ored i3
variable ;,.
How the ADCRead() function works?


//Function to Read given ADC channel (0-13)
:38ig30d i39 ADCRead(:38ig30d char ch)
,
if(ch13) r09:r3 0; //Invalid Channel

ADCJN0=0x00;

ADCJN0=(ch<<2); //Select ADC Channel

ADJN=1; //switch on the adc module

GJDJNE=1; //Start conversion

hil0(GJDJNE); //wait for the conversion to finish

ADJN=0; //switch off adc

r09:r3 ADRES;
,



The Iirs9 li3e checs iI 9he i3pu9 cha33el provided b 9he user is valid or 3o9. The3 we
selec9 cha33el. I9er 9ha9 we swi9ch o3 9he module b se99i3g ADON bi9. The3
co3versio3 is s9ar9ed b se99i3g 9he GODONE bi9. s soo3 as 9he GODONE bi9 is se9 9o 1
9he module s9ar9s 9he co3versio3 process. s lo3g as 9he module is bus 9he GODONE bi9
is HIGH, a3d whe3 9he co3versio3 is comple9e i9 is cleared b 9he module. So we wai9 i3
9he while loop as lo3g as GNE is high. #emember 9ha9 9he while loop is emp9 (a semi
colo3 jus9 aI9er i9), so as lo3g as GODONE is high 9he !U will do 3o9hi3g. s soo3 as
GODONE is cleared 9he while loop breas a3d we swi9ch oI 9he module b wri9i3g 0 9o 9he
ADON bi9. i3all 9he resul9 oI co3versio3 is re9ur3ed, ADRE$ regis9er holds 9he
co3ver9ed value.
Demo program to test PIC ADC Code
e will wri9e a ver simple program 9ha9 will demo3s9ra9e usage. The program will
read cha33el 0 (N0 !IN) a3d displa i9s value o3 CD $creen. BeIore a99emp9i3g
9he experime39 please read 9he Iollowi3g 9u9orials. s ou will 3eed 9he CD Suppor9 i3
addi9io3 9o 9he I39erIace code.
O I39erIaci3g wi9h !I MU
O Mai3g 9he Expa3sio3 board
The program is i39e3ded 9o be compiled usi3g 9he HI-TEH Ior !I18 usi3g 9he
M!B IE. So iI ou are 3o9 Iamiliar wi9h 9he build s9eps please see 9he Iollowi3g
9u9orials.
O ow3loadi3g, o3Iiguri3g a3d Usi3g Basic evelopme39 Tools Ior Microchip !I
Series oI MUs.

/********************************************************************

ANALOG TO DIGITAL CONVERTOR INTERFACING TEST PROGRAM

---------------------------------------------------------
Simple Program to connect with the internal ADC of PIC MCUs.
The program reads and display the analog input value at AN0.
Requires the PIC18 lcd library.

MCU: PIC18FXXXX Series from Microchip.
Compiler: HI-TECH C Compiler for PIC18 MCUs (http://www.htsoft.com/)

Copyrights 2008-2010 Avinash Gupta
eXtreme Electronics, India

For More Info visit
http://www.eXtremeElectronics.co.in

Mail: me@avinashgupta.com

********************************************************************/
#include <htc.h

#include "lcd.h"

//Chip Settings
__CJNFIG(1,0x0200);
__CJNFIG(2,0X1E1F);
__CJNFIG(3,0X8100);
__CJNFIG(4,0X00C1);
__CJNFIG(5,0XC00F);


//Simple Delay Routine
void Wait(:38ig30d i39 delay)
,
for(;delay;delay--)
__delay_us(100);
,

//Function to Initialise the ADC Module
void ADCInit()
,
//We use default value for +/- Vref

//VCFG0=0,VCFG1=0
//That means +Vref = Vdd (5v) and -Vref=GEN

//Port Configuration
//We also use default value here too
//All ANx channels are Analog

/*
ADCON2

*ADC Result Right Justified.
*Acquisition Time = 2TAD
*Conversion Clock = 32 Tosc
*/

ADCJN2=0b10001010;
,

//Function to Read given ADC channel (0-13)
:38ig30d i39 ADCRead(:38ig30d char ch)
,
if(ch13) r09:r3 0; //Invalid Channel

ADCJN0=0x00;

ADCJN0=(ch<<2); //Select ADC Channel

ADJN=1; //switch on the adc module

GJDJNE=1;//Start conversion

hil0(GJDJNE); //wait for the conversion to finish

ADJN=0; //switch off adc

r09:r3 ADRES;
,
void main()
,
//Let the LCD Module start up
Wait(100);

//Initialize the LCD Module
LCDInit(LS_BLINK);

//Initialize the ADC Module

ADCInit();

//Clear the Module
LCDClear();

//Write a string at current cursor pos
LCDWriteString("ADC Test");

hil0(1)
,
:38ig30d i39 ;al; //ADC Value

;al=ADCRead(0); //Read Channel 0
LCDWriteIntXY(0,1,;al,4);

Wait(1000);
,




,


Basic PIC Hardware to test the ADC Code.
The Iollowi3g image shows 9he basic hardware required 9o ru3 9he above code. No9e 9ha9
9he crs9al Ireque3c is 20MHz, so please use 9his value o3l, iI ou wa39 quic a3d error
Iree opera9io3. r iI ou are experie3ced e3ough ou ca3 sor9 ou9 errors iI a3.

ig.: $chematic for PIC ADC Test. (Click To Enlarge/Print)
II ou are usi3g our 40 !IN !I evelopme39 Board 9he3 mos9 oI 9he hardware is alread
do3e Ior ou. You jus9 3eed 9o mae a3d a99ach 9he Expa3sio3 Board 9o 9he mai3 ev
Board. RV2, which is a 10K variable resis9or is used 9o Ieed variable vol9age be9wee3 0 a3d
5v 9o 9he a3alog i3pu9 !IN. he3 9he demo is ru33i3g ou ca3 adjus9 #'2 ca3 3o9e 9he
cha3ge is read ou9 o3 9he Module.

You might also like