added explaination on how to fine-tune VccMeter. #changelog

Reshuffled some of the other parts of the readVcc function
moved MUX setup to the void setup function
This commit is contained in:
pyr0ball 2019-09-24 19:33:22 -07:00
parent cc0b19d391
commit 2bbc20ff4d
3 changed files with 30 additions and 13 deletions

View file

@ -87,6 +87,20 @@ void setup() {
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(Z_TRG), pulse, FALLING);
// Atmega's Secret Voltmeter setup:
// 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
Serial.println("Initializing Pyr0-Piezo Sensor...");
}

View file

@ -32,7 +32,7 @@
long voltMeterConstant = 1125300L; // For fine tuning input voltage sense
#endif
#ifdef I2C
#ifdef I2C_INPUT
#if !(defined(pP_i2c_address))
byte pP_i2c_address = 0xa0; // I2C Bus Address
#endif

View file

@ -25,18 +25,6 @@ void pulse() {
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
@ -49,6 +37,21 @@ long readVcc() {
return result; // Vcc in millivolts
}
/* The above function assumes an "ideal" multiplier constant.
Each Atmega chip is slightly different, so it won't be completely accurate
without tuning. Most of the time this won't be necessary, so don't mess
with this if you don't know what you're doing!
The reading can be fine-tuned by using a multimeter, and this equasion:
scale_constant = internal1.1Ref * 1023 * 1000
where
internal1.1Ref = 1.1 * Vcc1 (per voltmeter) / Vcc2 (per readVcc() function)
If the scale_constant calculated is different from the default 1125300,
update the voltMeterConstant variable in pP_config.h with the correct value*/
/*------------------------------------------------*/
void adjustVin() {