AVR1631 - Energy Meter Reference Design with ATxmega32A4  Rev 1.0
 All Data Structures Files Functions Variables Typedefs Macros
adc_driver.h
Go to the documentation of this file.
1 /* This file has been prepared for Doxygen automatic documentation generation.*/
60 #ifndef ADC_DRIVER_H
61 #define ADC_DRIVER_H
62 
63 #include "avr_compiler.h"
64 
65 
66 #define COMMON_MODE_CYCLES 16
67 
68 
69 /* Macros */
70 
83 #define ADC_Enable(_adc) ((_adc)->CTRLA |= ADC_ENABLE_bm)
84 
89 #define ADC_Disable(_adc) ((_adc)->CTRLA = (_adc)->CTRLA & (~ADC_ENABLE_bm))
90 
95 #define ADC_Pipeline_Flush(_adc) ((_adc)->CTRLA |= ADC_FLUSH_bm)
96 
97 
111 #define ADC_ConvMode_and_Resolution_Config(_adc, _signedMode, _resolution) \
112  ((_adc)->CTRLB = ((_adc)->CTRLB & (~(ADC_RESOLUTION_gm|ADC_CONMODE_bm)))| \
113  (_resolution| ( _signedMode? ADC_CONMODE_bm : 0)))
114 
119 #define ADC_ConvMode_Signed true
120 
125 #define ADC_ConvMode_Unsigned false
126 
127 
140 #define ADC_Prescaler_Config(_adc, _div) \
141  ((_adc)->PRESCALER = ((_adc)->PRESCALER & (~ADC_PRESCALER_gm)) | _div)
142 
143 
150 #define ADC_Reference_Config(_adc, _convRef) \
151  ((_adc)->REFCTRL = ((_adc)->REFCTRL & ~(ADC_REFSEL_gm)) | _convRef)
152 
153 
159 #define ADC_SweepChannels_Config(_adc, _sweepChannels) \
160  ((_adc)->EVCTRL = ((_adc)->EVCTRL & (~ADC_SWEEP_gm)) | _sweepChannels)
161 
162 
176 #define ADC_Events_Config(_adc, _eventChannels, _eventMode) \ \
177  (_adc)->EVCTRL = ((_adc)->EVCTRL & (~(ADC_EVSEL_gm | ADC_EVACT_gm))) | \
178  ((uint8_t) _eventChannels | _eventMode)
179 
180 
195 #define ADC_Ch_Interrupts_Config(_adc_ch, _interruptMode, _interruptLevel) \
196  (_adc_ch)->INTCTRL = (((_adc_ch)->INTCTRL & \
197  (~(ADC_CH_INTMODE_gm | ADC_CH_INTLVL_gm))) | \
198  ((uint8_t) _interruptMode | _interruptLevel))
199 
200 
210 #define ADC_Ch_InputMode_and_Gain_Config(_adc_ch, _inputMode, _gain) \
211  (_adc_ch)->CTRL = ((_adc_ch)->CTRL & \
212  (~(ADC_CH_INPUTMODE_gm|ADC_CH_GAIN_gm))) | \
213  ((uint8_t) _inputMode|_gain)
214 
215 
220 #define ADC_DRIVER_CH_GAIN_NONE ADC_CH_GAIN_1X_gc
221 
222 
233 #define ADC_Ch_InputMux_Config(_adc_ch, _posInput, _negInput) \
234  ((_adc_ch)->MUXCTRL = (uint8_t) _posInput | _negInput)
235 
236 
243 #define ADC_Ch_Conversion_Complete(_adc_ch) \
244  (((_adc_ch)->INTFLAGS & ADC_CH_CHIF_bm) != 0x00)
245 
246 
260 #define ADC_CompareValue_Set(_adc, _value) ((_adc)->CMP = _value)
261 
262 
267 #define ADC_FreeRunning_Enable(_adc) ((_adc)->CTRLB |= ADC_FREERUN_bm)
268 
269 
274 #define ADC_FreeRunning_Disable(_adc) \
275  ((_adc)->CTRLB = (_adc)->CTRLB & (~ADC_FREERUN_bm))
276 
277 
278 
279 #define ADC_Low_Impedance_Mode(_adc) ((_adc)->CTRLB |= ADC_IMPMODE_bm)
280 
281 #define ADC_High_Impedence_Mode(_adc) \
282  ((_adc)->CTRLB = (_adc)->CTRLB & (~ADC_IMPMODE_bm))
283 
284 
285 
286 
295 #define ADC_Ch_Conversion_Start(_adc_ch) ((_adc_ch)->CTRL |= ADC_CH_START_bm)
296 
297 
309 #define ADC_Conversions_Start(_adc, _channelMask) \
310  (_adc)->CTRLA |= _channelMask & \
311  (ADC_CH0START_bm | ADC_CH1START_bm | \
312  ADC_CH2START_bm | ADC_CH3START_bm)
313 
314 
321 #define ADC_BandgapReference_Enable(_adc) ((_adc)->REFCTRL |= ADC_BANDGAP_bm)
322 
323 
328 #define ADC_BandgapReference_Disable(_adc) ((_adc)->REFCTRL &= ~ADC_BANDGAP_bm)
329 
330 
337 #define ADC_TempReference_Enable(_adc) ((_adc)->REFCTRL |= ADC_TEMPREF_bm)
338 
339 
344 #define ADC_TempReference_Disable(_adc) \
345  ((_adc)->REFCTRL = (_adc)->REFCTRL & (~ADC_TEMPREF_bm))
346 
347 
348 /* Prototype for assembly macro. */
349 uint8_t SP_ReadCalibrationByte( uint8_t index );
350 
351 /* Prototypes for functions. */
352 void ADC_CalibrationValues_Load(ADC_t * adc);
353 
354 uint16_t ADC_ResultCh_GetWord_Unsigned(ADC_CH_t * adc_ch, uint16_t offset);
355 int16_t ADC_ResultCh_GetWord_Signed(ADC_CH_t * adc_ch, int16_t offset);
356 
357 uint16_t ADC_Offset_Get_Unsigned(ADC_t * adc, ADC_CH_t *ch, bool oversampling);
358 int16_t ADC_Offset_Get_Signed(ADC_t * adc, ADC_CH_t *ch, bool oversampling);
359 
360 uint16_t ADC_ResultCh_GetWord(ADC_CH_t * adc_ch);
361 uint8_t ADC_ResultCh_GetLowByte(ADC_CH_t * adc_ch);
362 uint8_t ADC_ResultCh_GetHighByte(ADC_CH_t * adc_ch);
363 
364 void ADC_Wait_8MHz(ADC_t * adc);
365 void ADC_Wait_32MHz(ADC_t * adc);
366 
372 #define ADC_Referance_Config(_adc, _convRef) ADC_Reference_Config(_adc, _convRef)
373 #define ADC_CalibrationValues_Set(_adc) ADC_CalibrationValues_Load(_adc)
374 
375 
376 
382 #ifndef ADCACAL0_offset
383 
384 #define ADCACAL0_offset 0x20
385 #define ADCACAL1_offset 0x21
386 #define ADCBCAL0_offset 0x24
387 #define ADCBCAL1_offset 0x25
388 #endif
389 
390 
391 #endif