5.2. ADCIN
ADCIN Channel,Var
Read the on-chip analog to digital converter Channel and store the
result in Var. While the ADC registers can be accessed directly, ADCIN
makes the process a little easier.
Before ADCIN can be used, the appropriate TRIS register must be set to
make the desired pins inputs. The ADCON, ANCON and/or ANSEL
registers must also be set to assign the desired pins to analog inputs and
in some cases to set the result format and clock source (set the clock
source the same as the DEFINE specified for it, below). See the
Microchip data sheets for more information on these registers and things
like the clock source and how to set them for the specific device. Note:
The PIC14000 ADC is not compatible with the ADCIN instruction.
Depending on the device, it may have an 8-, 10- or 12-bit ADC. For
many PIC MCUs, the high bit of ADCON0 or ADCON1 controls whether
the result is left or right justified. In most cases, 8-bit results should be
left justified (ADCON1.7 = 0) and 10- and 12-bit results should be right
justified (ADCON1.7 = 1).
Several DEFINEs may also be used. The defaults are shown below:
DEFINE ADC_BITS 8 ‘ Set number of bits in result (8, 10 or 12)
DEFINE ADC_CLOCK 3 ‘ Set clock source (rc =3)
DEFINE ADC_SAMPLEUS 50 ‘ Set sampling time inmicroseconds
ADC_SAMPLEUS is the number of microseconds the program waits
between setting the Channel and starting the analog to digital
conversion. This is the sampling time.
TRISA = 255 ‘ Set PORTA to all input
ADCON1 = 0 ‘ PORTA is analog
ADCIN 0, B0 ‘ Read channel 0 to B0