AvrIO  1.4.5
Bibliothèque C modulaire pour ATMEL AVR
adc.h
1 
21 #ifndef _AVRIO_ADC_H_
22 #define _AVRIO_ADC_H_
23 
24 #include <avrio/defs.h>
25 
26 __BEGIN_C_DECLS
27 /* ========================================================================== */
46 /* constants ================================================================ */
54 typedef enum {
56  eAdcVcc = 1,
59 } eAdcRef;
60 
64 typedef enum {
65  eAdcRaw = 0,
67  eAdcMin = 2,
68  eAdcMax = 4,
69 } eAdcFilter;
70 
71 /* internal public functions ================================================ */
80 void vAdcInit (void);
81 
88 void vAdcEnable (void);
89 
96 void vAdcDisable (void);
97 
103 uint16_t usAdcRead (uint8_t ucChannel);
104 
118 uint16_t usAdcReadFilter (uint8_t ucChannel, uint8_t ucTerms, eAdcFilter eFilter);
119 
128 static inline void vAdcSetChannel (uint8_t ucChan);
129 
138 void vAdcSetDiv (uint8_t ucDiv);
139 
143 uint8_t ucAdcGetDiv (void);
144 
145 // -----------------------------------------------------------------------------
146 # if defined(__DOXYGEN__)
147 /*
148  * __DOXYGEN__ defined
149  * Partie documentation ne devant pas être compilée.
150  * =============================================================================
151  */
166 static inline uint16_t usAdcReadAverage (uint8_t ucChannel, uint8_t ucTerms);
167 
175 static inline void vAdcSetRef (uint8_t ucRef);
176 
184 static inline uint8_t eAdcGetRef (void)
185 
186 
187 /* macros =================================================================== */
195 #define ADC_VALUE(_MEASUREMENT,_FULL_SCALE)
196 
204 #define ADC_MEASUREMENT(_VALUE,_FULL_SCALE)
205 
210 # else
211 /*
212  * __DOXYGEN__ not defined
213  * Partie ne devant pas être documentée.
214  * =============================================================================
215  */
216 #include "avrio-config.h"
217 #ifdef AVRIO_ADC_ENABLE
218 #include "avrio-board-adc.h"
219 #endif
220 #include <avrio/delay.h>
221 
222 /* inline public functions ================================================== */
223 #if defined(REFS2) && defined(REFS1) && defined(REFS0)
224 #define ADCREF_MASK (_BV(REFS2)|_BV(REFS1)|_BV(REFS0))
225 #elif defined(REFS1) && defined(REFS0)
226 #define ADCREF_MASK (_BV(REFS1)|_BV(REFS0))
227 #else
228 #define ADCREF_MASK (_BV(REFS0))
229 #endif
230 
231 #if !defined(ADMUX) && defined(ADMUXB)
232 #define ADMUX ADMUXB
233 #endif
234 
235 
236 /* -----------------------------------------------------------------------------
237  * Valeurs courantes:
238  00 AREF, Internal V ref turned off
239  01 AV CC with external capacitor at AREF pin
240  10 Reserved
241  11 Internal Voltage Reference with external capacitor at AREF pin
242 * attiny441/841: ADMUXB & REFS2 defined
243  AREF Pin Disconnected
244  000 VCC
245  001 Internal 1.1V reference
246  010 Internal 2.2V reference
247  011 Internal 4.096V reference
248 
249  AREF Pin Connected, with external bypass capacitor connected to pin
250  100 AREF pin (internal reference turned off)
251  101 Internal 1.1V reference
252  110 Internal 2.2V reference
253  111 Internal 4.096V reference
254  */
255 INLINE void vAdcSetRef (eAdcRef eRef) {
256 
257  ADMUX = (ADMUX & ~ADCREF_MASK) | (eRef << REFS0);
258  delay_ms (100);
259 }
260 
261 INLINE eAdcRef eAdcGetRef (void) {
262 
263  return (ADMUX & ADCREF_MASK) >> REFS0;
264 }
265 
266 INLINE uint16_t usAdcReadAverage (uint8_t ucChannel, uint8_t ucTerms) {
267 
268  return usAdcReadFilter (ucChannel, ucTerms, eAdcAverage);
269 }
270 
271 #if defined(ADC_SCALE_ENABLE)
272 
275 //void vAdcSetScale (uint8_t ucChannel, uint8_t ucScale);
276 
280 //uint8_t ucAdcGetScale (uint8_t ucChannel);
281 
285 //static inline void vAdcSetAutoscale (uint8_t ucChannel);
286 
290 //static inline void vAdcClearAutoscale (uint8_t ucChannel);
291 
292 void vAdcSetScale (uint8_t ucChannel, uint8_t ucScale);
293 uint8_t ucAdcGetScale (uint8_t ucChannel);
294 uint8_t ucAdcGetScaleMax (uint8_t ucChannel);
295 #else
296 INLINE void vAdcSetScale (uint8_t ucChannel, uint8_t ucScale) {
297 
298 }
299 INLINE uint8_t ucAdcGetScale (uint8_t ucChannel) {
300 
301  return 0;
302 }
303 INLINE uint8_t ucAdcGetScaleMax (uint8_t ucChannel) {
304 
305  return 0;
306 }
307 #endif
308 
309 #if defined(ADC_AUTOSCALE_ENABLE)
310 extern uint16_t usAdcAutoScaleFlag;
311 
312 INLINE void vAdcSetAutoscale (uint8_t ucChannel) {
313 
314  usAdcAutoScaleFlag = _BV(ucChannel);
315 }
316 
317 INLINE void vAdcClearAutoscale (uint8_t ucChannel) {
318 
319  usAdcAutoScaleFlag = 0;
320 }
321 #else
322 INLINE void vAdcSetAutoscale (uint8_t ucChannel) {
323 
324 }
325 
326 INLINE void vAdcClearAutoscale (uint8_t ucChannel) {
327 
328 }
329 #endif
330 
331 /* macros =================================================================== */
332 #define ADC_VALUE(_MEASUREMENT,_FULL_SCALE) \
333  ((uint16_t)(((_MEASUREMENT)*1024.0)/(_FULL_SCALE)))
334 
335 #define ADC_MEASUREMENT(_VALUE,_FULL_SCALE) \
336  ((((double)(_VALUE))*((double)(_FULL_SCALE)))/1024.0)
337 # endif /* __DOXYGEN__ not defined */
338 
339 /* ========================================================================== */
340 __END_C_DECLS
341 #endif /* _AVRIO_ADC_H_ not defined */
eAdcRef
Tension de référence.
Definition: adc.h:54
Mesure valeur maximale.
Definition: adc.h:67
#define delay_ms(__ms)
Temporisation en millisecondes.
Definition: delay.h:65
Broche AREF.
Definition: adc.h:55
static void vAdcSetChannel(uint8_t ucChan)
Modifie la voie du multiplexeur.
uint8_t ucAdcGetDiv(void)
Renvoie le facteur de division de l&#39;horloge de l&#39;ADC.
uint16_t usAdcRead(uint8_t ucChannel)
Lecture de l&#39;ADC.
void vAdcEnable(void)
Valide l&#39;ADC.
Référence interne supplémentaire (REFS = 10, seuleument sur certains MCU)
Definition: adc.h:58
void vAdcSetDiv(uint8_t ucDiv)
Modifie le facteur de division de l&#39;horloge de l&#39;ADC.
eAdcFilter
Type de filtrage de la mesure.
Definition: adc.h:64
void vAdcInit(void)
Initialise et valide l&#39;ADC pour l&#39;utilisation.
Mesure avec moyennage.
Definition: adc.h:66
void vAdcDisable(void)
Dévalide l&#39;ADC.
Référence interne (sur tous les MCU AVR8 disposant d&#39;un ADC)
Definition: adc.h:57
#define _BV(n)
Renvoie un masque avec le bit de rang n à 1.
Definition: defs.h:76
uint16_t usAdcReadFilter(uint8_t ucChannel, uint8_t ucTerms, eAdcFilter eFilter)
Lecture de l&#39;ADC avec filtrage.
Mesure directe de l&#39;ADC.
Definition: adc.h:65
Tension AVCC avec un condensateur sur AREF.
Definition: adc.h:56
static uint16_t usAdcReadAverage(uint8_t ucChannel, uint8_t ucTerms)
Lecture de l&#39;ADC avec moyennage.
Mesure valeur minimale.
Definition: adc.h:68
static void vAdcSetRef(uint8_t ucRef)
Modifie la tension de référence.