I'm using the Tiny84.
ADC clock = 125kHz
PWM is 10-bit Phase correct.
I have a routine that updates the ADMUX, starts a conversion, waits for it to complete, then writes the result to a variable. I'm outputting the ADC count to the OCR1A register, so I can see differences in my scope. I'm also using a PORTB register to trigger another pin for my scope so I can see when things are happening.
This first set of code gives ADC counts that are very irregular. The PWM duty is very jittery. From noise, etc, i'm sure.
That's why I built the for loop, to add averaging.
uint16_t adc2ct = 0; uint16_t adcsum = 0; int8_t i; void setreadADC2() { PORTB = 0b00000100; ADMUX = 0x82; // 0x82 = ADC2, 1.1 Vref adcsum = 0; i = 1; // for (i = 0; i < 1; i++) // { ADCSRA |= _BV(ADSC); while(ADCSRA & _BV(ADSC)); adcsum = adcsum + ADC; // } adc2ct = adcsum/i; PORTB = 0b00000000; }
However, if I just change the commenting, to the below. The ADC readings are rock solid.
uint16_t adc2ct = 0; uint16_t adcsum = 0; int8_t i; void setreadADC2() { PORTB = 0b00000100; ADMUX = 0x82; // 0x82 = ADC2, 1.1 Vref adcsum = 0; // i = 1; for (i = 0; i < 1; i++) { ADCSRA |= _BV(ADSC); while(ADCSRA & _BV(ADSC)); adcsum = adcsum + ADC; } adc2ct = adcsum/i; PORTB = 0b00000000; }
Isn't the adcsum still being divided by an int 1?