Sie sind auf Seite 1von 10

Ezine: http://www.best-microcontroller-projects.

com
Measuring Resistance Using Digital I/O
Using a Microcontroller for
Measuring Resistance Without using an ADC.
Copyright 2008 John Main
Get more bulletins just like this one:
Click Here.
http://www.best-microcontroller-projects.com/
Ezine: http://www.best-microcontroller-projects.com Page 1 o 1!
Ezine: http://www.best-microcontroller-projects.com
Table of Contents
Legal........................................................................................................
Resources................................................................................................. !
"ssential "#ine........................................................................................ !
$%C C Course.......................................................................................... !
$%C %nterrupt &ecrets............................................................................... '
&tate Machine &ecrets............................................................................. '
Measuring Analogue $ara(eters With )o ADC ............................................... *
Measuring A Resistance........................................................................... *
$C Joystic+ Metho,.................................................................................. -
Cali.ration............................................................................................. 8
Con/ersion............................................................................................. 8
Applie, to the $%C................................................................................... 8
0peration.............................................................................................. 1
R&22 &ettings..................................................................................... 20
R&22 output....................................................................................... 20
De/ice................................................................................................. 20
Co(piler.............................................................................................. 22
Co,e Do3nloa,..................................................................................... 22
Ezine: http://www.best-microcontroller-projects.com Page " o 1!
Ezine: http://www.best-microcontroller-projects.com
Rights
NOTICE: You DO H!e the "i#ht $i!e %w& This "eport'
%s lon# s &ou (on)t chn#e n&thin#' use *+%, or #enerll& (o
somethin# &ou)re not suppose( to (o.
- Cop&ri#ht .//0 1ohn ,in
http://www.best-microcontroller-projects.com
%ll ri#hts reser!e(. No prt o2 this e3oo4 m& be repro(uce(' store(
in retrie!l s&stem or trnsmitte( in n& 2orm or b& n& mens
electronic' mechnicl' photocop&in#' recor(in# or otherwise without
e5press written' (te( n( si#ne( permission 2rom the uthor 1ohn
,in.
Legal
DISCLAIMER AND/OR LEGAL NOTICES:
4er(s of use5
%ll so2twre' hr(wre n( in2ormtion 2oun( in this eboo4 or
ssocite( with it shll be here2ter re2erre( to s the pro(uct. %ll
persons usin# or ctin# on the pro(uct shll be here2ter re2erre( to
s the user.
3& usin# the pro(uct the user #rees to bi(e b& the 2ollowin#
con(itions.
Disclimer:
%ll pro(ucts re pro!i(e( 6s is6 n( without wrrnt& o2 n& 4in(' either e5presse( or
implie(. The entire ris4 s to the 7ulit& n( per2ormnce o2 the pro(uct is borne b& the
user.
*houl( the pro(uct pro!e (e2ecti!e in n& respect' the user will ssume the entire cost o2 n&
ser!ice n( repir n( will ssume 2ull libilit& 2or n& in(irect' specil' inci(entl or
conse7uentil (m#es risin# out o2 the use o2 or inbilit& to use the pro(uct' inclu(in#'
without limittion' (m#es 2or loss o2 #oo(will' wor4 stopp#e' computer 2ilure or
ml2unction' or n& n( ll other commercil (m#es or losses' e!en i2 (!ise( o2 the
possibilit& thereo2' n( re#r(less o2 the le#l or e7uitble theor& 8contrct' tort or
otherwise9 upon which the clim is bse(.
Ezine: http://www.best-microcontroller-projects.com Page # o 1!
Ezine: http://www.best-microcontroller-projects.com
Resources
PIC C Course
A Co(plete C course for $%C (icros in 22 parts.
CL%C6 the i(age.
0r Clic+ 7ere
PIC Interrupt Secrets
7o3 to Use $%C %nterrupts to Ma8i(u( effect.
CL%C6 the i(age.
0r Clic+ 7ere.
State Machine Secrets.
7o3 to use state (achines to create Solutions to
co(ple8 pro.le(s inclu,ing fully ,e.ugge, e8a(ples 9
one is co,ing a scrolling (enu on a stan,ar, LCD
,isplay.
CL%C6 the %(age
0r Clic+ 7ere.
Ezine: http://www.best-microcontroller-projects.com Page $ o 1!
Ezine: http://www.best-microcontroller-projects.com
Measuring Analogue Parameters With No ADC
7ere:s a tric+ % ha/e .een (eaning to try an, it coul, .e useful for you if you
either run out of ADC inputs or your ,e/ice ,oesn:t ha/e one in the first place. %t:s
not a true analogue (easure(ent .ut allo3s you to (easure a resistance that
results in an analogue /oltage e.g. co(ponents that /ary their resistance in
response to a physical para(eter.
;uite often you 3ill .e using co(ponents that change their resistance ,epen,ing
on a physical para(eter e.g. te(perature <4CR=> hu(i,ity> Light <LDR=>
potentio(eter <$04= an, the (etho, ,escri.e, here 3ill let you (easure their
resistances 3ithout using an ADC.
4he i,ea is not ne3 since it 3as first use, on $Cs to i(ple(ent /ery cheap
analogue input for ?oystic+s using only ,igital inputs.
A single ?oystic+ is (a,e fro( t3o orthogonal potentio(eters 200+@!-0+ an, the
tric+ is getting the /alue fro( the pots into the $C since> at the ti(e> ADCs
3ere tons of (oney so there 3as no 3ay they 3oul, (anufacture a ?oystic+ 3ith
ADCs @ ?ust for ga(e playingA
%t:s ,one using capacitors.
Measuring A Resistance
As any engineer 3ill tell you their first e8perience of <,ifficult at the ti(e=
eBuations is 3hen you sol/e the ,ifferential eBuations for charging an,
,ischarging a capacitor an, you figure out the RC ti(e constant 4or.
4hat ti(e constant is the +ey since if you +eep the capacitance the sa(e an, /ary
the resistance then the ti(e ta+en for charging the capacitor 3ill /ary in
proportion to the /alue of the resistance.
%f you choose the capacitance an, (a8. pot /alue appropriately then you can
cycle through (easure(ents Buic+ly enough <for ga(e playing= so that it appears
that the pot is continuously rea,.
All you nee, is a 3ay of ,ischarging the capacitor an, then apply a +no3n /oltage
to the capacitor /ia the pot.
Ezine: http://www.best-microcontroller-projects.com Page % o 1!
Ezine: http://www.best-microcontroller-projects.com
The Basic PC Joystick Interface
<rea,s one of the four potentio(eters @
so(e ga(e pa,s ha/e t3o ?oystic+s=
PC Joystick Method
4his is the 3ay that the $C ?oystic+ 3as rea, <although no3 (ore relia.le
(etho,s are use,= an, it e(ploys a (onosta.le 3hich 3hen triggere, ,ischarge,
the capacitor. 4he (onosta.le then sets its output lo3 until the capacitor /oltage
reaches a threshol, at 3hich point the output goes high.
4herefore the ti(e ta+en for the capacitor to charge is proportional to the /alue
of the pot so the /alue of the pot can .e esti(ate,. Cy (easuring the ,uration of
the (onosta.le signal you o.tain a /alue that is proportional to the /alue of the
resistance.
Dor the $C this 3as not a spectacular success for the follo3ing reasons5
$C co(puter see!s 3ere ,ifferent fro( each other.
$ort interfaces 3ere ,ifferent fro( each other.
$ro.le(s 3ith OS ,ifferences.
Cig tolerance ,ifferences in co(ponents <,ifferent ?oystic+ (anufacturers=.
Co(puter intensi/e olling of the port.
4a+es a long ti"e to reach (a8 pot /alue.
...an, although the ?oystic+ 3or+e, it reBuire, cali.ration for each co(puter an,
each ga(e> an, reBuire, slightly ,ifferent soft3are to cope 3ith ,ifferent $C
har,3are an, 0&.
Ezine: http://www.best-microcontroller-projects.com Page & o 1!
Ezine: http://www.best-microcontroller-projects.com
4his gi/es you an i,ea of 3hy this (etho, is not for co((ercial usage.
Cut you 3on:t ha/e this huge pro.le( .ecause you 3ill )04 .e running the
soft3are on a (ulti@purpose platfor( so all you shoul, .e concerne, a.out is the
co(ponent tolerances < capacitorEpotentio(eter change 3ith te(perature an,
ti(e <aging= = an, po3er supply tolerance.
Note: You may need to calibrate depending on your application.
Caliration
0ne pro.le( is that you (ay nee, to cali.rate the syste(. An, this is for t3o
reasons5
2. Un+no3n inaccurate po3er supply.
2. %naccurate potEcapacitance.
Note: Just remember to check and test and don't use this technique in a
commercial system (where you run out thousands of boards because you will get
into trouble) alternati!ely reduce the resolution required e.g. for a "# system
measure $"%&"'%&("%&)''% of the supply etc. then the tolerance will be less
important.
Con!ersion
Although the /alue is proportional it is on an e8ponential cur/e i.e. it:s not linear
so you nee, to use a ta.le to con/ert it into resistance /alues or use a calculation
in the co,e to con/ert fro( e8ponential.
Applied to the PIC
4he ,ifference .et3een the $%C an, the $C is that the $%C ,igital pin can .e
configure, as an input or as an output on the fly <so you can ,ischarge the
capacitor .y setting the pin as an output an, at logic lo3= an, then set it as an
input an, .y using the stan,ar, $%C input /oltage le/els <2F @ logic high= as the
threshol, you ,on:t nee, any e8tra %Cs.
Ezine: http://www.best-microcontroller-projects.com Page ' o 1!
Ezine: http://www.best-microcontroller-projects.com
"peration
4he proce,ure for using it is5
2. Change the pin to an output.
2. &et the output lo3 <to ,ischarge the capacitor= for G2(s.
. &et the pin to an input.
!. &tart the ti(er.
'. &top the ti(er 3hen the input goes high.
*. Rea, out /alue fro( ti(er 3hich is proportional to resistance.
Cy changing the ,igital input to an output <3ith the output set lo3= the capacitor
3ill .e ,ischarge, to #ero current li(ite, to 'FE220 H 2(A. <you coul, pro.a.ly
get a3ay 3ithout using the 220R since the internal pin circuitry li(its the current
sin+ to 2'(A @ actually % forgot it 3hen testing an, it 3or+s fine 3ithout it @ it:s
really a protection if the pot is set to #ero resistance @ % ?ust teste, it using a
selection of resistors so % ,i, not ha/e that pro.le(=.
4hen changing the pin to an input @ it effecti/ely .eco(es isolate, fro( the circuit
since /ery little current is ,ra3n .y an input pin @ the capacitor 3ill charge fro(
nearly #ero <nearly #ero .ecause of the 220R resistor @ the input can:t Buite reach
#ero @ this 3ill .e lo3 enough to register as logic #ero= to 'F /ia the
potentio(eter. As the input /oltage crosses the 2F <ish= threshol, for logic high
the $%C input 3ill .e logic 2.
Note: *or a more accurate (repeatable) threshold choose a +chmitt trigger input
or use an internal comparator.
Measuring this ti(e fro( reset to logic 2 3ill gi/e a /alue proportional to the
/alue of the potentio(eter <one ar( of the pot H /aria.le resistor=.
Dor the 200+ I 20n co(ponents 4or H 2(s 3hich is *J of the ti(e to charge
Ezine: http://www.best-microcontroller-projects.com Page ( o 1!
Ezine: http://www.best-microcontroller-projects.com
to the final /oltage an, this gi/es so(e i,ea of the or,er of ti(e to .e ,etecte,
.y the (icrocontroller.
4he progra( outputs t3o /alues5
A confi,ence loop counter <?ust a /aria.le that incre(ents=
4he 4i(er2 /alue.
Note: ,f you use #ista putty.e-e (google for it) is a suitable replacement for
hyperterminal. .yperterminal was remo!ed from #ista/
RS#$# Settings
Kou shoul, set hyperter(inal or putty to the follo3ing settings5
Cau, 2!00
Data Cits 8
$arity )one
&top .its 2
Dlo3
control
)one
RS#$# output
4he ter(inal output loo+s li+e this5
1'
22'
4he upper /alue is the confi,ence output an, lo3er is 4i(er2 /alue. 4his is +ept
at the sa(e position on the screen using F4200 co,es <% really hate the screen
scrolling continuously @ then again there:s no history=.
Something to Notice
%f you re(o/e the resistance co(pletely then the co,e is al3ays 3aiting for the
capacitor to charge an, it ne/er 3illA 4his is an e8a(ple of the type of pro.le(
that can .e create, .y an unforeseen circu(stance. Although this e8a(ple is
Ezine: http://www.best-microcontroller-projects.com Page ) o 1!
Ezine: http://www.best-microcontroller-projects.com
tri/ial it ,e(onstrates that if you ,on:t allo3 for e/ery ,ifferent e8ternal or
internal state the co,e coul, hang. 4he co,e is fore/er stuc+ unless the state
changes. %n this case it ,oes not (atter .ecause the co,e is a ,e(onstration an,
not critical .ut the 3ay aroun, it is to use the 3atch,og ti(er to force a reset.
Cy changing the co,e i.e. using a (ore co(ple8 pro?ect ha/ing a user interface it
3oul, .e possi.le to allo3 the user to stop the test or put up a 3arning (essage.
At the /ery least it 3oul, allo3 the user to interact 3ith the syste( .efore it gets
stuc+ again.
De!ice
4he ,e/ice use, is the 22D*-' <3ith the ADC turne, off= this is ?ust a ,e/ice %
ha, han,y .ut you can run the co,e on any $%C chip.
Compiler
4he co(piler use, is the Mi+roC co(piler 3hich lets you use it free for the 2
st
2+
7e8 output.
Code Download
Do3nloa, the co,e here5 Clic+ 7ere 4o Do3nloa,
Ezine: http://www.best-microcontroller-projects.com Page 1! o 1!