# adc_hardware

ADC hardware, built-in PIC ADC module

Author | Stef Mientki, Copyright © 2002..2008, all rights reserved.
Adapted-by | Sebastien Lelong.
Compiler | >=2.4g

Description

    library to use the PIC's AD converter
    See 16F87x datasheet (DS30292C, p112)
    Number of channels, behind "--" yields only for 16F874 and 16F877
    No external Vref: Nchan = 1,3,5 -- 6,8
    Only external +Vref: Nchan = 2,4 -- 5,7
    Both external +Vref and - Vref: Nchan = 1,2,3 -- 4,6

    Analog channels can still be used as digital outputs, by setting
        pin = output

    relevant IO pins
     * RA0 = AN0
     * RA1 = AN1
     * RA2 = AN2  or  -Vref
     * RA3 = AN3  or  +Vref
     * RA4 = no analog IO
     * RA5 = AN4
     * next pins only for 16F874 and 16F877
     * RE0 = AN5
     * RE1 = AN6
     * RE2 = AN7

    Example PIC-AD

     (if you use JALcc, you can copy the complete example below and "uncomment" it)

     define the AD converter settings
    const ADC_hardware_Nchan      = 2         ;number of selected channels
    const ADC_hardware_NVref      = 0         ;number of external references
    const ADC_hardware_Rsource    = 10_000    ;maximum source resistance
    const ADC_hardware_high_resolution = true ;true = high resolution = 10 bits
                                              ;false = low resolution = 8 bits

     get the library, after defining the constants
    include adc_hardware

     initialize the AD converter according to the above parameters
    ADC_init

     now take a sample

     read high resolution, channel 0
    var_word=adc_read(0)

     read low resolution, channel 0
    var_byte=adc_read_low_res(0)

Sources

    I (Stef) mixed the following libraries with my own ideas
     * f877_modules.jal from Javier Martinez
     * janalog.jal from Vasile Surducan

Dependencies

- delay

## Summary

### Global variables/contants

- `var volatile byte _adc_acquisition_time`
- `var volatile byte _adcon0_shadow = 0`

### Procedures

- `adc_read_bytes(byte in adc_chan, byte out adc_hbyte, byte out adc_lbyte)`
- `adc_on()`
- `adc_init()`

Private

- `_adc_read_low_res(byte in adc_chan, byte out adc_byte)`
- `_adc_init_2_vref()`
- `_adc_init_1_vref()`
- `_adc_init_no_vref()`
- `_ad_init_general()`

### Functions

- `adc_read(byte in adc_chan) return word`
- `adc_read_low_res(byte in adc_chan) return byte`

## API details

### Global variables/contants

- `var volatile byte _adc_acquisition_time`

  No documentation found

- `var volatile byte _adcon0_shadow = 0`

  No documentation found

### Procedures

- `adc_read_bytes(byte in adc_chan, byte out adc_hbyte, byte out adc_lbyte)`

      Does an AD conversion on the selected channel
      returns the 10-bit result as 2 byte parameters
      It doesn't matter if ADC is set to low or high resolution

- `adc_on()`

      Turns all ADC channels back on

- `adc_init()`

      Initializes the PIC AD-converter,
      by calling the correct routine according to the number of external references
      Settings are done according to the special ADC constants
      Sets all the analog pins to input
      Calculates aquisition time and determines if right or
      left justification is optimal

Private

- `_adc_read_low_res(byte in adc_chan, byte out adc_byte)`

      Does an AD conversion with low resolution (8 bit) on the selected channel

- `_adc_init_2_vref()`

      Initializes the PIC AD-converter, when both +Vref and -Vref are present
      Settings are done according to the special ADC constants
      Sets all the analog pins to input
      Calculates aquisition time and determines if right or
      left justification is optimal

- `_adc_init_1_vref()`

      Initializes the PIC AD-converter, when only +Vref is present
      Settings are done according to the special ADC constants
      Sets all the analog pins to input
      Calculates aquisition time and determines if right or
      left justification is optimal

- `_adc_init_no_vref()`

      Initializes the PIC AD-converter, when no external Vref is present
      Settings are done according to the special ADC constants
      Sets all the analog pins to input
      Calculates aquisition time and determines if right or
      left justification is optimal

- `_ad_init_general()`

      Internal routine, that
      calculates aquisition time
      and determines if right or left justification is optimal

### Functions

- `adc_read(byte in adc_chan) return word`

      Does an AD conversion on the selected channel
      returns the result as a 10-bits word
      It doesn't matter if ADC is set to low or high resolution

- `adc_read_low_res(byte in adc_chan) return byte`

      Returns Low Resolution ADC-value as the RESULT
      It doesn't matter if ADC is set to low or high resolution

## Related samples

Here are the list of samples which use this library:

16f1455 | 16f1455_usb_hid_generic.jal
18f14k50 | 18f14k50_usb_hid_generic.jal
18f2450 | 18f2450_usb_hid_generic.jal
18f2550 | 18f2550_usb_hid_generic.jal
18f4550 | 18f4550_usb_hid_generic.jal
18f67j50 | 18f67j50_usb_hid_generic.jal