Está en la página 1de 4

Configuring the PIC18F4550 10-bit ADC

module
Introduction
As always, make sure you have a copy of the datasheet for your PIC. Here's a link to the
PIC18!""# datasheet.
$he Analo% to &i%ital Converter on your PIC microcontroller is an incredi'ly useful tool.
(sin% the A&C, you can interface all sorts of wonderful peripherals, such as
potentiometers, %yroscopes, accelerometers, and many other types of sensors.

$he A&C takes a )**+)&& ,#+") on my PIC mcu- &C si%nal as its input and then
converts the volta%e to a di%ital value. (sin% the 1#+'it A&C, this means that a volta%e
level of #) should 'e read as #, and ") as 1#./ ,.01# + 1-. *ome PIC microcontrollers
also have 1.+'it A&C modules, allowin% for a much hi%her resolution. &ependin% on the
num'er of pins on the microcontroller, it has 'etween 1# and 1/ analo% inputs.
ADC Configuration
$he A&C module on the PIC18!""# has five re%isters associated with it ,pa%e .12 in
the datasheet-3
4esult Hi%h 4e%ister ,A&45*H-
4esult 6ow 4e%ister ,A&45*6-
Control 4e%ister # ,A&C78#-
Control 4e%ister 1 ,A&C781-
Control 4e%ister . ,A&C78.-
9e'll start 'y confi%urin% the A&C78# control re%ister.

ADCON0bits.ADON = 0; // Disable A/D module
ADCON0bits.CHS0 = 0; // Select channel 0 (AN0)
ADCON0bits.CHS1 = 0;
ADCON0bits.CHS2 = 0;
ADCON0bits.CHS = 0;
7nly one channel ,I:7 pin- may 'e sampled at one time. $he CH*/3CH*# ,CHannel
*elect- 'its are used to select the channel that will 'e sampled when the ;7<&785 'it is
set. I'm only usin% one analo% input and so I decided to use channel # ,A8#-, as this will
leave the rest of the I:7 pins availa'le as di%ital inputs or outputs. I choose channel # 'y
clearin% 'its CH*/3CH*# and, since I'm not usin% any other channels, I will always leave
them cleared. If you use multiple channels, =ust chan%e the CH*/3CH*# 'its to select the
channel you want to sample, then set the ;7<&785 'it to start the sample:conversion
process.
8e>t is the A&C781 re%ister. $his re%ister controls which I:7 pins are analo% and which
are di%ital. As previously mentioned, I chose to use channel # so that the rest of the pins
would 'e availa'le as di%ital I:7. $wo 'its in the re%ister also determine what the volta%e
reference source is. A ta'le in the datasheet shows the different options3

A&C781 confi%uration3

ADCON1bits.!C"#1 = 0; // $se !SS %o& !&e%' sou&ce
ADCON1bits.!C"#0 = 0; // $se !DD %o& !&e%( sou&ce
ADCON1bits.)C"#0 = 0; // *a+e AN0 ,in analo- and all othe&s di-ital
ADCON1bits.)C"#1 = 1;
ADCON1bits.)C"#2 = 1;
ADCON1bits.)C"# = 1;
$he last confi%uration re%ister is A&C78.. $he A&? 'it determines whether the result
in the A&45*H3A&45*6 'yte pair is ri%ht or left =ustified.
I prefer to make the result ri%ht =ustified. 5verythin% else is little endian, so why shouldn't
the A:& result 'e@ Actually, there is a valid reason for havin% left =ustification. If you only
wanted 8+'its from the 1#+'it result, then you could use left =ustification and only use the
A&45*H 'yte, effectively discardin% the two least si%nificant 'its.

8ow the A:& conversion clock needs to 'e set. $he time to convert one 'it is called $A&
and must 'e %reater than the minimum $A& time, shown 'elow in $a'le .8+.A3
As you can see, the minimum $A& time is #.8 @s. $A& can 'e confi%ured to eBual3
. $
7*C
! $
7*C
8 $
7*C
11 $
7*C
/. $
7*C
1! $
7*C
$
7*C
C 1:
7*C
, where
7*C
is the freBuency of your crystal or oscillator ,after the
P66, if you're usin% it-. In my case, 7*C is !8?HD, makin% $7*C C 1:!8?HD EC
.#.8// ns. *o, divide #.8 @s 'y .#.8// ns and the result is E/8.!, meanin% that for $A& to
'e lon%er than the minimum time, I need to make it at least /8.!F$7*C. $his is 'etween
the /. $7*C and 1! $7*C confi%uration options, so we must %o with the lon%er one, 1!
$7*C.
If you don't feel like doin% the math, then ta'le .1+1 is an easy way to determine what the
A:& conversion clock 'its should 'e set to3
inally, the three 'its selectin% the acBuisition time need to 'e confi%ured. Gou can find
the formula to calculate the acBuisition time in the datasheet, 'ut it's pro'a'ly sufficient
to say that it needs to 'e a'out .." @s. $he acBuisition time determines how lon% the
channel is sampled 'efore startin% the conversion. $he acBuisition time can 'e one of the
followin% options3
# $
A&
. $
A&
! $
A&
1 $
A&
8 $
A&
1. $
A&
11 $
A&
.# $
A&
*ince my $
A&
is 1!F$
7*C
C 1!F.#.8// ns EC 1.// @s, I need to make the acBuisition
time at least . $A&.
A&C78. confi%uration3

ADCON2bits.AD"* = 1; // A/D &esult is &i-ht .usti%ied
ADCON2bits.AC/00 = 1; // Ac1uisition time
ADCON2bits.AC/01 = 0;
ADCON2bits.AC/02 = 0;
ADCON2bits.ADCS0 = 0; // A/D con2e&sion cloc+
ADCON2bits.ADCS1 = 1;
ADCON2bits.ADCS2 = 1;
&on't for%et to set the $4I* 'its correspondin% to your analo% input pins3

034SAbits.034SA0 = 1; // in,ut
$he last step is to ena'le the A:& module3

ADCON0bits.ADON = 1; // 5nable A/D module
Conclusion
8ow the A:& module on your PIC18!""# should 'e confi%uredH All that's left is to
actually sample the analo% input 'y settin% the ;7<&785 'it, waitin% for the
;7<&785 'it to 'e cleared, and save the result 'y readin% the A&45*H3A&45*6 pair
,or A&45*-.
7ne final thin% that should 'e noted is that the ma>imum recommended input impedance
is .."I@ . *o if you're usin% a 1#I or 1##I potentiometer, for instance, you need to put a
'uffer amplifier ,also called a Jline followerJ- 'etween the input pin and the si%nal
source.