Go Down

Topic: Small bug in wiring_analog.c concerning ATTiny and analog references (Read 2175 times) previous topic - next topic


If you search, you find a lot of folks suggesting the 2.56 volt analog reference using an analogReference value of 6.

That ostensibly works if you look at the ATTiny data sheet... until you look up a bit and notice that the three bits of that value are not next to each other.

The original wiring_analog.c says:

#if defined(ADMUX)
        ADMUX = (analog_reference << 6) | (pin & 0x07);

But that shifts the top bit of the analogReference() value out of the byte. The correct thing to do - at least on the ATTiny platform - is

#if defined(ADMUX)
        ADMUX = (analog_reference << 6) | (pin & 0x07) | ((analog_reference & 0x4)?0x10:0);

That is, REFS0 is bit 6 in the ADMUX word, REFS1 is bit 7, and REFS2 is bit FOUR.

See 17.13.1 in the ATTiny85 data sheet.

Go Up