Xmega Application Note | |||||
#include <string.h>
#include "adc.h"
Go to the source code of this file.
Defines | |
#define | SAMPLE_COUNT 100 |
Functions | |
void | adc_ch3_callback (ADC_t *adc, uint8_t ch, adc_result_t res) |
int | main (void) |
Variables | |
int16_t | adcSamples [4][SAMPLE_COUNT] |
uint16_t | interrupt_count = 0 |
int8_t | offset |
#define SAMPLE_COUNT 100 |
How many samples for each ADC channel.
Definition at line 5 of file asf_adc_example_polled.c.
void adc_ch3_callback | ( | ADC_t * | adc, | |
uint8_t | ch, | |||
adc_result_t | res | |||
) |
Callback routine that reads out the result form the conversion on all channels to a global array. If the number of conversions carried out is less than the number given in the define SAMPLE_COUNT a new conversion on all the channels is started. If not the interrupt on ADC A channel 3 is disabled.
Definition at line 110 of file asf_adc_example_interrupt.c.
References adc_disable(), adc_set_conversion_trigger(), ADC_TRIG_MANUAL, adc_write_configuration(), adcch_disable_interrupt(), adcch_get_signed_result(), adcch_write_configuration(), adcSamples, interrupt_count, offset, and SAMPLE_COUNT.
Referenced by main().
00111 { 00112 /* Read samples and clear interrupt flags. */ 00113 adcSamples[0][interrupt_count] = adcch_get_signed_result(&ADCA, 0) - offset; 00114 adcSamples[1][interrupt_count] = adcch_get_signed_result(&ADCA, 1) - offset; 00115 adcSamples[2][interrupt_count] = adcch_get_signed_result(&ADCA, 2) - offset; 00116 adcSamples[3][interrupt_count] = res - offset; 00117 00118 if(interrupt_count == SAMPLE_COUNT-1) 00119 { 00120 struct adc_channel_config adc_ch_conf_ch3; 00121 struct adc_config adc_conf; 00122 00123 memset(&adc_ch_conf_ch3, 0, sizeof(struct adc_channel_config)); 00124 memset(&adc_conf, 0, sizeof(struct adc_config)); 00125 00126 adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 4, 0); 00127 adc_write_configuration(&ADCA, &adc_conf); 00128 00129 adcch_disable_interrupt(&adc_ch_conf_ch3); 00130 adcch_write_configuration(&ADCA, 3, &adc_ch_conf_ch3); 00131 adc_disable(&ADCA); 00132 } 00133 00134 interrupt_count++; 00135 }
int main | ( | void | ) |
Definition at line 15 of file asf_adc_example_polled.c.
References ADC_CH0, adc_disable(), adc_enable(), ADC_REF_VCC, ADC_RES_12, adc_set_clock_rate(), adc_set_conversion_parameters(), adc_set_conversion_trigger(), ADC_SIGN_ON, adc_start_conversion(), ADC_TRIG_FREERUN_SWEEP, ADC_TRIG_MANUAL, adc_wait_for_interrupt_flag(), adc_write_configuration(), adcch_get_result, adcch_get_signed_result(), ADCCH_NEG_NONE, ADCCH_NEG_PIN1, ADCCH_POS_PIN1, ADCCH_POS_PIN4, ADCCH_POS_PIN5, ADCCH_POS_PIN6, ADCCH_POS_PIN7, adcch_set_input(), adcch_write_configuration(), adcSamples, offset, and SAMPLE_COUNT.
00016 { 00017 struct adc_config adc_conf; 00018 struct adc_channel_config adc_ch_conf_ch0; 00019 struct adc_channel_config adc_ch_conf_ch1; 00020 struct adc_channel_config adc_ch_conf_ch2; 00021 struct adc_channel_config adc_ch_conf_ch3; 00022 uint16_t ADCA_cal; 00023 00024 /* Move stored calibration values to ADC A. */ 00025 /* I can not find any function to insert this calibration data into the ADC 00026 ADCA_cal = adc_get_calibration_data(ADC_CAL_ADCA); */ 00027 00028 // Clear the configuration structures. 00029 memset(&adc_conf, 0, sizeof(struct adc_config)); 00030 memset(&adc_ch_conf_ch0, 0, sizeof(struct adc_channel_config)); 00031 memset(&adc_ch_conf_ch1, 0, sizeof(struct adc_channel_config)); 00032 memset(&adc_ch_conf_ch2, 0, sizeof(struct adc_channel_config)); 00033 memset(&adc_ch_conf_ch3, 0, sizeof(struct adc_channel_config)); 00034 00035 /* Set up ADC A to have signed conversion mode and 12 bit resolution. 00036 and set ref to internal VCC/1.6 V.*/ 00037 adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12, ADC_REF_VCC); 00038 00039 /* Set sample rate */ 00040 adc_set_clock_rate(&adc_conf, 200000UL); 00041 00042 /* Write ADC module configuration */ 00043 adc_write_configuration(&ADCA, &adc_conf); 00044 00045 /* Get offset value for ADC A. */ 00046 adcch_set_input(&adc_ch_conf_ch0, ADCCH_POS_PIN1, ADCCH_NEG_PIN1, 1); 00047 adcch_write_configuration(&ADCA, 0, &adc_ch_conf_ch0); 00048 00049 adc_enable(&ADCA); 00050 00051 /* Wait until common mode voltage is stable which take exactly one conversion */ 00052 adc_start_conversion(&ADCA, ADC_CH0); 00053 adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); 00054 00055 /* Get offset value */ 00056 adc_start_conversion(&ADCA, ADC_CH0); 00057 adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); 00058 offset = adcch_get_result(&ADCA, 0); 00059 adc_disable(&ADCA); 00060 00061 /* Setup channel 0, 1, 2 and 3 to have single ended input. 00062 Set input to the channels in ADC A to be PIN 4, 5, 6 and 7. */ 00063 adcch_set_input(&adc_ch_conf_ch0, ADCCH_POS_PIN4, ADCCH_NEG_NONE, 1); 00064 adcch_set_input(&adc_ch_conf_ch1, ADCCH_POS_PIN5, ADCCH_NEG_NONE, 1); 00065 adcch_set_input(&adc_ch_conf_ch2, ADCCH_POS_PIN6, ADCCH_NEG_NONE, 1); 00066 adcch_set_input(&adc_ch_conf_ch3, ADCCH_POS_PIN7, ADCCH_NEG_NONE, 1); 00067 00068 /* Write configuration to channels */ 00069 adcch_write_configuration(&ADCA, 0, &adc_ch_conf_ch0); 00070 adcch_write_configuration(&ADCA, 1, &adc_ch_conf_ch1); 00071 adcch_write_configuration(&ADCA, 2, &adc_ch_conf_ch2); 00072 adcch_write_configuration(&ADCA, 3, &adc_ch_conf_ch3); 00073 00074 /* Setup freerunning sweep of all four virtual channels.*/ 00075 adc_set_conversion_trigger(&adc_conf, ADC_TRIG_FREERUN_SWEEP, 4, 0); 00076 adc_write_configuration(&ADCA, &adc_conf); 00077 00078 /* Enable ADC A with free running mode, VCC reference and signed conversion.*/ 00079 adc_enable(&ADCA); 00080 00081 for(int i = 0; i < SAMPLE_COUNT; ++i) 00082 { 00083 adc_start_conversion(&ADCA, ADC_CH0); 00084 adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); 00085 adcSamples[0][i] = adcch_get_signed_result(&ADCA, 0) - offset; 00086 00087 adc_start_conversion(&ADCA, ADC_CH1); 00088 adc_wait_for_interrupt_flag(&ADCA, ADC_CH1); 00089 adcSamples[1][i] = adcch_get_signed_result(&ADCA, 1) - offset; 00090 00091 adc_start_conversion(&ADCA, ADC_CH2); 00092 adc_wait_for_interrupt_flag(&ADCA, ADC_CH2); 00093 adcSamples[2][i] = adcch_get_signed_result(&ADCA, 2) - offset; 00094 00095 adc_start_conversion(&ADCA, ADC_CH3); 00096 adc_wait_for_interrupt_flag(&ADCA, ADC_CH3); 00097 adcSamples[3][i] = adcch_get_signed_result(&ADCA, 3) - offset; 00098 } 00099 00100 adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 4, 0); 00101 adc_write_configuration(&ADCA, &adc_conf); 00102 adc_disable(&ADCA); 00103 00104 }
int16_t adcSamples[4][SAMPLE_COUNT] |
Sample storage (all four channels).
Definition at line 10 of file asf_adc_example_polled.c.
uint16_t interrupt_count = 0 |
Definition at line 12 of file asf_adc_example_polled.c.
int8_t offset |
Definition at line 13 of file asf_adc_example_polled.c.
Generated on Fri Oct 22 12:15:25 2010 for AVR1300 Using the Xmega ADC by ![]() |