Xmega Application Note | |||||
This file contains the function implementations the XMEGA ADC driver.
The driver is not intended for size and/or speed critical code, since most functions are just a few lines of code, and the function call overhead would decrease code performance. The driver is intended for rapid prototyping and documentation purposes for getting started with the XMEGA ADC module.
For size and/or speed critical code, it is recommended to copy the function contents directly into your application instead of making a function call.
Several functions use the following construct: "some_register = ... | (some_parameter ? SOME_BIT_bm : 0) | ..." Although the use of the ternary operator ( if ? then : else ) is discouraged, in some occasions the operator makes it possible to write pretty clean and neat code. In this driver, the construct is used to set or not set a configuration bit based on a boolean input parameter, such as the "some_parameter" in the example above.
Copyright (c) 2008, Atmel Corporation All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Definition in file adc_driver.c.
#include "adc_driver.h"
Go to the source code of this file.
Functions | |
void | ADC_CalibrationValues_Load (ADC_t *adc) |
This function get the calibration data from the production calibration. | |
int8_t | ADC_Offset_Get_Signed (ADC_t *adc, ADC_CH_t *ch, bool oversampling) |
This function gets the offset of the ADC when it is configured in signed mode. | |
uint8_t | ADC_Offset_Get_Unsigned (ADC_t *adc, ADC_CH_t *ch, bool oversampling) |
This function gets the offset of the ADC when it is configured in unsigned mode. | |
uint8_t | ADC_ResultCh_GetHighByte (ADC_CH_t *adc_ch) |
This function clears the interrupt flag and returns the high byte of the coversion result. | |
uint8_t | ADC_ResultCh_GetLowByte (ADC_CH_t *adc_ch) |
This function clears the interrupt flag and returns the low byte of the coversion result. | |
uint16_t | ADC_ResultCh_GetWord (ADC_CH_t *adc_ch) |
This function clears the interrupt flag and returns the coversion result without compensating for offset. | |
int16_t | ADC_ResultCh_GetWord_Signed (ADC_CH_t *adc_ch, int8_t signedOffset) |
This function clears the interrupt flag and returns the signed coversion result. | |
uint16_t | ADC_ResultCh_GetWord_Unsigned (ADC_CH_t *adc_ch, uint8_t offset) |
This function clears the interrupt flag and returns the unsigned coversion result. | |
void | ADC_Wait_32MHz (ADC_t *adc) |
This function waits until the adc common mode is settled. | |
void | ADC_Wait_8MHz (ADC_t *adc) |
This function waits until the adc common mode is settled. |
void ADC_CalibrationValues_Load | ( | ADC_t * | adc | ) |
This function get the calibration data from the production calibration.
The calibration data is loaded from flash and stored in the calibration register. The calibration data reduces the non-linearity error in the adc.
adc | Pointer to ADC module register section. |
Definition at line 77 of file adc_driver.c.
Referenced by main().
00078 { 00079 if(&ADCA == adc){ 00080 /* Get ADCACAL0 from production signature . */ 00081 adc->CALL = SP_ReadCalibrationByte( PROD_SIGNATURES_START + ADCACAL0_offset ); 00082 adc->CALH = SP_ReadCalibrationByte( PROD_SIGNATURES_START + ADCACAL1_offset ); 00083 }else { 00084 /* Get ADCBCAL0 from production signature */ 00085 adc->CALL = SP_ReadCalibrationByte( PROD_SIGNATURES_START + ADCBCAL0_offset ); 00086 adc->CALH = SP_ReadCalibrationByte( PROD_SIGNATURES_START + ADCACAL1_offset ); 00087 } 00088 }
int8_t ADC_Offset_Get_Signed | ( | ADC_t * | adc, | |
ADC_CH_t * | ch, | |||
bool | oversampling | |||
) |
This function gets the offset of the ADC when it is configured in signed mode.
This function does one or several measurements to determine the offset of the ADC.
This function only return the low byte of the 12-bit convertion, because the offset should never be more than +-8 LSB off.
adc | Pointer to the ADC to calculate offset from. | |
ch | Pointer to the ADC channel to measure on. | |
oversampling | false for one measurement. true for averaging several measurements. |
Definition at line 318 of file adc_driver.c.
Referenced by main().
00319 { 00320 if (oversampling) 00321 { 00322 int16_t offset=0; 00323 for (int i=0; i<4; i++) 00324 { 00325 /* Do one conversion to find offset. */ 00326 ADC_Ch_Conversion_Start(ch); 00327 00328 do{ 00329 }while(!ADC_Ch_Conversion_Complete(ch)); 00330 offset += ADC_ResultCh_GetWord_Signed(ch, 0x00); 00331 } 00332 return ((int8_t)(offset/4)); 00333 } 00334 else 00335 { 00336 int8_t offset=0; 00337 00338 /* Do one conversion to find offset. */ 00339 ADC_Ch_Conversion_Start(ch); 00340 00341 do{ 00342 }while(!ADC_Ch_Conversion_Complete(ch)); 00343 offset = (uint8_t)ADC_ResultCh_GetWord_Signed(ch, 0x00); 00344 00345 return offset; 00346 } 00347 }
uint8_t ADC_Offset_Get_Unsigned | ( | ADC_t * | adc, | |
ADC_CH_t * | ch, | |||
bool | oversampling | |||
) |
This function gets the offset of the ADC when it is configured in unsigned mode.
This function does one or several measurements to determine the offset of the ADC.
This function only return the low byte of the 12-bit convertion, because the offset should never be more than +-8 LSB off.
adc | Pointer to the ADC to calculate offset from. | |
ch | Pointer to the ADC channel to measure on. | |
oversampling | false for one measurement. true for averaging several measurements. |
Definition at line 271 of file adc_driver.c.
00272 { 00273 if (oversampling) 00274 { 00275 uint16_t offset=0; 00276 for (int i=0; i<4; i++) 00277 { 00278 /* Do one conversion to find offset. */ 00279 ADC_Ch_Conversion_Start(ch); 00280 00281 do{ 00282 }while(!ADC_Ch_Conversion_Complete(ch)); 00283 offset += ADC_ResultCh_GetWord_Unsigned(ch, 0x00); 00284 } 00285 return ((uint8_t)(offset>>2)); 00286 } 00287 else 00288 { 00289 uint8_t offset=0; 00290 00291 /* Do one conversion to find offset. */ 00292 ADC_Ch_Conversion_Start(ch); 00293 00294 do{ 00295 }while(!ADC_Ch_Conversion_Complete(ch)); 00296 offset = (uint8_t)ADC_ResultCh_GetWord(ch); 00297 00298 return offset; 00299 } 00300 }
uint8_t ADC_ResultCh_GetHighByte | ( | ADC_CH_t * | adc_ch | ) |
This function clears the interrupt flag and returns the high byte of the coversion result.
This funtion should be used together with the ADC_ResultCh_ConversionComplete. When the conversion result is ready this function reads out the result.
adc_ch | Pointer to ADC channel register section. |
Definition at line 186 of file adc_driver.c.
00187 { 00188 /* Clear interrupt flag.*/ 00189 adc_ch->INTFLAGS = ADC_CH_CHIF_bm; 00190 00191 /* Return low byte result register contents.*/ 00192 return adc_ch->RESH; 00193 }
uint8_t ADC_ResultCh_GetLowByte | ( | ADC_CH_t * | adc_ch | ) |
This function clears the interrupt flag and returns the low byte of the coversion result.
This funtion should be used together with the ADC_Ch_Conversion_Complete. When the conversion result is ready this funciton reads out the result.
adc_ch | Pointer to ADC channel register section. |
Definition at line 166 of file adc_driver.c.
00167 { 00168 /* Clear interrupt flag.*/ 00169 adc_ch->INTFLAGS = ADC_CH_CHIF_bm; 00170 /* Return result register contents*/ 00171 return adc_ch->RESL; 00172 }
uint16_t ADC_ResultCh_GetWord | ( | ADC_CH_t * | adc_ch | ) |
This function clears the interrupt flag and returns the coversion result without compensating for offset.
This function should be used together with the ADC_Ch_Conversion_Complete. When the conversion result is ready this funciton reads out the result.
adc_ch | Pointer to ADC channel register section. |
Definition at line 144 of file adc_driver.c.
Referenced by ADC_Offset_Get_Unsigned().
00145 { 00146 /* Clear interrupt flag.*/ 00147 adc_ch->INTFLAGS = ADC_CH_CHIF_bm; 00148 00149 /* Return result register contents*/ 00150 return adc_ch->RES;; 00151 }
int16_t ADC_ResultCh_GetWord_Signed | ( | ADC_CH_t * | adc_ch, | |
int8_t | signedOffset | |||
) |
This function clears the interrupt flag and returns the signed coversion result.
This function should be used together with the ADC_Ch_Conversion_Complete. When the conversion result is ready this funciton reads out the result.
adc_ch | Pointer to ADC channel register section. | |
signedOffset | Offset value to subtract. |
Definition at line 123 of file adc_driver.c.
Referenced by ADC_Offset_Get_Signed(), ISR(), and main().
00124 { 00125 int16_t answer; 00126 00127 /* Clear interrupt flag.*/ 00128 adc_ch->INTFLAGS = ADC_CH_CHIF_bm; 00129 00130 /* Return result register contents*/ 00131 answer = adc_ch->RES - signedOffset; 00132 00133 return answer; 00134 }
uint16_t ADC_ResultCh_GetWord_Unsigned | ( | ADC_CH_t * | adc_ch, | |
uint8_t | offset | |||
) |
This function clears the interrupt flag and returns the unsigned coversion result.
This function should be used together with the ADC_Ch_Conversion_Complete. When the conversion result is ready this funciton reads out the result.
adc_ch | Pointer to ADC channel register section. | |
offset | Unsigned offset value to subtract. |
Definition at line 100 of file adc_driver.c.
Referenced by ADC_Offset_Get_Unsigned().
00101 { 00102 uint16_t answer; 00103 00104 /* Clear interrupt flag.*/ 00105 adc_ch->INTFLAGS = ADC_CH_CHIF_bm; 00106 00107 /* Return result register contents*/ 00108 answer = adc_ch->RES - offset; 00109 00110 return answer; 00111 }
void ADC_Wait_32MHz | ( | ADC_t * | adc | ) |
This function waits until the adc common mode is settled.
After the ADC clock has been turned on, the common mode voltage in the ADC need some time to settle. The time it takes equals one dummy conversion. Instead of doing a dummy conversion this function waits until the common mode is settled.
The ADC clock is turned off every time the ADC i disabled or the device goes into sleep (not Idle sleep mode).
adc | Pointer to ADC module register section. |
Definition at line 240 of file adc_driver.c.
00241 { 00242 /* Store old prescaler value. */ 00243 uint8_t prescaler_val = adc->PRESCALER; 00244 00245 /* Set prescaler value to minimum value. */ 00246 adc->PRESCALER = ADC_PRESCALER_DIV8_gc; 00247 00248 /* wait 8*COMMON_MODE_CYCLES for common mode to settle*/ 00249 delay_us(8*COMMON_MODE_CYCLES); 00250 00251 /* Set prescaler to old value*/ 00252 adc->PRESCALER = prescaler_val; 00253 }
void ADC_Wait_8MHz | ( | ADC_t * | adc | ) |
This function waits until the adc common mode is settled.
After the ADC clock has been turned on, the common mode voltage in the ADC need some time to settle. The time it takes equals one dummy conversion. Instead of doing a dummy conversion this function waits until the common mode is settled.
adc | Pointer to ADC module register section. |
Definition at line 208 of file adc_driver.c.
Referenced by main().
00209 { 00210 /* Store old prescaler value. */ 00211 uint8_t prescaler_val = adc->PRESCALER; 00212 00213 /* Set prescaler value to minimum value. */ 00214 adc->PRESCALER = ADC_PRESCALER_DIV4_gc; 00215 00216 /* Wait 4*COMMON_MODE_CYCLES for common mode to settle. */ 00217 delay_us(4*COMMON_MODE_CYCLES); 00218 00219 /* Set prescaler to old value*/ 00220 adc->PRESCALER = prescaler_val; 00221 }
Generated on Tue Aug 4 13:31:15 2009 for AVR1300 Using the XMEGA ADC by ![]() |