@@ -61,8 +61,8 @@ float sVCCVoltage;
6161uint16_t sVCCVoltageMillivolt ;
6262
6363// for isVCCTooLowMultipleTimes()
64- long sLastVoltageCheckMillis ;
65- uint8_t sVoltageTooLowCounter = 0 ;
64+ long sLastVCCCheckMillis ;
65+ uint8_t sVCCTooLowCounter = 0 ;
6666
6767/*
6868 * Conversion time is defined as 0.104 milliseconds by ADC_PRESCALE in ADCUtils.h.
@@ -155,7 +155,7 @@ uint8_t checkAndWaitForReferenceAndChannelToSwitch(uint8_t aChannelNumber, uint8
155155 /*
156156 * Switch reference from DEFAULT to INTERNAL
157157 */
158- delayMicroseconds (8000 ); // experimental value is >= 7600 us for Nano board and 6200 for UNO board
158+ delayMicroseconds (8000 ); // experimental value is >= 7600 us for Nano board and 6200 for Uno board
159159 } else if ((tOldADMUX & 0x0F ) != aChannelNumber) {
160160 if (aChannelNumber == ADC_1_1_VOLT_CHANNEL_MUX) {
161161 /*
@@ -361,6 +361,10 @@ uint16_t readUntil4ConsecutiveValuesAreEqual(uint8_t aChannelNumber, uint8_t aDe
361361 * !!! Function without handling of switched reference and channel.!!!
362362 * Use it ONLY if you only call getVCCVoltageSimple() or getVCCVoltageMillivoltSimple() in your program.
363363 * !!! Resolution is only 20 millivolt !!!
364+ * Raw reading of 1.1 V is 225 at 5 V.
365+ * Raw reading of 1.1 V is 221 at 5.1 V.
366+ * Raw reading of 1.1 V is 214 at 5.25 V (+5 %).
367+ * Raw reading of 1.1 V is 204 at 5.5 V (+10 %).
364368 */
365369float getVCCVoltageSimple (void ) {
366370 // use AVCC with (optional) external capacitor at AREF pin as reference
@@ -384,7 +388,7 @@ uint16_t getVCCVoltageMillivoltSimple(void) {
384388 * Similar to getVCCVoltageMillivolt() * 1023 / 1100
385389 */
386390uint16_t getVCCVoltageReadingFor1_1VoltReference (void ) {
387- uint16_t tVCC = waitAndReadADCChannelWithReference (ADC_1_1_VOLT_CHANNEL_MUX, DEFAULT); // 225 for 1.1 V at 5 V VCC
391+ uint16_t tVCC = waitAndReadADCChannelWithReference (ADC_1_1_VOLT_CHANNEL_MUX, DEFAULT);
388392 /*
389393 * Do not switch back ADMUX to enable checkAndWaitForReferenceAndChannelToSwitch() to work correctly for the next measurement
390394 */
@@ -402,6 +406,10 @@ float getVCCVoltage(void) {
402406 * Read value of 1.1 volt internal channel using VCC (DEFAULT) as reference.
403407 * Handles reference and channel switching by introducing the appropriate delays.
404408 * !!! Resolution is only 20 millivolt !!!
409+ * Raw reading of 1.1 V is 225 at 5 V.
410+ * Raw reading of 1.1 V is 221 at 5.1 V.
411+ * Raw reading of 1.1 V is 214 at 5.25 V (+5 %).
412+ * Raw reading of 1.1 V is 204 at 5.5 V (+10 %).
405413 */
406414uint16_t getVCCVoltageMillivolt (void ) {
407415 uint16_t tVCC = waitAndReadADCChannelWithReference (ADC_1_1_VOLT_CHANNEL_MUX, DEFAULT);
@@ -502,6 +510,12 @@ uint16_t getVoltageMillivoltWith_1_1VoltReference(uint8_t aADCChannelForVoltageM
502510#if !defined(VCC_EMERGENCY_STOP_MILLIVOLT)
503511#define VCC_EMERGENCY_STOP_MILLIVOLT 3000 // Many Li-ions are specified down to 3.0 volt
504512#endif
513+ #if !defined(VCC_TOO_HIGH_STOP_MILLIVOLT)
514+ #define VCC_TOO_HIGH_STOP_MILLIVOLT 5250 // + 5 % operation voltage
515+ #endif
516+ #if !defined(VCC_TOO_HIGH_EMERGENCY_STOP_MILLIVOLT)
517+ #define VCC_TOO_HIGH_EMERGENCY_STOP_MILLIVOLT 5500 // +10 %. Max recommended operation voltage
518+ #endif
505519#if !defined(VCC_CHECK_PERIOD_MILLIS)
506520#define VCC_CHECK_PERIOD_MILLIS 10000 // Period of VCC checks
507521#endif
@@ -517,40 +531,40 @@ bool isVCCTooLowMultipleTimes() {
517531 * Check VCC every VCC_CHECK_PERIOD_MILLIS (10) seconds
518532 */
519533
520- if (millis () - sLastVoltageCheckMillis >= VCC_CHECK_PERIOD_MILLIS) {
521- sLastVoltageCheckMillis = millis ();
534+ if (millis () - sLastVCCCheckMillis >= VCC_CHECK_PERIOD_MILLIS) {
535+ sLastVCCCheckMillis = millis ();
522536
523537# if defined(INFO)
524538 readAndPrintVCCVoltageMillivolt (&Serial);
525539# else
526540 readVCCVoltageMillivolt ();
527541# endif
528542
529- if (sVoltageTooLowCounter < VCC_CHECKS_TOO_LOW_BEFORE_STOP) {
543+ if (sVCCTooLowCounter < VCC_CHECKS_TOO_LOW_BEFORE_STOP) {
530544 /*
531545 * Do not check again if shutdown has happened
532546 */
533547 if (sVCCVoltageMillivolt > VCC_STOP_THRESHOLD_MILLIVOLT) {
534- sVoltageTooLowCounter = 0 ; // reset counter
548+ sVCCTooLowCounter = 0 ; // reset counter
535549 } else {
536550 /*
537551 * Voltage too low, wait VCC_CHECKS_TOO_LOW_BEFORE_STOP (6) times and then shut down.
538552 */
539553 if (sVCCVoltageMillivolt < VCC_EMERGENCY_STOP_MILLIVOLT) {
540554 // emergency shutdown
541- sVoltageTooLowCounter = VCC_CHECKS_TOO_LOW_BEFORE_STOP;
555+ sVCCTooLowCounter = VCC_CHECKS_TOO_LOW_BEFORE_STOP;
542556# if defined(INFO)
543557 Serial.println (F (" Voltage < " STR (VCC_EMERGENCY_STOP_MILLIVOLT) " mV detected -> emergency shutdown" ));
544558# endif
545559 } else {
546- sVoltageTooLowCounter ++;
560+ sVCCTooLowCounter ++;
547561# if defined(INFO)
548562 Serial.print (F (" Voltage < " STR (VCC_STOP_THRESHOLD_MILLIVOLT) " mV detected: " ));
549- Serial.print (VCC_CHECKS_TOO_LOW_BEFORE_STOP - sVoltageTooLowCounter );
563+ Serial.print (VCC_CHECKS_TOO_LOW_BEFORE_STOP - sVCCTooLowCounter );
550564 Serial.println (F (" tries left" ));
551565# endif
552566 }
553- if (sVoltageTooLowCounter == VCC_CHECKS_TOO_LOW_BEFORE_STOP) {
567+ if (sVCCTooLowCounter == VCC_CHECKS_TOO_LOW_BEFORE_STOP) {
554568 /*
555569 * 6 times voltage too low -> shutdown
556570 */
@@ -562,12 +576,35 @@ bool isVCCTooLowMultipleTimes() {
562576 return false ;
563577}
564578
579+
580+ /*
581+ * Return true if VCC_EMERGENCY_STOP_MILLIVOLT (3 V) reached
582+ */
583+ bool isVCCTooLow (){
584+ return (sVCCVoltageMillivolt < VCC_EMERGENCY_STOP_MILLIVOLT);
585+ }
586+
587+
565588void resetVCCTooLowMultipleTimes (){
566- sVoltageTooLowCounter = 0 ;
589+ sVCCTooLowCounter = 0 ;
567590}
568591
569- bool isVoltageTooLow (){
570- return (sVoltageTooLowCounter >= VCC_CHECKS_TOO_LOW_BEFORE_STOP);
592+ /*
593+ * Recommended VCC is 1.8 V to 5.5 V, absolute maximum VCC is 6.0 V.
594+ * Check for 5.25 V, because such overvoltage is quite unlikely to happen during regular operation.
595+ * Raw reading of 1.1 V is 225 at 5 V.
596+ * Raw reading of 1.1 V is 221 at 5.1 V.
597+ * Raw reading of 1.1 V is 214 at 5.25 V (+5 %).
598+ * Raw reading of 1.1 V is 204 at 5.5 V (+10 %).
599+ * @return true if 5 % overvoltage reached
600+ */
601+ bool isVCCTooHigh (){
602+ readVCCVoltageMillivolt ();
603+ return (sVCCVoltageMillivolt > VCC_TOO_HIGH_STOP_MILLIVOLT);
604+ }
605+ bool isVCCTooHighSimple (){
606+ readVCCVoltageMillivoltSimple ();
607+ return (sVCCVoltageMillivolt > VCC_TOO_HIGH_STOP_MILLIVOLT);
571608}
572609
573610/*
0 commit comments