00001 /* 00002 * Copyright (C) 2004 by Ole Reinhardt <ole.reinhardt@kernelconcepts.de>, 00003 * Kernelconcepts http://www.kernelconcepts.de 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 3. Neither the name of the copyright holders nor the names of 00015 * contributors may be used to endorse or promote products derived 00016 * from this software without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS 00019 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE 00022 * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00023 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00024 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 00025 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00027 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 00028 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00029 * SUCH DAMAGE. 00030 * 00031 * For additional information see http://www.ethernut.de/ 00032 * 00033 */ 00034 00045 00046 #ifndef _ADC_H_ 00047 #define _ADC_H_ 00048 00049 // ENUM declaring possible ADC reference voltages 00050 // - AVCC = 5V 00051 // - AREF = External reference 00052 // - INTERNAL_256 = 2.56V 00053 00054 enum adc_ref_type 00055 { 00056 AVCC = 0, 00057 AREF, 00058 INTERNAL_256 00059 }; 00060 00061 typedef enum adc_ref_type adc_ref_t; 00062 00063 // ENUM declaring possible ADC modes 00064 // FREE_RUNNING: 00065 // Free-running mode. Samples continuously taken 00066 // every 13 cycles of ADC clock after 00067 // ADC_start_conversion() is called. 00068 // SINGLE_CONVERSION: 00069 // Single-conversion mode. One sample taken every time 00070 // ADC_start_conversion() is called. 00071 00072 enum adc_mode_type 00073 { 00074 ADC_OFF, 00075 FREE_RUNNING, 00076 SINGLE_CONVERSION 00077 }; 00078 00079 typedef enum adc_mode_type adc_mode_t; 00080 00081 // ENUM declaring possible ADC channels 00082 00083 enum adc_channel_type 00084 { 00085 ADC0=0, 00086 ADC1=1, 00087 ADC2=2, 00088 ADC3=3, 00089 ADC4=4, 00090 ADC5=5, 00091 ADC6=6, 00092 ADC7=7 00093 }; 00094 00095 typedef enum adc_channel_type adc_channel_t; 00096 00097 #define ADC_PRESCALE_DIV2 0x00 ///< 0x01,0x00 -> CPU clk/2 00098 #define ADC_PRESCALE_DIV4 0x02 ///< 0x02 -> CPU clk/4 00099 #define ADC_PRESCALE_DIV8 0x03 ///< 0x03 -> CPU clk/8 00100 #define ADC_PRESCALE_DIV16 0x04 ///< 0x04 -> CPU clk/16 00101 #define ADC_PRESCALE_DIV32 0x05 ///< 0x05 -> CPU clk/32 00102 #define ADC_PRESCALE_DIV64 0x06 ///< 0x06 -> CPU clk/64 00103 #define ADC_PRESCALE_DIV128 0x07 ///< 0x07 -> CPU clk/128 00104 00105 00106 // ADC_Init 00107 // 00108 // This function initializes the ADC based on the 00109 // #defines in config.h 00110 // 00111 // post: ADC initialized and primed for call to 00112 // start_conversion 00113 00114 void ADCInit(void); 00115 00116 00117 // ADC_SetRef 00118 // 00119 // Allows setting of reference voltage for ADC. 00120 // 00121 // NOTE: This function stops ADC conversion. One must 00122 // call ADC_start_conversion to restart the ADC. 00123 // 00124 // pre: "reference" is a valid ADC reference from the 00125 // choices given above 00126 // post: ADC conversion stopped and reference voltage 00127 // set to desired choice 00128 00129 void ADCSetRef(adc_ref_t reference); 00130 00131 00132 // ADC_SetMode 00133 // 00134 // Allows setting of ADC conversion mode: either 00135 // single-conversion or free-running. 00136 // 00137 // NOTE: This function stops ADC conversion. One must 00138 // call ADC_start_conversion to restart the ADC. 00139 // 00140 // pre: "mode" is a valid ADC reference from the 00141 // choices given above 00142 // post: ADC conversion stopped and the ADC mode is 00143 // set to desired choice 00144 00145 void ADCSetMode(adc_mode_t mode); 00146 00147 00148 // ADC_SetPrescale 00149 // 00150 // Allows setting of ADC clock prescalar (ADC rate). 00151 // The ADC rate is given by the system clock rate 00152 // divided by the prescalar value. Possible prescalar 00153 // values range from 2-128 in powers of 2 (2,4,8,etc.) 00154 // 00155 // NOTE: This function stops ADC conversion. One must 00156 // call ADC_start_conversion to restart the ADC. 00157 // 00158 // pre: "prescalar" is a valid ADC reference from the 00159 // choices given above 00160 // post: ADC conversion stopped and ADC prescalar 00161 // set to desired choice 00162 00163 uint8_t ADCSetPrescale(uint8_t prescalar); 00164 00165 00166 // ADC_SetChannel 00167 // 00168 // Sets the channel that the ADC reads. The ADC 00169 // may only read from one channel at a time. 00170 // 00171 // pre: "adc_channel" is a valid ADC reference from the 00172 // choices given above 00173 // post: ADC conversion stopped and ADC channel 00174 // set to desired choice 00175 00176 void ADCSetChannel(adc_channel_t adc_channel); 00177 00178 00179 // ADC_BufferFlush 00180 // 00181 // Flushes the local buffer used to store ADC values 00182 // between conversion and the user's call to ADC_read 00183 // 00184 // NOTE: It is recommended that one calls buffer flush 00185 // if any changes are made to the ADC's state. 00186 // 00187 // pre: none 00188 // post: Local ADC buffer has been flushed 00189 00190 void ADCBufferFlush(void); 00191 00192 00193 // ADC_StartConversion 00194 // 00195 // Begins ADC conversion. If in single-conversion mode, 00196 // this function will only convert one value. If in 00197 // free-running mode, this function will begin 00198 // continuous conversion at the rate set by the 00199 // prescalar (see ADC_set_prescalar). 00200 // 00201 // NOTE: Converted values from the ADC are stored 00202 // in a local buffer. The user must call 00203 // ADC_read to obtain these values. 00204 // 00205 // pre: none 00206 // post: The ADC has started conversion. Completion of 00207 // any conversions is not guaranteed. 00208 00209 void ADCStartConversion(void); 00210 00211 00212 // ADC_StartLowNoiseConversion 00213 // 00214 // Set Conversion Mode to SINGLE_CONVERSION, Enters 00215 // adc sleep mode and wait until conversion interrupt occurs. 00216 // CPU will go to sleep mode!!! 00217 // BE AWARE OF WHAT IS WRITTEN IN THE AVR DATASHEET 00218 // 00219 // NOTE: Converted values from the ADC are stored 00220 // in a local buffer. The user must call 00221 // ADC_read to obtain these values. 00222 // 00223 // Only implemented for avr_gcc. Any other architecture 00224 // and compiler will use normal conversion 00225 // pre: none 00226 // post: The ADC has started conversion. Completion of 00227 // any conversions is not guaranteed. 00228 00229 00230 void ADCStartLowNoiseConversion(void); 00231 00232 // ADC_StopConversion 00233 // 00234 // Stops ADC conversion if ADC is in free-running 00235 // mode. Has no effect if ADC is in single-conversion 00236 // mode. 00237 // 00238 // pre: none 00239 // post: ADC conversion has been stopped. 00240 00241 void ADCStopConversion(void); 00242 00243 00244 // ADC_read 00245 // 00246 // Reads ADC values from local buffer. Reads one ADC 00247 // conversion value at a time. 00248 // 00249 // return: 0 = OK 00250 // 1 = No ADC value to read. "value" is invalid 00251 // 00252 // pre: "value" points to a valid variable. 00253 // post: If no errors, one ADC conversion value has 00254 // been read and placed in the variable pointed 00255 // to by "value" 00256 00257 uint8_t ADCRead(uint16_t *value); 00258 00259 // ADC_GetMode 00260 // returns current conversion mode 00261 00262 inline adc_mode_t ADCGetMode(void); 00263 00264 #endif 00265