Alternate Analog to Digital Unit

The library unit for handling the analog-to-digital converter (ADC) within most PIC chips is quite powerful. It gives full control over all of the parameters.

Changes


But these are somewhat daunting for newcomers to figure out. So, I decided to add two new commands to set things up automatically for most general situations. So while it is no doubt the case that one can optimize the ADC with the original library unit commands, these new guys will handle common applications quite well and do so easily.

The new commands are  ReadADC8() and ReadADC10(), which return 8- and 10-bit results from the ADC, respectively.

Commands


Here are all the commands, both old and new, listed for your convenience. As usual, the descriptions come from the source code, originally penned by PMP's author, Philippe Paternotte. If it isn't clear by now, always go to the source code itself for full details.

procedure A2D_Init(A2D_Channels : tA2D_Channels;
                                    A2D_Acqt : tA2D_ACQT;
                                    A2D_Clock : tA2D_Clock;
                                    A2D_Mode : tA2D_Mode;
                                    A2D_Vref_Mode : tA2D_Vref_Mode);

where:

A2D_Channels: The mask of all used channels for most of processors, or it is a special code for others. See datasheets for ADCON1.PCFG field.

A2D_Acqt: The acquisition time. For PIC18 processors it is the TAD code; for other processors it is the acquisition time (number of µS to wait after channel change before starting conversion).

A2D_Clock: The clock mode to use (conversion speed) which depends on     processor family/model.

A2D_Mode: Data width to use: 8-bit, 10-bit,... Mainly this controls the left/right justification bit ADFM. Since left justification has no real usage, this library uses it to get only the 8 most significant bits of the result.

A2D_Vref_Mode: Defines the Vref+ and Vref- that should be used as A2D    reference; depends on processor family/model.

The initialization procedure returns with A2D left ON.

function A2D_Get(Channel : byte) : tA2D_Result;

Get a channel value. Selects the channel, waits for acquisition time, then performs the A2D conversion and returns a right justified 16-bit word value whether the selected mode is 8-bit or not. This function does not wait if the ADC is already on and the currently selected channel has already been requested.  It activates ADON automatically but does not clear it.

function A2D_GetWait(Channel : byte; A2D_Acqt : byte) : tA2D_Result;

Get a channel value, with dynamic acquisition time. Selects the channel, waits the given acquisition time (in µs), then performs the A2D conversion and returns a right justified 16-bit word value whether the selected mode is 8-bit or not. This function is to be used when the acquisition time is equal to adtTAD_0 in the A2D_Init call. It does not change the setup made by the A2D_Init call, and does not use the acquisition time setting in that procedure. This function also does not wait if the ADC is on and the currently selected channel has already been requested. It activates ADON automatically but does not clear it.

function ReadADC8(A2D_Channels : byte) : byte;

This is a basic analog-to-digital function suitable for beginners. It requires no special setup; simply read the ADC value and that's that. It sets things up with reasonable default values for many situations. The only parameter is that which determines which analog pin to read. ANS0, ANS1, ANS2, etc. The result is an 8-bit byte.

You don't need to know all this, but a 20 uS acquisition time, a clock divisor based upon the system clock, an-8 bit result and the full supply voltage range are used. 

function ReadADC10(A2D_Channels : byte) : word;

This is a basic analog-to-digital function suitable for beginners. It requires no special setup; simply read the ADC value and that's that. It sets things up with reasonable default values for many situations. The only parameter is that which determines which analog pin to read. ANS0, ANS1, ANS2, etc. The result is a 10-bit word.

You don't need to know all this, but a 20 uS acquisition time, a clock divisor based upon the system clock, a-10 bit result and the full supply voltage range are used.

Add It to Your Library Collection


And here's the new library unit, "A2D_alt.pas" for you to download:


Be sure to read over the source code for additional details on how to use it.

Also note that the lone parameter passed to these functions is something like ANS0, not AN0, A0 or even 0.

Test It Out


Here's a demo I've put together to try out the alternate versions of both the LCD and A2D library units. Use the following schematic:


It simply reads the pot settings (both 8- and 10-bit values) and prints them to the LCD. You can fetch the source code for the experiment here:

No comments:

Post a Comment