Search code examples
microcontrollerwatchdogsleep-modehardwareattiny

ATTINY 85 SLEEP MODE


I am new to programming microcontrollers and I've just started using Attiny85. I am trying to build a circuit for LED with tactile switch. Every time the tactile switch is pressed it jumps the LED to next state of operation. Since it is battery operated, when the LED is OFF I want the attiny 85 to consume as low current as possible. As of now it is consuming 4mA when the LED is OFF without sleep mode. So I tried the power down mode for Attiny 85 but some how i am stuck in the power down mode

if(count == 8){
          analogWrite(0,LOW);
          //Serial.println("I am OFF");
          //Serial.println(count);
          set_sleep_mode(SLEEP_MODE_PWR_DOWN); //Power down everything
          sleep_mode();
          sleep_disable();
        }

It is successfully entering the sleep mode but i am not able to get out of it. Can please someone help. I want the Attiny 85 to get out of the sleep mode when the tactile switch is pressed again. The switch is on pin 7 i.e. PB2 of attiny 85.


Solution

  • Please refer to the datasheet, section 7.1 Sleep Modes at page 34.

    In the table you can see, in Power-down mode only 3 sources can wake up the CPU:

    1. INT0, only level interrupt and pin change interrupt
    2. USI module start condition
    3. Watchdog interrupt

    That means, if you want the part to wake up when button has been pressed, then best option will be to configure the pin change interrupt.

    First you need to configure an interrupt service routine (ISR). The ISR is required just to handle the interrupt event, without it the program will be restarted. Since there is no action required, the ISR can be empty:

    #include <avr/interrupt.h>
    
    EMPTY_INTERRUPT(PCINT0_vect);
    

    next you need to configure pin change interrupt (refer to the section 9.2 External Interrupts in the datasheet) E.g.:

    // I don't know which pin do you use for the button
    // Let's assue it is PB0 which corresponds to PCINT0 
    // (see section 1. Pin Configurations)
    PCMSK = (1 << PCINT0); // set pin change mask to PCINT0
    GIMSK = (1 << PCIE); // enable pin change interrupt
    sei(); // enable global interrupts
    

    That's all. After the interrupt is configured, any logical level change at the input will cause the CPU to wake up.