AVR 328P ADC sampling value is incorrect

Thread Starter

Young2

Joined Dec 7, 2020
90
Aref uses an external reference of 1.024v and the sampling channel is ADC1 with a sampling voltage of 0.5v. But no matter how the sampling voltage changes, the value sampled back by ADC is always 1023, where could the problem be?
Code:
 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
}
 
Top