这个程序是中断方式来AD转换。128个全转换完成就产生中断,而结果存在缓冲数组里。
程序是根据快速指导建立的。
程序如下:
#include <asf.h>
struct adc_module adc_instance;
#define ADC_SAMPLES 128
uint16_t adc_result_buffer[ADC_SAMPLES];
volatile bool adc_read_done = false;
void adc_complete_callback(struct adc_module *const module);
void configure_adc(void);
void configure_adc_callbacks(void);
void adc_complete_callback(struct adc_module *const module)
{
adc_read_done = true;
}
void configure_adc(void)
{
struct adc_config config_adc;
adc_get_config_defaults(&config_adc);
#if (!SAML21)
config_adc.gain_factor = ADC_GAIN_FACTOR_DIV2;
#endif
config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV8;
config_adc.reference = ADC_REFERENCE_INTVCC1;
config_adc.positive_input = ADC_POSITIVE_INPUT_PIN6;
config_adc.resolution = ADC_RESOLUTION_12BIT;
adc_init(&adc_instance, ADC, &config_adc);
adc_enable(&adc_instance);
}
void configure_adc_callbacks(void)
{
adc_register_callback(&adc_instance,
adc_complete_callback, ADC_CALLBACK_READ_BUFFER);
adc_enable_callback(&adc_instance, ADC_CALLBACK_READ_BUFFER);
}
uint16_t result;
int main (void)
{
system_init();
configure_adc();
configure_adc_callbacks();
system_interrupt_enable_global();
adc_read_buffer_job(&adc_instance, adc_result_buffer, ADC_SAMPLES);
while (adc_read_done == false) {
/* Wait for asynchronous ADC read to complete */
}
while (1) {
/* Infinite loop */
}
}
运行结果,可以看到数组里的数:
|