I am experiencing somewhat a peculiar phenomenon. I am trying to get an ATtiny24A into sleep mode. I had a working code before, but through revisions and testing of other parts of my program it seems that the new code no longer sends the MCU into sleep.
I designed a breakout board for programming the ATtiny24a. The programming board has an onboard LED connected to pin 8 of the MCU. It also, has another LED connected to pin 13 of the MCU. You can see the design:

To only test the sleep mode operation of the MCU, I created a new file dedicated only to that feature. I initialize the pin 8 (onboard LED) as an output, and pin 13 as an input. I then initialize the interrupt configuration to trigger on pin change, pin 13, using a tactile button pressed (from an external breadboard). Following this, I turn on the LED for about 3 seconds, then the MCU should go into sleep mode. It doesn’t. If it did, I wanted it to be able to wake up using the interrupt and continue into the while loop in the main function. Where I have the LED blink every second. Also in the while loop, I have an integer variable that counts up, and once a condition is met, the MCU should go back to sleep. None of this works. Here is the code:
#define F_CPU 1000000UL
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>
#define LED PORTA5 //pin 8
#define BT PINA0 //pin 13
int main(void){
uint8_t clk = 0;
//SETUP IO PINS
DDRA |= 1 << LED; //setting up LED pin
DDRA &= ~(1 << BT); //setting up tactile button pin
//SETUP INTERRUPT
GIMSK |= 1 << PCIE0;
PCMSK0 |= 1 << PCINT0;
sei();
set_sleep_mode(SLEEP_MODE_PWR_DOWN); //setting sleep mode
PORTA |= 1 << LED; //initialize LED on
for(uint8_t i = 0; i < 3; i++ ){
_delay_ms(1000);
}
sleep_mode(); //enter sleep mode
while(1){
PORTA ^= 1 << LED;
_delay_ms(500);
if(clk > 5){
clk = 0;
sleep_mode();
}
clk += 1;
}
return 0;
}
ISR(PCINT0_vect, ISR_NAKED);
When I upload this code into the MCU. The LED stays on indefinitely, it never enters the first sleep instruction before the while loop.
I have checked the sleep and io file library content via github. I have verified that the functions and attributes are valid; also I don't receive any compiler warnings or errors.
Rather then using the functions and attributes I tried using bitwise operations to configure the register directly:
set_sleep_mode(SLEEP_MODE_PWR_DOWN); to MCUCR |= 1 << SM1;
and
sleep_mode(); to MCUCR |= 1 << SE; (this is more like sleep enabled).
With these replacements, the LED on the board stays on for 3 seconds then starts blinking indefinitely in the while loop. Ignoring the sleep instruction in the loop.
I have also tried using pointers and dereferencing to directly assigned the values to the register.
*((volatile unsigned char *) 0x55)) |= 1 << 4 4 representing the 4th bit (SM1) of the MCUCR register.
*((volatile unsigned char *) 0x55)) |= 1 << 5 5 representing the 5th bit (SE) of the MCUCR register.
It behaves the same as the above replacement.
I am going to continue to try to figure this out, but I do hope someone replies with a resolution and the reason why my code currently doesn't work. I ran other codes on the MCU, such as blinking the LEDs, Timer1 PWM output on pin 8, pin change interrupt trigger, and they have worked fine. I just can't get the MCU to enter sleep state anymore, even changing the MCU doesn't work.
