@@ -334,24 +334,37 @@ float getVCCVoltage(void) {
334334}
335335
336336/*
337- * Read value of 1.1 volt internal channel using VCC as reference.
337+ * Or getVCCVoltageMillivolt() * 1023 / 1100
338+ */
339+ uint16_t getVCCVoltageReadingFor1_1VoltReference (void ) {
340+ checkAndWaitForReferenceAndChannelToSwitch (ADC_1_1_VOLT_CHANNEL_MUX, DEFAULT);
341+ uint16_t tVCC = readADCChannelWithReferenceOversample (ADC_1_1_VOLT_CHANNEL_MUX, DEFAULT, 2 );
342+ /*
343+ * Do not switch back ADMUX to enable checkAndWaitForReferenceAndChannelToSwitch() to work correctly for the next measurement
344+ */
345+ return ((1023L * 1023L ) / tVCC);
346+ }
347+
348+ /*
349+ * Read value of 1.1 volt internal channel using VCC (DEFAULT) as reference.
338350 * Handles reference and channel switching by introducing the appropriate delays.
339351 * !!! Resolution is only 20 millivolt !!!
340352 */
341353uint16_t getVCCVoltageMillivolt (void ) {
342- uint8_t tOldADMUX = checkAndWaitForReferenceAndChannelToSwitch (ADC_1_1_VOLT_CHANNEL_MUX, DEFAULT);
354+ checkAndWaitForReferenceAndChannelToSwitch (ADC_1_1_VOLT_CHANNEL_MUX, DEFAULT);
343355 uint16_t tVCC = readADCChannelWithReferenceOversample (ADC_1_1_VOLT_CHANNEL_MUX, DEFAULT, 2 );
344- ADMUX = tOldADMUX;
345356 /*
346- * Do not wait for reference to settle here, since it may not be necessary
357+ * Do not switch back ADMUX to enable checkAndWaitForReferenceAndChannelToSwitch() to work correctly for the next measurement
347358 */
348359 return ((1023L * 1100 ) / tVCC);
349360}
350361
351- void printVCCVoltageMillivolt (Print *aSerial) {
362+ uint16_t printVCCVoltageMillivolt (Print *aSerial) {
352363 aSerial->print (F (" VCC=" ));
353- aSerial->print (getVCCVoltageMillivolt ());
364+ uint16_t tVCCVoltageMillivolt = getVCCVoltageMillivolt ();
365+ aSerial->print (tVCCVoltageMillivolt);
354366 aSerial->println (" mV" );
367+ return tVCCVoltageMillivolt;
355368}
356369
357370/*
@@ -362,9 +375,8 @@ float getTemperature(void) {
362375 return 0.0 ;
363376#else
364377 // use internal 1.1 volt as reference
365- uint8_t tOldADMUX = checkAndWaitForReferenceAndChannelToSwitch (ADC_TEMPERATURE_CHANNEL_MUX, INTERNAL);
378+ checkAndWaitForReferenceAndChannelToSwitch (ADC_TEMPERATURE_CHANNEL_MUX, INTERNAL);
366379 float tTemp = (readADCChannelWithReferenceOversample (ADC_TEMPERATURE_CHANNEL_MUX, INTERNAL, 2 ) - 317 );
367- ADMUX = tOldADMUX;
368380 return (tTemp / 1.22 );
369381#endif
370382}
0 commit comments