the soldering iron.
I programmed the PIC using my
simple JDM style programmer
shown in Figure 4. I introduced it in
my last column and offer it through
my website elproducts.com This
little programmer running off the
ic-prog.exe freeware software really
works great.
resistor I knew would be tested
and created a range around it. For
example, look at the calculation
below for the 2K resistor.
[2K/(2K+10K)] * 1024 =
170 A/D counts
SOFTWARE
The software is written in
PICBasic Pro, which makes using the
A/D port very easy. PICBasic Pro has
an ADCIN command that handles all
the register setup and places the
result in a variable you designate
in the command line. Let’s step
through the code to see how it works.
The full code is shown in the Software
Listing, which is available on the
Nuts & Volts website ( www.nuts
volts.com).
The description at the top of
the program is preceded by single
quotes which indicates to PICBasic
Pro the line is a comment and not a
command.
The program code really begins
after the comments as the PICBasic
Pro DEFINEs are established for
the A/D converter. These DEFINEs
really set up the PIC’s internal
ADCON1 register. The analog-to-digital (A/D) converter is set up
for a 10 bit result, using the PIC’s
internal resistor-capacitor sample
timer at a sampling delay time of 50
microseconds before starting the
A/D conversion.
The variable that will store the 10
bit result needs to be set up as a
word and we do that in the line
shown here.
checking is done. After the label
“loop:” the ADCIN command is
issued to start the A/D conversion
process at the A0 pin and store the
10-bit result (as set up earlier) in the
variable “adval.”
The value of the “adval” variable
is then tested to see which LED to
light. Since we know the resistors
being checked, each If-Then statement below looks for the range the
“adval” must be within. If it matches,
Port B is set up to turn off all
the LEDs except the proper one
that indicates which tray to put the
resistor in.
If the resistance measured is
greater than 1,000 A/D counts, then
all the LEDs are turned off. This is
how we keep the LEDs off when no
resistor is in the probes.
Finally, the program delays for
half of a second and then tests the
resistance again by using the GOTO
command to jump to the “loop:”
label.
I wanted to explain the A/D
counts in a little more detail before
the software explanation. You see an
A/D port measures voltage, not resistance. Therefore, I created a resistive
ladder using the 10K pull-up and the
resistor under test as the pull-down.
The creates a voltage between 0– 5
volts, depending on the resistor
under test.
The mathematical formula used
to come up with the A/D counts (0-
1,024) is below:
To allow for tolerance variation
from resistor to resistor, I made the If-Then command range 150 to 200 A/D.
If the value of “adval” is between
these values, then the second LED is
lit indicating that tray is where the
resistor belongs.
NEXT STEPS
Obviously, I did not design it for
resistors other than the ones I
planned to see. If a 1K resistor
somehow found its way on to the
probes, none of the ranges would
match and no LEDs would light up.
This would look like it’s not working
so she would just put it in a scrap
pile for me to go through later.
The probes were a little touchy so
sometimes the lit LED would jump
back and forth between two values
but eventually settle down at the
proper one. Spring clips would
probably work better for probes.
CONCLUSION
5V * [Rtest / (Rtest + 10K)] *
[1024/Vref] = A/D counts
In this setup, Vref comes from the
internal PIC 5V line so the equation
adval var word
‘ Create adval to store result
Now the ports are initialized by
writing directly to the data direction
TRIS registers to set them up properly
as inputs (1) or outputs (0).
The LEDs connected to Port B are
then tested by turning all of Port B
high then low with a one second delay
in-between.
The Main loop of code is next
and this is where all the resistor
74 February 2006
reduces to the formula:
[Rtest / (Rtest + 10K)] * 1024 =
A/D counts
This project shows just how
handy being able to program a PIC
can be. You can design custom test
equipment or custom control
modules with very little effort using a
higher level language and a few
simple software and hardware tools.
Hopefully, you found this article
interesting. Let me know what you
think and maybe suggest a few ideas
you would like me to tackle in a
future article. Just email me at
chuck@elproducts.com or visit my
website’s forum under the “Forum”
link at www.elproducts.com NV
If the resistor under test is very
large, then the A/D count will be close
to 1,024. If the resistor is very small
then, the A/D count will be close to
zero. I calculated the A/D for each
To view or download the
complete Software Listing,
please go to the Nuts & Volts
website at
www.nutsvolts.com