CS-424, Lab # 6
Report due: 3-13-09


In this experiment you will work with the PIC18F452's analog to digital (A/D) converter.
You will use Bit RE2 of PORTE (which is brought out to the QuikProto board) for analog
input and detect and display, in binary, the output from a variable resistance voltage divider
circuit controlled by a potentiometer that simulates the voltage obtained from a robot sensor.
In addition you will use the same voltage divider in conjunction with your PWM motor system
from the last lab to vary the speed of a motor in accordance with the position of the
potentiometer (instead of using the switches), thus simulating the control of a robot's motor
according to the reading of an analog sensor.

As discussed in class, an analog to digital converter (ADC) is used to convert an analog
(continuously-varying) voltage to a binary number that represents the value of the voltage.
These circuits are very important in robotics, since many sensors used in robots output
analog voltages. The computer controlling the robot must "know" the value of these voltages
in order to be able to take appropriate action. Many robot sensors use variable resistors
wired in a voltage divider configuration to provide an analog voltage proportional to the
quantity being measured. For example, most temperature sensors use a variable resistor,
known as a thermistor, whose electrical resistence depends on the temperature. Figure 1
shows a potentiometer (a variable resistor also called a "pot") wired in a voltage divider
configuration. The output voltage of the circuit depends on the setting of the pot. In the
figure, if the voltage is tapped at the upper end, the output voltage will be V; if it is tapped
at the lower end, it will be 0. The voltage varies linearly as the tap is moved from the top
to the bottom. The position of the tap in the pot in your kit is varied by turning a knob on
its side. There are three leads coming out of a potentiometer. To configure a voltage
divider from a pot, one of the end leads of the pot is connected to a constant voltage (+5
volts from the QuikFlash board), the other end lead to ground, and the middle lead will
be the varying output voltage.

Figure 1.

Experimental procedure

Part 1

Wire up the voltage divider potentiometer circuit shown in Figure 1 on the QuikProto
board. The center lead of the pot will be attached to Bit E2 (ADC input), one end lead
to +5 volts (VDD), and the other end lead to ground (GND) from the QuikFlash
protoboard's expansion header. Write a program that will first configure Bit RE2 of
Port E for analog input. The configuration should be as in the example discussed in class.
Namely, you will be using only ADRESH to contain the result of the analog to digital
conversion (thus ignoring the least significant two bits of the converted value) and an
ADC oscillator frequency divide factor of 16 (which is appropriate for the clock rate on
the QuikFlash). The program should go into a loop that continually reads the converted
analog voltage and displays its binary value on the top row of the QuikFlash's Liquid
Crystal Display. You should observe that when the pot is rotated to its extreme
positions, something close to 00000000 and 11111111 will be displayed. Your
program should continually poll the GO_DONE bit in ADCON0 (as discussed in class)
to determine when an analog to digital conversion is completed. (If you prefer you may
use the AD conversion interrupt (PIR1:<ADIF-bit>) signal in conjunction with an
interrupt service routine you write as an alternative to polling.)

Part 2

In this part of the experiment you will use your motor circuit from Lab 5 (wired up on a
CS-210 prototype board). Once again Bit C4 of PORTC from the QuikProto board will
be wired to the base of the transistor in order to turn the motor on or off. In this lab you
will not be using switches to control the speed of the motor, instead the potentiometer
circuit you used in Part 1. Add some code to your program from Part 1 that will use the
most significant two bits of the converted analog value to control the motor as follows:

Most significant bit   Next most significant bit       Motor Speed
        0                            0                                  Motor off
        0                            1                                  PWM duty cycle = 5%
        1                            0                                  PWM duty cycle = 10%
        1                            1                                  PWM duty cycle = 100%

Your program should also light the center and right LEDs (connected to RA2 and RA1,
respectively) on the QuikFlash board according to the values of these two bits. In other
words, center LED RA2 should be on (off) when the most significant bit of the converted
analog value is on (off). In similar fashion right LED RA1 should track the state of the next
most significant bit of the converted number.

As usual, in your report you should submit circuit diagrams and listings (.LST and .HEX)
of your programs.