of Atmega ADC and supply reference

Eliminateur:
retrolefty,
i see, interesting code there...
so you use the Vcc as reference but select the bandgap as source, since BG is a known fixed value(thermal variations notwithstanding) you then use the measured difference to infer the real Vcc.
I like it, a lot!, it's much more elegant than my idea of using a external divider and analog pin, and much more precise.
I have some questions about it:

  1. I don't quite follow the mapping equation you did, why are you using *1023, shouldn't it be *1024 since you have the number of steps there?, why the +5 then /10?(maybe if put differently i could understand it, maybe it's my particular math view hehehe

Coding Badly came up with that math and it is effective as I've tested it with a precision adjustable power supply and found the compensation to be very good over quite a wide Vcc variation, 3.5 to 5.5 as I recall. In the original posting of this, as CB and I were developing it he did a great job of explaining the math. It's more then might meet the eye but I can't re-explain it as well as CB did (in his reply #13 in the link below). I just can't locate the original postings in the old site at the moment.
EDIT: Finally found the original posting thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1294478456/all

  1. about the bitshifts in ADMUX, aren't they backwards?, i mean, how to you shift a 0 by a value?, couldn't you just use a bitfield directly?(admux = 0101110b), since those macros correspond directly to bits, also, couldn't you use the _BV macro?

Again CB provided that, and it is correct. You can use admux = 0101110b, but using << and predefined bit names is the style used in most low level code on the Arduino platform.

I'd forgotten all about the direct MUX access for the ADC(and i even twiddled with it once to use the internal temp sensor, but gave up as it's horribly inaccurate and needs calibration), guess too much "analogread" dulled my AVR-fu (incidentally, it would be very nice if base arduino analogread included this calibration, maybe an "analogreadcal" or a callibration flag, or "analogcal()" function).

Keep in mind that for this method to get best accuracy results one needs to 'tweek' the actual bandgap voltage for the specific chip in service. While the bandgap voltage seems to be very stable it's actual nominal voltage is a pretty wide specification. Think of the "const long InternalReferenceVoltage = 1115L;" statment as where you would trim the function's actual results based on independent measurement/testing. CB came up with a independent sketch that would allow the bandgap voltage to appear on the Aref pin where one could read it with a good DVM and then use that value in this sketch. However I found it more fun to just 'adjust' the value while testing with real world voltages.

Hav you considered adding it to the playground?

No, I have never tried to post anything in the playground. While I do search the playground site out from time to time, I get very fustration trying to actually locate something in that section even when I know it's in there somewhere. It's layout leaves a lot to be desired in my opinion. Anyway, Coding Badly really was the key to getting the task simplified and understandable with the mux commands and compensation math, for this software challenged hardware person. I just originated the posting asking about if and how it could be done on an Arduion and then wrapped it around a simple sketch after testing it with decent voltage sources and test equipment. I first read of the method on the AVRfreaks site but there example code was not real Arduino sketch friendly. Anyone is free to put it on the playground if they wish and know how to.

Lefty