Bonjour à tous,
J'ai pioché des bouts de code et en les assemblant : problème
je lis la tension d'alimentation : ok
je fais passer la puce en deepsleep : ok
je sors du deepsleep
je lis la tension : elle ne change pas, pourtant l'alim baisse (accu) au cours du temps.
je redémarre la vraie valeur s'affiche avant le passage en deepsleep puis même scénario
Comment réveiller totalement la puce pour qu'elle lise bien la tension ?
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
volatile uint8_t wdt_count;
volatile uint8_t tensionInter=0;
void setup()
{
pinMode(2, OUTPUT);
long voltage = readVcc();
ADCSRA &= ~_BV(ADEN); // switch ADC OFF
ACSR |= _BV(ACD); // switch Analog Compartaror OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Configure attiny85 sleep mode
// Reset watchdog interrupt counter
wdt_count = 255; } //max value
void loop(void)
{ wdt_count = 0;
watchdog_start_interrupt(6); // prescale of 6 ~= 1sec
while(wdt_count < 20) // Wait 10 watchdog interupts (~20secs)
{ sleep_mode(); } // Make CPU sleep until next WDT interrupt
watchdog_stop(); // réveil
long voltage = readVcc(); // tension de la batterie
}
long readVcc()
{ ADMUX = _BV(MUX3) | _BV(MUX2);
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 = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; } // Vcc in millivolts
void enterSleep (void)
{ sleep_enable();
sleep_cpu(); }
void watchdog_stop() /* Turn off WDT */
{ WDTCR |= _BV(WDCE) | _BV(WDE);
WDTCR = 0x00; }
void watchdog_start_interrupt(uint8_t wd_prescaler)
{ if(wd_prescaler > 9) wd_prescaler = 9;
byte _prescaler = wd_prescaler & 0x7;
if (wd_prescaler > 7 ) _prescaler |= _BV(WDP3);
// ^ fourth bit of the prescaler is somewhere else in the register...
// set new watchdog timer prescaler value
WDTCR = _prescaler;
WDTCR |= _BV(WDIE) | _BV(WDCE) | _BV(WDE); } // start timed sequence
ISR(WDT_vect) // Watchdog Interrupt Service / is executed when watchdog timed out
{ wdt_count++;
WDTCR |= _BV(WDIE); } // Watchdog goes to interrupt not reset