Skip to content

Commit

Permalink
platform independent VCC measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Lehmann committed Dec 24, 2013
1 parent 70b58ef commit 3bb2e59
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
31 changes: 26 additions & 5 deletions opticalDensity.ino
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ float readSensor(int sensorPin)

// taken from
// http://hacking.majenko.co.uk/making-accurate-adc-readings-on-arduino
long readVcc() {
long readVcc_uno() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
Expand All @@ -85,11 +85,32 @@ long readVcc() {
}


// taken from https://code.google.com/p/tinkerit/wiki/SecretVoltmeter
//
//const long scaleConst = 1156.300 * 1000 ; // internalRef * 1023 * 1000;
const long scaleConst = 1125300L;
long readVcc() {
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif

delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA,ADSC)); // measuring

uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both

long result = (high<<8) | low;





result = scaleConst / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return (long)result; // Vcc in millivolts
}

0 comments on commit 3bb2e59

Please sign in to comment.