| 1145 | kaklik | 1 | /*! \file a2d.h \brief Analog-to-Digital converter function library. */ | 
      
        |  |  | 2 | //***************************************************************************** | 
      
        |  |  | 3 | // | 
      
        |  |  | 4 | // File Name	: 'a2d.h' | 
      
        |  |  | 5 | // Title		: Analog-to-digital converter functions | 
      
        |  |  | 6 | // Author		: Pascal Stang - Copyright (C) 2002 | 
      
        |  |  | 7 | // Created		: 4/08/2002 | 
      
        |  |  | 8 | // Revised		: 4/30/2002 | 
      
        |  |  | 9 | // Version		: 1.1 | 
      
        |  |  | 10 | // Target MCU	: Atmel AVR series | 
      
        |  |  | 11 | // Editor Tabs	: 4 | 
      
        |  |  | 12 | // | 
      
        |  |  | 13 | // This code is distributed under the GNU Public License | 
      
        |  |  | 14 | //		which can be found at http://www.gnu.org/licenses/gpl.txt | 
      
        |  |  | 15 | // | 
      
        |  |  | 16 | ///	\ingroup driver_avr | 
      
        |  |  | 17 | /// \defgroup a2d A/D Converter Function Library (a2d.c) | 
      
        |  |  | 18 | /// \code #include "a2d.h" \endcode | 
      
        |  |  | 19 | /// \par Overview | 
      
        |  |  | 20 | ///		This library provides an easy interface to the analog-to-digital | 
      
        |  |  | 21 | ///		converter available on many AVR processors.  Updated to support | 
      
        |  |  | 22 | ///		the ATmega128. | 
      
        |  |  | 23 | // | 
      
        |  |  | 24 | //**************************************************************************** | 
      
        |  |  | 25 | //@{ | 
      
        |  |  | 26 |  | 
      
        |  |  | 27 | #ifndef A2D_H | 
      
        |  |  | 28 | #define A2D_H | 
      
        |  |  | 29 |  | 
      
        |  |  | 30 | // defines | 
      
        |  |  | 31 |  | 
      
        |  |  | 32 | // A2D clock prescaler select | 
      
        |  |  | 33 | //		*selects how much the CPU clock frequency is divided | 
      
        |  |  | 34 | //		to create the A2D clock frequency | 
      
        |  |  | 35 | //		*lower division ratios make conversion go faster | 
      
        |  |  | 36 | //		*higher division ratios make conversions more accurate | 
      
        |  |  | 37 | #define ADC_PRESCALE_DIV2		0x00	///< 0x01,0x00 -> CPU clk/2 | 
      
        |  |  | 38 | #define ADC_PRESCALE_DIV4		0x02	///< 0x02 -> CPU clk/4 | 
      
        |  |  | 39 | #define ADC_PRESCALE_DIV8		0x03	///< 0x03 -> CPU clk/8 | 
      
        |  |  | 40 | #define ADC_PRESCALE_DIV16		0x04	///< 0x04 -> CPU clk/16 | 
      
        |  |  | 41 | #define ADC_PRESCALE_DIV32		0x05	///< 0x05 -> CPU clk/32 | 
      
        |  |  | 42 | #define ADC_PRESCALE_DIV64		0x06	///< 0x06 -> CPU clk/64 | 
      
        |  |  | 43 | #define ADC_PRESCALE_DIV128		0x07	///< 0x07 -> CPU clk/128 | 
      
        |  |  | 44 | // default value | 
      
        |  |  | 45 | #define ADC_PRESCALE			ADC_PRESCALE_DIV64 | 
      
        |  |  | 46 | // do not change the mask value | 
      
        |  |  | 47 | #define ADC_PRESCALE_MASK		0x07 | 
      
        |  |  | 48 |  | 
      
        |  |  | 49 | // A2D voltage reference select | 
      
        |  |  | 50 | //		*this determines what is used as the | 
      
        |  |  | 51 | //		full-scale voltage point for A2D conversions | 
      
        |  |  | 52 | #define ADC_REFERENCE_AREF		0x00	///< 0x00 -> AREF pin, internal VREF turned off | 
      
        |  |  | 53 | #define ADC_REFERENCE_AVCC		0x01	///< 0x01 -> AVCC pin, internal VREF turned off | 
      
        |  |  | 54 | #define ADC_REFERENCE_RSVD		0x02	///< 0x02 -> Reserved | 
      
        |  |  | 55 | #define ADC_REFERENCE_256V		0x03	///< 0x03 -> Internal 2.56V VREF | 
      
        |  |  | 56 | // default value | 
      
        |  |  | 57 | #define ADC_REFERENCE			ADC_REFERENCE_AVCC | 
      
        |  |  | 58 | // do not change the mask value | 
      
        |  |  | 59 | #define ADC_REFERENCE_MASK		0xC0 | 
      
        |  |  | 60 |  | 
      
        |  |  | 61 | // bit mask for A2D channel multiplexer | 
      
        |  |  | 62 | #define ADC_MUX_MASK			0x1F | 
      
        |  |  | 63 |  | 
      
        |  |  | 64 | // channel defines (for reference and use in code) | 
      
        |  |  | 65 | // these channels supported by all AVRs with A2D | 
      
        |  |  | 66 | #define ADC_CH_ADC0				0x00 | 
      
        |  |  | 67 | #define ADC_CH_ADC1				0x01 | 
      
        |  |  | 68 | #define ADC_CH_ADC2				0x02 | 
      
        |  |  | 69 | #define ADC_CH_ADC3				0x03 | 
      
        |  |  | 70 | #define ADC_CH_ADC4				0x04 | 
      
        |  |  | 71 | #define ADC_CH_ADC5				0x05 | 
      
        |  |  | 72 | #define ADC_CH_ADC6				0x06 | 
      
        |  |  | 73 | #define ADC_CH_ADC7				0x07 | 
      
        |  |  | 74 | #define ADC_CH_122V				0x1E	///< 1.22V voltage reference | 
      
        |  |  | 75 | #define ADC_CH_AGND				0x1F	///< AGND | 
      
        |  |  | 76 | // these channels supported only in ATmega128 | 
      
        |  |  | 77 | // differential with gain | 
      
        |  |  | 78 | #define ADC_CH_0_0_DIFF10X		0x08 | 
      
        |  |  | 79 | #define ADC_CH_1_0_DIFF10X		0x09 | 
      
        |  |  | 80 | #define ADC_CH_0_0_DIFF200X		0x0A | 
      
        |  |  | 81 | #define ADC_CH_1_0_DIFF200X		0x0B | 
      
        |  |  | 82 | #define ADC_CH_2_2_DIFF10X		0x0C | 
      
        |  |  | 83 | #define ADC_CH_3_2_DIFF10X		0x0D | 
      
        |  |  | 84 | #define ADC_CH_2_2_DIFF200X		0x0E | 
      
        |  |  | 85 | #define ADC_CH_3_2_DIFF200X		0x0F | 
      
        |  |  | 86 | // differential | 
      
        |  |  | 87 | #define ADC_CH_0_1_DIFF1X		0x10 | 
      
        |  |  | 88 | #define ADC_CH_1_1_DIFF1X		0x11 | 
      
        |  |  | 89 | #define ADC_CH_2_1_DIFF1X		0x12 | 
      
        |  |  | 90 | #define ADC_CH_3_1_DIFF1X		0x13 | 
      
        |  |  | 91 | #define ADC_CH_4_1_DIFF1X		0x14 | 
      
        |  |  | 92 | #define ADC_CH_5_1_DIFF1X		0x15 | 
      
        |  |  | 93 | #define ADC_CH_6_1_DIFF1X		0x16 | 
      
        |  |  | 94 | #define ADC_CH_7_1_DIFF1X		0x17 | 
      
        |  |  | 95 |  | 
      
        |  |  | 96 | #define ADC_CH_0_2_DIFF1X		0x18 | 
      
        |  |  | 97 | #define ADC_CH_1_2_DIFF1X		0x19 | 
      
        |  |  | 98 | #define ADC_CH_2_2_DIFF1X		0x1A | 
      
        |  |  | 99 | #define ADC_CH_3_2_DIFF1X		0x1B | 
      
        |  |  | 100 | #define ADC_CH_4_2_DIFF1X		0x1C | 
      
        |  |  | 101 | #define ADC_CH_5_2_DIFF1X		0x1D | 
      
        |  |  | 102 |  | 
      
        |  |  | 103 | // compatibility for new Mega processors | 
      
        |  |  | 104 | // ADCSR hack apparently no longer necessary in new AVR-GCC | 
      
        |  |  | 105 | #ifdef ADCSRA | 
      
        |  |  | 106 | #ifndef ADCSR | 
      
        |  |  | 107 | 	#define ADCSR	ADCSRA | 
      
        |  |  | 108 | #endif | 
      
        |  |  | 109 | #endif | 
      
        |  |  | 110 | #ifdef ADATE | 
      
        |  |  | 111 | 	#define ADFR	ADATE | 
      
        |  |  | 112 | #endif | 
      
        |  |  | 113 |  | 
      
        |  |  | 114 | // function prototypes | 
      
        |  |  | 115 |  | 
      
        |  |  | 116 | //! Initializes the A/D converter. | 
      
        |  |  | 117 | /// Turns ADC on and prepares it for use. | 
      
        |  |  | 118 | void a2dInit(void); | 
      
        |  |  | 119 |  | 
      
        |  |  | 120 | //! Turn off A/D converter | 
      
        |  |  | 121 | void a2dOff(void); | 
      
        |  |  | 122 |  | 
      
        |  |  | 123 | //! Sets the division ratio of the A/D converter clock. | 
      
        |  |  | 124 | /// This function is automatically called from a2dInit() | 
      
        |  |  | 125 | /// with a default value. | 
      
        |  |  | 126 | void a2dSetPrescaler(unsigned char prescale); | 
      
        |  |  | 127 |  | 
      
        |  |  | 128 | //! Configures which voltage reference the A/D converter uses. | 
      
        |  |  | 129 | /// This function is automatically called from a2dInit() | 
      
        |  |  | 130 | /// with a default value. | 
      
        |  |  | 131 | void a2dSetReference(unsigned char ref); | 
      
        |  |  | 132 |  | 
      
        |  |  | 133 | //! sets the a2d input channel | 
      
        |  |  | 134 | void a2dSetChannel(unsigned char ch); | 
      
        |  |  | 135 |  | 
      
        |  |  | 136 | //! start a conversion on the current a2d input channel | 
      
        |  |  | 137 | void a2dStartConvert(void); | 
      
        |  |  | 138 |  | 
      
        |  |  | 139 | //! return TRUE if conversion is complete | 
      
        |  |  | 140 | u08 a2dIsComplete(void); | 
      
        |  |  | 141 |  | 
      
        |  |  | 142 | //! Starts a conversion on A/D channel# ch, | 
      
        |  |  | 143 | /// returns the 10-bit value of the conversion when it is finished. | 
      
        |  |  | 144 | unsigned short a2dConvert10bit(unsigned char ch); | 
      
        |  |  | 145 |  | 
      
        |  |  | 146 | //! Starts a conversion on A/D channel# ch, | 
      
        |  |  | 147 | /// returns the 8-bit value of the conversion when it is finished. | 
      
        |  |  | 148 | unsigned char a2dConvert8bit(unsigned char ch); | 
      
        |  |  | 149 |  | 
      
        |  |  | 150 | #endif | 
      
        |  |  | 151 | //@} |