//*****************************************************************************
//
// File Name : adc.h
// Title : Analog-to-Digital-Converter functions
// Author : Smoerijf.be (Orinal by Pascal Stang - edit by smoerijf)
// Target MCU : Atmel AVR Series
//
//*****************************************************************************
//@{
#ifndef ADC_H
#define ADC_H
// defines
// A2D clock prescaler select
// *selects how much the CPU clock frequency is divided
// to create the A2D clock frequency
// *lower division ratios make conversion go faster
// *higher division ratios make conversions more accurate
#define ADC_P2 0x00 ///< 0x01,0x00 -> CPU clk/2
#define ADC_P4 0x02 ///< 0x02 -> CPU clk/4
#define ADC_P8 0x03 ///< 0x03 -> CPU clk/8
#define ADC_P16 0x04 ///< 0x04 -> CPU clk/16
#define ADC_P32 0x05 ///< 0x05 -> CPU clk/32
#define ADC_P64 0x06 ///< 0x06 -> CPU clk/64
#define ADC_P128 0x07 ///< 0x07 -> CPU clk/128
// do not change the mask value
#define ADC_PRESCALE_MASK 0x07
// A2D voltage reference select
// *this determines what is used as the
// full-scale voltage point for A2D conversions
#define ADC_AREF 0x00 ///< 0x00 -> AREF pin, internal VREF turned off
#define ADC_AVCC 0x01 ///< 0x01 -> AVCC pin, internal VREF turned off
//#define ADC_RSVD 0x02 ///< 0x02 -> Reserved
#define ADC_256V 0x03 ///< 0x03 -> Internal 2.56V VREF
// default value
// do not change the mask value
#define ADC_REFERENCE_MASK 0xC0
#define ADC_MUX_MASK 0x1F
// these channels supported only in ATmega128
// differential with gain
#define ADC_CH_0_0_DIFF10X 0x08
#define ADC_CH_1_0_DIFF10X 0x09
#define ADC_CH_0_0_DIFF200X 0x0A
#define ADC_CH_1_0_DIFF200X 0x0B
#define ADC_CH_2_2_DIFF10X 0x0C
#define ADC_CH_3_2_DIFF10X 0x0D
#define ADC_CH_2_2_DIFF200X 0x0E
#define ADC_CH_3_2_DIFF200X 0x0F
// differential
#define ADC_CH_0_1_DIFF1X 0x10
#define ADC_CH_1_1_DIFF1X 0x11
#define ADC_CH_2_1_DIFF1X 0x12
#define ADC_CH_3_1_DIFF1X 0x13
#define ADC_CH_4_1_DIFF1X 0x14
#define ADC_CH_5_1_DIFF1X 0x15
#define ADC_CH_6_1_DIFF1X 0x16
#define ADC_CH_7_1_DIFF1X 0x17
#define ADC_CH_0_2_DIFF1X 0x18
#define ADC_CH_1_2_DIFF1X 0x19
#define ADC_CH_2_2_DIFF1X 0x1A
#define ADC_CH_3_2_DIFF1X 0x1B
#define ADC_CH_4_2_DIFF1X 0x1C
#define ADC_CH_5_2_DIFF1X 0x1D
// compatibility for new Mega processors
// ADCSR hack apparently no longer necessary in new AVR-GCC
#ifdef ADCSRA
#ifndef ADCSR
#define ADCSR ADCSRA
#endif
#endif
#ifdef ADATE
#define ADFR ADATE
#endif
// function prototypes
void adcInit(u08 prescale, u08 ref);
void adcOff(void);
void adcSetPrescaler(u08 prescale);
void adcSetReference(u08 ref);
void adcSetChannel(u08 ch);
void adcStartConvert(void);
u08 adcIsComplete(void);
u16 adcGet(u08 ch);
u08 adcGet8(u08 ch);
#endif
//@}