Aversive/Modules/hardware/adc
De Wikidroids
Return to Aversive/Modules/hardware
Module ADC
This module provides several functions which enables the use of ATmega's internal Analog to Digital Convertor.
- initialisation
void adc_init(void)
- shuts off the ADC, for power consumption
void adc_shutoff(void)
- launch a conversion : this function launches a conversion with the specified configuration.
void adc_launch(void * conversion_config)
The conversion_config is casted to an int.
- get a conversion result : must not been used in interrupt mode !!
s16 adc_get_value(void * conversion_config) s32 adc_get_value32(void * conversion_config)
The conversion_config is casted to an int.
This functions is blocking while the ADC conversion is operating !! This is relatively slow (0.26 ms max), so be careful using this. To enhance speed, you can call adc_launch() before, in order to start the conversion long enough before.
If the config is equal to ADC_NO_CONFIG the config used is the previous one, ( or the one used for launching the conversion )
The 32 bitversion of the function is the same, except for the size of the result. This is used for compatibility with the control_system (waiting for a configurable one....)
- register an event, called in all cases when the ADC finishes
void adc_register_event( (void (*f)(s16) )
- exemple of code : simple polling mode, no event, no launch
adc_init(); ... a = adc_get_value(ADC_REF_AVCC |MUX_ADC0); // polling mode is default,this function takes 0.25 ms
- exemple of code : polling mode, no event, but pré-launch
adc_init(); ... adc_launch(ADC_REF_AVCC |MUX_ADC0); ... a = adc_get_value(ADC_REF_AVCC |MUX_ADC0); // polling mode, but this function takes less time than in the previous example
In this example, call could be : adc_get_value(ADC_NO_CONFIG); the difference is, with this version, you do not make shure that an interruption has not launched another conversion with another config between.
- exemple of code : interrupt mode
adc_init(); ... adc_register_event(result_function) adc_launch( ADC_REF_AVCC |MUX_ADC0 | ADC_MODE_INT );
void result_function(s16 result) { ... // using the result adc_launch(ADC_REF_AVCC |MUX_ADC1 | ADC_MODE_INT ); // launch next conversion (optionnal) }
- details
The conversion_config is casted to an int whose 8 lower bits are directly reported into the ADMUX register. Attention ! when changing the reference voltage, the first conversion can be inaccurate, please discard this value !! The upper bits are be used as flags for options that are not in the ADMUX register. for detailed configuration options, please see in adc_archs.h, and in the datasheet of your AVR part !
One flag is important for the ADC, and determinates with which method the ADC will give back it's value : ADC_MODE_POLL (default): This flag will make the ADC give back it's value after a polling of the result. Event function is called, while in the adc_get_value function. ADC_MODE_INT : if this flag is set, the ADC will call the event function when the conversion finishes, in an interrupt routine ADC_MODE_ADVANCE : special mode, TBD