| BLDC control on ATAVRMC303 with ATxMega128A1

Functions
adc_driver.c File Reference

XMEGA ADC driver source file. More...

`#include "adc_driver.h"`
Include dependency graph for adc_driver.c:

Go to the source code of this file.

## Functions
void | ADC_CalibrationValues_Set (ADC_t *adc)
| This function get the calibration data from the production calibration. More...
uint8_t | ADC_Offset_Get (ADC_t *adc)
| This function get the offset of the ADC. More...
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. More...
uint8_t | ADC_ResultCh_GetLowByte (ADC_CH_t *adc_ch, uint8_t offset)
| This function clears the interrupt flag and returns the low byte of the coversion result. More...
uint16_t | ADC_ResultCh_GetWord (ADC_CH_t *adc_ch, uint8_t offset)
| This function clears the interrupt flag and returns the coversion result. More...
void | ADC_Wait_32MHz (ADC_t *adc)
| This function waits until the adc common mode is settled. More...
void | ADC_Wait_8MHz (ADC_t *adc)
| This function waits until the adc common mode is settled. More...

## Detailed Description

XMEGA ADC driver source file.

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.

Application note:
  AVR1300: Using the XMEGA ADC

Documentation
  For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html

Author
  Atmel Corporation: http://www.atmel.com
  Support email: avr@a.nosp@m.tmel.nosp@m..com

Revision
  1714

Date
  2008-08-13 11:09:37 +0200 (on, 13 aug 2008)

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.

## Function Documentation

## ◆ ADC_CalibrationValues_Set()

void ADC_CalibrationValues_Set | ( | 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 gain error in the adc.

Parameters

  adc | Pointer to ADC module register section.

Definition at line 77 of file adc_driver.c.

References SP_ReadCalibrationByte().

Referenced by ADC_Init().

78 {
79 if(&ADCA == adc){
80 /* Get ADCCAL0 from byte address 0x20 (Word address 0x10. */
81 adc->CAL = SP_ReadCalibrationByte(0x20);
82 }else {
83 /* Get ADCCAL0 from byte address 0x24 (Word address 0x12. */
84 adc->CAL = SP_ReadCalibrationByte(0x24);
85 }
86 }
SP_ReadCalibrationByte
uint8_t SP_ReadCalibrationByte(uint8_t index)
Here is the call graph for this function:

## ◆ ADC_Offset_Get()

uint8_t ADC_Offset_Get | ( | ADC_t * | adc | ) |

This function get the offset of the ADC.

This function make an internal coupling to the same pin and calculate the internal offset in the ADC.

Note
  This function only return the low byte of the 12-bit convertion, because the offset should never be more than +-8 LSB off.

Parameters

  adc | Pointer to the ADC to calculate offset from.

Returns
  Offset on the selected ADC

Definition at line 241 of file adc_driver.c.

References ADC_Ch_Conversion_Complete, ADC_Ch_Conversion_Start, ADC_Ch_InputMode_and_Gain_Config, ADC_Ch_InputMux_Config, ADC_ConvMode_and_Resolution_Config, ADC_Disable, ADC_Enable, ADC_Prescaler_Config, ADC_Referance_Config, ADC_ResultCh_GetLowByte(), ADC_Wait_32MHz(), offset, and SP_ReadCalibrationByte().

Referenced by ADC_Init().

242 {
243 uint8_t offset;
244
245 /* Set up ADC to get offset. */
246 ADC_ConvMode_and_Resolution_Config(adc, true, ADC_RESOLUTION_12BIT_gc);
247
248 ADC_Prescaler_Config(adc , ADC_PRESCALER_DIV8_gc);
249
250 ADC_Referance_Config(adc , ADC_REFSEL_INTVCC_gc);
251
252 ADC_Ch_InputMode_and_Gain_Config(&(adc->CH0),
253 ADC_CH_INPUTMODE_DIFF_gc,
254 ADC_CH_GAIN_1X_gc);
255
256 ADC_Ch_InputMux_Config(&(adc->CH0), ADC_CH_MUXPOS_PIN0_gc, ADC_CH_MUXNEG_PIN0_gc);
257
258 /* Enable ADC. */
259 ADC_Enable(adc);
260
261 /* Wait until ADC is ready. */
262 ADC_Wait_32MHz(adc);
263
264 /* Do one conversion to find offset. */
265 ADC_Ch_Conversion_Start(&(adc->CH0));
266
267 do{
268 }while(!ADC_Ch_Conversion_Complete(&(adc->CH0)));
269 offset = ADC_ResultCh_GetLowByte(&(adc->CH0), 0x00);
270
271 /* Disable ADC. */
272 ADC_Disable(adc);
273
274 return offset;
275 }
ADC_Disable
#define ADC_Disable(_adc)
This macro disables the selected adc.
Definition: adc_driver.h:89
ADC_ConvMode_and_Resolution_Config
#define ADC_ConvMode_and_Resolution_Config(_adc, _signedMode, _resolution)
This macro set the conversion mode and resolution in the selected adc.
Definition: adc_driver.h:110
ADC_Ch_InputMode_and_Gain_Config
#define ADC_Ch_InputMode_and_Gain_Config(_adc_ch, _inputMode, _gain)
This macro configures the input mode and gain to a specific virtual channel.
Definition: adc_driver.h:197
ADC_Wait_32MHz
void ADC_Wait_32MHz(ADC_t *adc)
This function waits until the adc common mode is settled.
Definition: adc_driver.c:214
ADC_ResultCh_GetLowByte
uint8_t ADC_ResultCh_GetLowByte(ADC_CH_t *adc_ch, uint8_t offset)
This function clears the interrupt flag and returns the low byte of the coversion result...
Definition: adc_driver.c:131
ADC_Prescaler_Config
#define ADC_Prescaler_Config(_adc, _div)
This macro set the prescaler factor in the selected adc.
Definition: adc_driver.h:127
ADC_Ch_Conversion_Complete
#define ADC_Ch_Conversion_Complete(_adc_ch)
This macro returns the channel conversion complete flag..
Definition: adc_driver.h:222
ADC_Ch_InputMux_Config
#define ADC_Ch_InputMux_Config(_adc_ch, _posInput, _negInput)
This macro configures the Positiv and negativ inputs.
Definition: adc_driver.h:212
ADC_Referance_Config
#define ADC_Referance_Config(_adc, _convRef)
This macro set the conversion referance in the selected adc.
Definition: adc_driver.h:137
ADC_Ch_Conversion_Start
#define ADC_Ch_Conversion_Start(_adc_ch)
This macro start one channel conversion.
Definition: adc_driver.h:265
ADC_Enable
#define ADC_Enable(_adc)
This macro enables the selected adc.
Definition: adc_driver.h:83
offset
volatile char offset
Definition: mc_drv.c:47
Here is the call graph for this function:

## ◆ ADC_ResultCh_GetHighByte()

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 funciton reads out the result.

Note
  If this function is used with 12-bit right adjusted results, it returns the 8 LSB only. Offset is not compensated.
  If ADC interrupts are enabled, the interrupt handler will clear the interrupt flag before the polling loop can detect it, and this function will hang forever. Therefore, do not use interrupts with this function. Instead, read the result registers directly.

Parameters

  adc_ch | Pointer to ADC channel register section.

Returns
  High byte of conversion result.

Definition at line 160 of file adc_driver.c.

161 {
162 /* Clear interrupt flag.*/
163 adc_ch->INTFLAGS = ADC_CH_CHIF_bm;
164
165 /* Return low byte result register contents.*/
166 return adc_ch->RESH;
167 }

## ◆ ADC_ResultCh_GetLowByte()

uint8_t ADC_ResultCh_GetLowByte | ( | ADC_CH_t * | adc_ch,
|  | uint8_t | offset
| ) |  |

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.

Note
  If this function is used with 12-bit right adjusted results, it returns the 8 LSB only.

Parameters

  adc_ch | Pointer to ADC channel register section.
  offset | Compansation offset value.

Returns
  Low byte of conversion result.

Definition at line 131 of file adc_driver.c.

References offset.

Referenced by ADC_Offset_Get().

132 {
133 uint8_t answer;
134
135 /* Clear interrupt flag.*/
136 adc_ch->INTFLAGS = ADC_CH_CHIF_bm;
137 /* Return result register contents*/
138 answer = adc_ch->RESL - offset;
139
140 return answer;
141 }
offset
volatile char offset
Definition: mc_drv.c:47

## ◆ ADC_ResultCh_GetWord()

uint16_t ADC_ResultCh_GetWord | ( | ADC_CH_t * | adc_ch,
|  | uint8_t | offset
| ) |  |

This function clears the interrupt flag and returns the 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.

Parameters

  adc_ch | Pointer to ADC channel register section.
  offset | Offset value that is compansated for.

Returns
  Conversion result.

Definition at line 98 of file adc_driver.c.

Referenced by mc_ADC_Scheduler().

99 {
100 uint16_t answer;
101 uint16_t signedOffset = (uint16_t) offset;
102
103 /* Append 16-bit signed value if offset (signed) is negative. */
104 if (offset >= 128){
105 signedOffset |= 0xFF00;
106 }
107
108 /* Clear interrupt flag.*/
109 adc_ch->INTFLAGS = ADC_CH_CHIF_bm;
110
111 /* Return result register contents*/
112 answer = adc_ch->RES - signedOffset;
113
114 return answer;
115 }
offset
volatile char offset
Definition: mc_drv.c:47

## ◆ ADC_Wait_32MHz()

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.

Note
  The function sets the prescaler to the minimum value possible when the clock speed is larger than 8 MHz to minimize the time it takes the common mode to settle.
  The ADC clock is turned off every time the ADC i disabled or the device goes into sleep (not Idle sleep mode).

Parameters

  adc | Pointer to ADC module register section.

Definition at line 214 of file adc_driver.c.

References COMMEN_MODE_CYCLES.

Referenced by ADC_Offset_Get().

215 {
216 /* Store old prescaler value. */
217 uint8_t prescaler_val = adc->PRESCALER;
218
219 /* Set prescaler value to minimum value. */
220 adc->PRESCALER = ADC_PRESCALER_DIV8_gc;
221
222 /* wait 8*COMMEN_MODE_CYCLES for common mode to settle*/
223 delay_us(8*COMMEN_MODE_CYCLES);
224
225 /* Set prescaler to old value*/
226 adc->PRESCALER = prescaler_val;
227 }
COMMEN_MODE_CYCLES
#define COMMEN_MODE_CYCLES
Definition: adc_driver.h:66

## ◆ ADC_Wait_8MHz()

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.

Note
  The function sets the prescaler to the minimum value to minimize the time it takes the common mode to settle. If the clock speed is higher than 8 MHz use the ADC_wait_32MHz function.

Parameters

  adc | Pointer to ADC module register section.

Definition at line 182 of file adc_driver.c.

References COMMEN_MODE_CYCLES.

Referenced by ADC_Init().

183 {
184 /* Store old prescaler value. */
185 uint8_t prescaler_val = adc->PRESCALER;
186
187 /* Set prescaler value to minimum value. */
188 adc->PRESCALER = ADC_PRESCALER_DIV4_gc;
189
190 /* Wait 4*COMMEN_MODE_CYCLES for common mode to settle. */
191 delay_us(4*COMMEN_MODE_CYCLES);
192
193 /* Set prescaler to old value*/
194 adc->PRESCALER = prescaler_val;
195 }
COMMEN_MODE_CYCLES
#define COMMEN_MODE_CYCLES
Definition: adc_driver.h:66