From 6f04ab2dcb8f77e58000664439706c821da6c393 Mon Sep 17 00:00:00 2001 From: Stefan Arentz Date: Sun, 27 Dec 2009 22:04:06 -0500 Subject: [PATCH] Work in Progress --- tinyhacks-007-sleep/main.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tinyhacks-007-sleep/main.c b/tinyhacks-007-sleep/main.c index 449aa16..6706b5f 100644 --- a/tinyhacks-007-sleep/main.c +++ b/tinyhacks-007-sleep/main.c @@ -1,4 +1,4 @@ -// Blink2313 - Blink a LED on PD5 of a ATTiny 2313 +// tinyhacks-007-sleep/main.c #include #include @@ -9,34 +9,37 @@ volatile uint8_t count = 0; ISR(INT0_vect) { + // Disable the INT0 interrupt to prevent it from firing before we are ready to handle it again GIMSK &= ~(1 << INT0); + // Increment our interrupt counter count++; } int main(void) { + // PB0 is connected to a LED DDRB |= (1 << PB0); - MCUCR &= ~(1 << ISC00); - MCUCR &= ~(1 << ISC01); + + // Disable all internal systems during power down mode PRR = (1 << PRTIM1) | (1 << PRTIM0) | (1 << PRUSI) | (1 << PRADC); sei(); while (1) { - // Sleep + GIMSK |= (1 << INT0); // This needs to be before the sleep instruction + + // Sleep in power down mode. Before we sleep we enable the INT0 interrupt. set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); - GIMSK |= (1 << INT0); sleep_cpu(); // If we have been woken up 4 times then it is time to do 'work' - if (count == 4) + if (count >= 4) { // Flash the led three times - for (uint8_t i = 0; i < 3; i++) { PORTB |= (1 << PB0); _delay_ms(50); @@ -45,7 +48,6 @@ int main(void) } // Reset the counter - count = 0; }