//基准定义
#defineAD_ref_vref0x00//基准Vref
#defineAD_ref_avcc0x40//基准AVCC
#defineAD_ref_vref_in0xc0//基准内部Vref
#defineAD_refAD_ref_vref
//常量定义
//单端通道,不放大
#define AD_SE_ADC0 0x00//ADC0
#define AD_SE_ADC1 0x01//ADC1
#define AD_SE_ADC2 0x02//ADC2
#define AD_SE_ADC3 0x03//ADC3
#define AD_SE_ADC4 0x04//ADC4
#define AD_SE_ADC5 0x05//ADC5
#define AD_SE_ADC6 0x06//ADC6
#define AD_SE_ADC7 0x07//ADC7
//定义AD端口变量
#define AD_TEMPAD_SE_ADC0//ADC0
#define AD_VOUTAD_SE_ADC1//ADC1
#define AD_KEYINAD_SE_ADC2//ADC2
#defineAD_VINAD_SE_ADC3//ADC3
#define AD_IOUTAD_SE_ADC4//ADC4
#define AD_6BATAD_SE_ADC5//ADC5
#defineBAT_SE_PORTPORTB
#defineBAT_EN_PORTPORTB
#defineBAT_EN_PIN0X03
void adc_init(void)
{
DDRA=0x00;
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(0<<ADPS1)|(1<<ADPS0);//32分频
//ADMUX=(1<<REFS0);
//DIDR0=(1<<ADC5D)|(1<<ADC4D)|(1<<ADC3D)|(1<<ADC2D)|(1<<ADC1D)|(1<<ADC0D);//数据端口禁止
}
unsigned int read_adc(unsigned char adc_input,unsigned char i)//查询方式读取ADC单端通道
{
unsigned charx;
unsigned inty=0;
ADC=0;
ADMUX=(AD_ref|adc_input);//adc_input:单端通道 0x00~0x07,0x1E,0x1F
//0xc0:选择内部2.56V参考电压
delay_nus(100);
for(x=0;x<i;x++)
{
ADCSRA|=(1<<ADSC);//启动AD转换
loop_until_bit_is_set(ADCSRA,ADIF);//方法1 等待AD转换结束
// while ((ADCSRA&(1<<ADIF))==0);//写法2 这种写法优化不好
// loop_until_bit_is_clear(ADCSRA,ADSC);//方法2 检测ADSC=0也行
ADCSRA|=(1<<ADIF);//写1清除标志位
y += ADC;//ADC=ADCH:ADCL
}
return y;
}