| BLDC control on ATAVRMC303 with ATxMega128A1

adc_driver.c
Go to the documentation of this file.
1 /* This file has been prepared for Doxygen automatic documentation generation.*/
67 #include "adc_driver.h"
68
69
77 void ADC_CalibrationValues_Set(ADC_t * adc)
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 }
87
88
98 uint16_t ADC_ResultCh_GetWord(ADC_CH_t * adc_ch, uint8_t offset)
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 }
116
117
131 uint8_t ADC_ResultCh_GetLowByte(ADC_CH_t * adc_ch, uint8_t offset)
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 }
142
160 uint8_t ADC_ResultCh_GetHighByte(ADC_CH_t * adc_ch)
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 }
168
182 void ADC_Wait_8MHz(ADC_t * adc)
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 }
196
197
214 void ADC_Wait_32MHz(ADC_t * adc)
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 }
228
241 uint8_t ADC_Offset_Get(ADC_t * adc)
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 }
276
277 #ifdef __GNUC__
278
287 uint8_t SP_ReadCalibrationByte( uint8_t index )
288 {
289 uint8_t result;
290
291 /* Load the NVM Command register to read the calibration row. */
292 NVM_CMD = NVM_CMD_READ_CALIB_ROW_gc;
293 result = pgm_read_byte(index);
294
295 /* Clean up NVM Command register. */
296 NVM_CMD = NVM_CMD_NO_OPERATION_gc;
297
298 return result;
299 }
300
301 #endif
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.
Definition: adc_driver.c:98
ADC_Offset_Get
uint8_t ADC_Offset_Get(ADC_t *adc)
This function get the offset of the ADC.
Definition: adc_driver.c:241
ADC_Disable
#define ADC_Disable(_adc)
This macro disables the selected adc.
Definition: adc_driver.h:89
ADC_CalibrationValues_Set
void ADC_CalibrationValues_Set(ADC_t *adc)
This function get the calibration data from the production calibration.
Definition: adc_driver.c:77
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...
Definition: adc_driver.c:160
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
COMMEN_MODE_CYCLES
#define COMMEN_MODE_CYCLES
Definition: adc_driver.h:66
ADC_Ch_Conversion_Start
#define ADC_Ch_Conversion_Start(_adc_ch)
This macro start one channel conversion.
Definition: adc_driver.h:265
adc_driver.h
XMEGA ADC driver header file.
SP_ReadCalibrationByte
uint8_t SP_ReadCalibrationByte(uint8_t index)
ADC_Wait_8MHz
void ADC_Wait_8MHz(ADC_t *adc)
This function waits until the adc common mode is settled.
Definition: adc_driver.c:182
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