cnlenglan
cnlenglan
2017-11-09 12:24

AVR 328P ADC取样值不对,请教原因

  • avr studio

大家好,请教一个问题,Aref使用的外部基准1.024v,取样通道是ADC1,取样电压为0.5v。但是无论取样电压怎么变化,ADC取样回来的值总是1023,请问问题可能出在哪?

 unsigned int MCU_ADC_READ(unsigned char channel){
    unsigned char Sample_avgCn;
    unsigned int ret,tota=1;

    ADMUX&=~((1<<MUX0)|(1<<MUX1)|(1<<MUX2)|(1<<MUX3));//Clear MUX bits
    ADMUX|=channel;//MUX set

    ADCSRA|=(1<<ADSC)|(1<<ADEN);//Start a conversion
    while(ADCSRA&(1<<ADSC));//dump first result

    ADCSRA|=(1<<ADEN);
    for(Sample_avgCn=0;Sample_avgCn<8;Sample_avgCn++){//read 8 times
        ADCSRA|=(1<<ADSC);
        while(ADCSRA&(1<<ADSC));
        ret=ADCL;
        ret|=(unsigned int)(ADCH<<8);
        tota+=ret;
    }
    ADCSRA = 0;  //Turn OFF internal ADC
    return tota>>3;//averaging by 8
}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐

换一换