这个程序是用查询方式进行单一的AD转换有以下特点:
采样在单一通道0上
无符号转换
12比特结果
内部1V参考电压
手动转换触发
查询方式
首先,新建ASF工程然后加入ADC模块,在没加入模块或加入后的ASF EXPLORER中打开快速指导。
根据提示形成程序,如下:
#include <asf.h>
#define MY_ADC ADCA
#define MY_ADC_CH ADC_CH0
static void adc_init(void)
{
struct adc_config adc_conf;
struct adc_channel_config adcch_conf;
adc_read_configuration(&MY_ADC, &adc_conf);
adcch_read_configuration(&MY_ADC, MY_ADC_CH, &adcch_conf);
adc_set_conversion_parameters(&adc_conf, ADC_SIGN_OFF, ADC_RES_12,
ADC_REF_BANDGAP);
adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
adc_set_clock_rate(&adc_conf, 200000UL);
adcch_set_input(&adcch_conf, ADCCH_POS_PIN0, ADCCH_NEG_NONE, 1);
adc_write_configuration(&MY_ADC, &adc_conf);
adcch_write_configuration(&MY_ADC, MY_ADC_CH, &adcch_conf);
}
int main (void)
{
/* Insert system clock initialization code here (sysclk_init()). */
sysclk_init();
adc_init();
uint16_t result;
while(1)
{
adc_enable(&MY_ADC);
adc_start_conversion(&MY_ADC, MY_ADC_CH);
adc_wait_for_interrupt_flag(&MY_ADC, MY_ADC_CH);
result = adc_get_result(&MY_ADC, MY_ADC_CH);
}
/* Insert application code here, after the board has been initialized. */
}
通过打断点可观察到结果:
|