00001 /* ============================================================================= 00002 00003 Copyright (c) 2006 Pieter Conradie [www.piconomic.co.za] 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions are met: 00008 00009 * Redistributions of source code must retain the above copyright 00010 notice, this list of conditions and the following disclaimer. 00011 00012 * Redistributions in binary form must reproduce the above copyright 00013 notice, this list of conditions and the following disclaimer in 00014 the documentation and/or other materials provided with the 00015 distribution. 00016 00017 * Neither the name of the copyright holders nor the names of 00018 contributors may be used to endorse or promote products derived 00019 from this software without specific prior written permission. 00020 00021 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00022 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00024 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00025 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00026 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00027 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00028 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00029 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00030 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00031 POSSIBILITY OF SUCH DAMAGE. 00032 00033 Title: Simple polled ADC Driver 00034 Author(s): Pieter Conradie 00035 Creation Date: 2007-03-31 00036 Revision Info: $Id: adc.c 117 2010-06-24 20:21:28Z pieterconradie $ 00037 00038 ============================================================================= */ 00039 00040 /* _____STANDARD INCLUDES____________________________________________________ */ 00041 #include <avr/io.h> 00042 00043 /* _____PROJECT INCLUDES_____________________________________________________ */ 00044 #include "adc.h" 00045 00046 /* _____LOCAL DEFINITIONS____________________________________________________ */ 00047 00048 /* _____MACROS_______________________________________________________________ */ 00049 00050 /* _____GLOBAL VARIABLES_____________________________________________________ */ 00051 00052 /* _____LOCAL VARIABLES______________________________________________________ */ 00053 00054 /* _____LOCAL FUNCTION DECLARATIONS__________________________________________ */ 00055 00056 /* _____LOCAL FUNCTIONS______________________________________________________ */ 00057 00058 /* _____GLOBAL FUNCTIONS_____________________________________________________ */ 00059 void adc_init(void) 00060 { 00061 /* 00062 Select AVCC as reference with external capacitor at AREF pin 00063 and ADC0 as the single-ended input channel with 1x gain. 00064 */ 00065 ADMUX = (0<<REFS1)|(1<<REFS0)|(0<<ADLAR) 00066 |(0<<MUX4) |(0<<MUX3) |(0<<MUX2) |(0<<MUX1)|(0<<MUX0); 00067 00068 /* 00069 Enable ADC; Select Prescaler of 128 (clock frequency of 57.6 kHz) 00070 */ 00071 ADCSRA = (1<<ADEN)|(0<<ADSC) |(0<<ADFR) |(0<<ADIF) 00072 |(0<<ADIE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); 00073 } 00074 00075 void adc_sel_channel(const u8_t channel) 00076 { 00077 // Select new ADC channel in one atomic operation 00078 u8_t admux = ADMUX; 00079 00080 admux &= ~((1<<MUX4)|(1<<MUX3)|(1<<MUX2)|(1<<MUX1)|(1<<MUX0)); 00081 admux |= (channel<<MUX0); 00082 00083 ADMUX = admux; 00084 } 00085 00086 u16_t adc_get_sample(void) 00087 { 00088 // Start conversion 00089 BIT_SET_HI(ADCSRA, ADSC); 00090 00091 // Wait until conversion is finished... 13 ADC clock cycles 00092 // 4.43 kSPS 00093 LOOP_UNTIL_BIT_IS_LO(ADCSRA, ADSC); 00094 00095 // Return sampled value 00096 return ADC; 00097 } 00098 00099 u32_t adc_get_samples(const u8_t channel, u8_t nr_of_samples) 00100 { 00101 u32_t value = 0; 00102 00103 // Select the specified channel 00104 adc_sel_channel(channel); 00105 00106 // Repeat until all the samples have been taken 00107 while(nr_of_samples != 0) 00108 { 00109 value += adc_get_sample(); 00110 nr_of_samples--; 00111 } 00112 00113 // Return the accumulated value 00114 return value; 00115 } 00116 00117 /** 00118 * @} 00119 */ 00120 00121 /* _____LOG__________________________________________________________________ */ 00122 /* 00123 00124 2007-03-31 : Pieter Conradie 00125 - First release 00126 00127 */
1.6.3