-
PIC12C671 A/D converter part3
About Tad & Selecting the A/D Conversion Clock:
The A/D conversion time per bit is defined as Tad. The A/D conversion requires 9.5 Tad per 8-bit conversion. The source of the A/D conversion clock is software selected. The four possible options for Tad are:

2Tosc. 
8Tosc. 
32Tosc. 
Precision internal 4 MHz oscillator. Important Note: For correct A/D conversions, the A/D conversion clock (Tad) must be selected
The datasheet shows the Tad vs. Device Operating Frequencies in the chart below. Using this chart it’s easy to select the correct bit-values to configure the A/D conversion clock required for the oscillator value you’re planning to use. I got a little lazy and decided to use a screen capture from the datasheet instead of creating a table. Here’s how to use it:
The following table shows the resultant Tad times derived from the device operating frequencies and the A/D clock source selected.

Tad vs. Device Operating FrequenciesAs shown above, if you’re using the internal ADC RC oscillator, you need to set the ADCON0 bits ADCS1, ADCS0 to 11. Using the internal ADC clock is only desirable when running the PIC at low clock frequencies to conserve power. If you’re using a 4MHz ceramic resonator, crystal or other clock source, you’ll need to set these bits to either 01, or 10.
Let’s dive right into some sample code to configure the A/D inputs and acquire an analog value. We will take this analog value and send it to the PC serial port using one of the I/O-pins as a serial output. This example will assume the following parameters:

We are using the internal precision 4MHz oscillator to conserve I/O-pins.

We want to use GP0 as our analog input pin (A/D channel-0).

We want to use GP1 as our serial output-pin to the PC.
Following along with the datasheet for the PIC12C671, we’ll need to perform the following sequence to achieve our goals:
The following steps should be followed when doing an A/D conversion:
-
Configure the A/D module:

Configure analog pins / voltage reference / and digital I/O (ADCON1).

Select A/D input channel (ADCON0).

Select A/D conversion clock (ADCON0).

Turn on A/D module (ADCON0).
-
Configure the A/D interrupt (if desired):

Clear ADIF bit.

Set ADIE bit.

Set GIE bit.
-
Wait the required acquisition time.
-
Start conversion.

Set GO/DONE bit (ADCON0).
-
Wait for A/D conversion to complete, by either:

Polling for the GO/DONE bit to be cleared.

Waiting for the A/D interrupt.
-
Read the A/D result register (ADRES), clear bit ADIF if required.
-
For the next conversion, go to step 1 or step 2 as required. The A/D conversion time per bit is defined as Tad. A minimum wait of 2Tad is required before the next acquisition starts.
Looks pretty complicated doesn’t it..? It’s really not. Here’s how we do it with PicBasic:
'------------------------------------------------------------------------- ' PIC12C671 A/D SAMPLE ROUTINE FOR TESTING '------------------------------------------------------------------------- ' PIN NAME USE/CONNECTION ' 1 Vdd +5VDC ' 2 GPIO.5 N/C (NO CONNECTION) ' 3 GPIO.4 N/C ' 4 GPIO.3 N/C ' 5 GPIO.2 N/C ' 6 GPIO.1 SERIAL OUTPUT TO PC ' 7 GPIO.0 1K RESISTOR TO CENTER OF 5K POT ' 8 VSS GROUND '------------------------------------------------------------------------- DEFINE OSCCAL_1k 1 'SET OSCCAL FOR PIC12C671 RESULT VAR BYTE 'A/D CONVERSION RESULT STORAGE BYTE ADCON1 = 6 'SET GP.0 TO ANALOG INPUT, Vref = Vdd, ALL ELSE = DIGITAL I/O MAIN: GOSUB GETVAL 'SETUP A/D PAUSE 1000 'PAUSE FOR 1 SECOND SEROUT GPIO.1,N2400,[RESULT] 'SEND A/D CONVERSION RESULT TO THE PC SERIAL PORT PAUSE 1000 'PAUSE FOR 1 SECOND GOTO MAIN GETRESULT: PAUSEUS 50 'WAIT FOR A/D CHANNEL SETUP (50 uS) ADCON0.2 = 1 'START CONVERSION BY SETTING GO/DONE-BIT (HIGH) LOOP: 'WAIT FOR GO/DONE BIT TO BE CLEARED IF ADCON0.2=1 THEN 'CHECK GO/DONE BIT (0=DONE) GOTO LOOP 'NOT FINISHED CONVERTING, THEN LOOP UNTIL DONE ENDIF 'IF STATEMENT SATISFIED, ADCON0.2 = 0 (DONE) RETURN 'FINISHED CONVERSION, RETURN TO GETVAL ROUTINE GETVAL: ADCON0=%10000001 'SET A/D Fosc/32,[4MHz], CHANNEL-0 = ON GOSUB GETRESULT 'START THE CONVERSION RESULT = ADRES 'STORE THE A/D CONVERSION VALUE IN RESULT RETURN 'RETURN TO MAIN ROUTINE, START OVER END 'END PROGRAM
A/D Results

Here’s the simple schematic to use for this experiment:We setup channel-0 as the A/D input, read the analog value present on channel-0, and sent the analog value to the PC serial port through pin GP1. Using PicBasic we can easily access & use the onboard A/D converter of the PIC12C671. Using the onboard 4 MHz oscillator to free-up the two extra I/O-pins is simple with the command DEFINE OSCCAL_1K 1.
This whole application looks pretty mundane until you realize what you can do with the A/D converter. There are several very useful projects we can create using this small 8-pin PIC. Here are just a few ideas:

A light-following Robot. Using up to 5 photo resistors, we can create a Robot that follows light by turning towards the largest value returned from each A/D conversion. This could easily be integrated with another PIC, or even the BASIC Stamp by sending the A/D conversion values serially to the other PIC, or Stamp. 
We could do just the opposite, and create a Robot that avoids the light, and seeks darkness by turning towards the lowest A/D value based on input from up to 5 photo resistors.. 
A plant positioning system that controls a servo motor based on the input from the A/D conversion. The servo motor is turned to face the plant towards the highest value, thus positioning the plant where it always has the brightest sunshine directly on it. 
A voltage monitoring system that warns us when the voltage drops below, or goes above, pre-determined limits. 
A simple on/off switch based on the amount of light striking a CDS-Cell. 
A serial voltage meter that displays the voltage on your PC screen. 
A watering system for the yard that cuts-off the water during peak sunlight, and turns in back on during low light levels. 
A circuit that activates a back-up power supply when low voltage is sensed. 
Low-voltage, battery operated, low-battery warning systems. 
Movement detection systems based on light-beam interference. Have you ever seen a toy that started talking when you approached it..? Food for thought..! These are only a few ideas, but the applications are virtually endless.
That’s it for using the PIC12C671 A/D converter. This example showed how to use A/D channel-0 (GP0), but it should help you understand how to configure the ADCON0, ADCON1 A/D configurations registers, and take advantage of the 8-pin, PIC12C67X series microcontrollers. These are powerful little microcontrollers and understanding how to set them up by configuring the internal registers makes integrating them into your projects simple & fun. The first article in this series shows how to configure the onboard oscillator, and gives details of how it works. Click HERE to see the first article in this series.

Click HERE to purchase PicBasic Compilers – and hardware.

Click HERE to learn more about the PicBasic compiler.
source http://www.rentron.com
-


Recent Comments