00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdlib.h>
00023
00024 #include <aversive.h>
00025 #include <adc.h>
00026
00027
00028 static uint16_t g_adc_previous_config = ADC_NO_CONFIG;
00029
00030
00031 static void (*adc_event)(int16_t) = NULL;
00032
00033
00038 void adc_init(void)
00039 {
00040 #if defined(PRADC) && defined(PRR)
00041 cbi(PRR, PRADC);
00042 #elif defined(PRADC) && defined(PRR0)
00043 cbi(PRR0, PRADC);
00044 #endif
00045
00046 ADCSRA = (1<<ADEN) | (ADC_PRESCALE << ADPS0);
00047 ADMUX = 0;
00048 #ifdef ADCSRB
00049 ADCSRB = 0;
00050 #endif
00051 }
00052
00056 void adc_shutdown(void)
00057 {
00058 ADCSRA = 0;
00059
00060 #if defined(PRADC) && defined(PRR)
00061 sbi(PRR, PRADC);
00062 #elif defined(PRADC) && defined(PRR0)
00063 sbi(PRR0, PRADC);
00064 #endif
00065 }
00066
00067
00072 void adc_register_event(void (*f)(int16_t))
00073 {
00074 uint8_t flags;
00075
00076 IRQ_LOCK(flags);
00077 adc_event = f;
00078 IRQ_UNLOCK(flags);
00079 }
00080
00085 SIGNAL(SIG_ADC)
00086 {
00087 int16_t result;
00088
00089 if (!adc_event)
00090 return;
00091
00092 result = ADC;
00093
00094
00095
00096
00097 if ( ( g_adc_previous_config & ADC_RESULT_SIGNED)
00098 && !(g_adc_previous_config & ADC_MODE_16_BITS)
00099 && (result & 0x0200) )
00100 result |= 0xFE00;
00101
00102 adc_event(result);
00103 }
00104
00105
00111 void adc_launch(uint16_t conversion_config)
00112 {
00113
00114
00115 ADMUX = conversion_config & 0xFF ;
00129 if ( (conversion_config & ADC_RESULT_SIGNED) &&
00130 (g_adc_previous_config != conversion_config) ) {
00131 cbi(ADCSRA,ADEN);
00132 sbi(ADCSRA,ADEN);
00133 }
00134
00135
00136 g_adc_previous_config = conversion_config;
00137
00138
00139 #ifdef MUX5_IN_ADCSRB
00140 if (conversion_config & MUX5_MASK_IN_CONFIG)
00141 sbi(ADCSRB, MUX5);
00142 else
00143 cbi(ADCSRB, MUX5);
00144 #endif // MUX5_IN_ADCSRB
00145
00146
00147 if (conversion_config & ADC_MODE_TRIGGERED)
00148 sbi(ADCSRA, ADFR);
00149 else
00150 cbi(ADCSRA, ADFR);
00151
00152
00153 if (conversion_config & ADC_MODE_INT) {
00154
00155
00156
00157 ADCSRA |= (1 << ADSC) | (1 << ADIF) | (1 << ADIE);
00158 }
00159 else {
00160
00161
00162 cbi(ADCSRA, ADIE);
00163 ADCSRA |= (1 << ADSC) | (1 << ADIF);
00164 }
00165 }
00166
00176 int16_t adc_get_value(uint16_t conversion_config)
00177 {
00178 int16_t result;
00179
00180
00181 if ((conversion_config == ADC_NO_CONFIG) ||
00182 (conversion_config == g_adc_previous_config)) {
00183 if (bit_is_clear(ADCSRA, ADSC) &&
00184 bit_is_clear(ADCSRA, ADIF)) {
00185
00186
00187 adc_launch(g_adc_previous_config);
00188 }
00189 }
00190
00191
00192 else {
00193
00194
00195 cbi(ADCSRA, ADFR);
00196 cbi(ADCSRA, ADIE);
00197
00198
00199
00200 while (bit_is_set(ADCSRA, ADSC));
00201
00202
00203 adc_launch(conversion_config);
00204 }
00205
00206
00207
00208 while (bit_is_clear(ADCSRA, ADIF));
00209 sbi(ADCSRA, ADIF);
00210
00211 result = ADC;
00212
00213
00214
00215 if( ( g_adc_previous_config & ADC_RESULT_SIGNED ) &&
00216 !(g_adc_previous_config & ADC_MODE_16_BITS) &&
00217 (result & 0x0200) ) {
00218 result |= 0xFE00;
00219 }
00220
00221 return result;
00222 }
00223
00224
00229 int32_t adc_get_value32(void *conversion_config)
00230 {
00231 return adc_get_value((uint16_t)conversion_config);
00232 }
00233