@@ -91,7 +91,7 @@ void hwInternalSleep(unsigned long ms) {
91
91
if (!pinIntTrigger && ms >= 125 ) { hwPowerDown (SLEEP_120MS); ms -= 120 ; }
92
92
if (!pinIntTrigger && ms >= 64 ) { hwPowerDown (SLEEP_60MS); ms -= 60 ; }
93
93
if (!pinIntTrigger && ms >= 32 ) { hwPowerDown (SLEEP_30MS); ms -= 30 ; }
94
- if (!pinIntTrigger && ms >= 16 ) { hwPowerDown (SLEEP_15Ms ); ms -= 15 ; }
94
+ if (!pinIntTrigger && ms >= 16 ) { hwPowerDown (SLEEP_15MS ); ms -= 15 ; }
95
95
}
96
96
97
97
int8_t hwSleep (unsigned long ms) {
@@ -132,6 +132,54 @@ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mo
132
132
return retVal;
133
133
}
134
134
135
+ uint16_t hwCPUVoltage () {
136
+ // Measure Vcc against 1.1V Vref
137
+ #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
138
+ ADMUX = (_BV (REFS0) | _BV (MUX4) | _BV (MUX3) | _BV (MUX2) | _BV (MUX1));
139
+ #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
140
+ ADMUX = (_BV (MUX5) | _BV (MUX0));
141
+ #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
142
+ ADMUX = (_BV (MUX3) | _BV (MUX2));
143
+ #else
144
+ ADMUX = (_BV (REFS0) | _BV (MUX3) | _BV (MUX2) | _BV (MUX1));
145
+ #endif
146
+ // Vref settle
147
+ delay (70 );
148
+ // Do conversion
149
+ ADCSRA |= _BV (ADSC);
150
+ while (bit_is_set (ADCSRA,ADSC));
151
+ // return Vcc in mV
152
+ return (1125300UL ) / ADC;
153
+ }
154
+
155
+ uint8_t hwCPUFrequency () {
156
+ noInterrupts ();
157
+ // setup timer1
158
+ TIFR1 = 0xFF ;
159
+ TCNT1 = 0 ;
160
+ TCCR1A = 0 ;
161
+ TCCR1C = 0 ;
162
+ // save WDT settings
163
+ uint8_t WDTsave = WDTCSR;
164
+ wdt_enable (WDTO_500MS);
165
+ // enable WDT interrupt mode => first timeout WDIF, 2nd timeout reset
166
+ WDTCSR |= (1 << WDIE);
167
+ wdt_reset ();
168
+ // start timer1 with 1024 prescaling
169
+ TCCR1B = _BV (CS12) | _BV (CS10);
170
+ // wait until wdt interrupt
171
+ while (bit_is_clear (WDTCSR,WDIF));
172
+ // stop timer
173
+ TCCR1B = 0 ;
174
+ // restore WDT settings
175
+ wdt_reset ();
176
+ WDTCSR |= (1 << WDCE) | (1 << WDE);
177
+ WDTCSR = WDTsave;
178
+ interrupts ();
179
+ // return frequency in 1/10MHz (accuracy +- 10%)
180
+ return TCNT1 * 2048UL / 100000UL ;
181
+ }
182
+
135
183
136
184
137
185
#ifdef MY_DEBUG
0 commit comments