AVR單片機(jī)ADC轉(zhuǎn)換程序
#defineAD_ref_vref0x00//基準(zhǔn)Vref
本文引用地址:http://www.ex-cimer.com/article/201611/315730.htm#defineAD_ref_avcc0x40//基準(zhǔn)AVCC
#defineAD_ref_vref_in0xc0//基準(zhǔn)內(nèi)部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<
//ADMUX=(1< //DIDR0=(1< } 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:選擇內(nèi)部2.56V參考電壓 delay_nus(100); for(x=0;x { ADCSRA|=(1< loop_until_bit_is_set(ADCSRA,ADIF);//方法1 等待AD轉(zhuǎn)換結(jié)束 //while ((ADCSRA&(1< //loop_until_bit_is_clear(ADCSRA,ADSC);//方法2 檢測(cè)ADSC=0也行 ADCSRA|=(1< y += ADC;//ADC=ADCH:ADCL } return y; }
評(píng)論