[AVR] Cannot wake up Atmega 328p from PWR_DOWN by 32.768kHz crystal

Status
Not open for further replies.

xkram15

Newbie level 2
Joined
Oct 5, 2014
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
16
Hi,

I am trying to build low powered application. The concept is that 32.768kHz crystal is waking up Atmega328p every 8 seconds. I tried to follow several tutorials from Internet but unfortunately my microcontroller cannot wake up from SLEEP_MODE_PWR_DOWN or SLEEP_MODE_STANDBY modes. Wiring is pretty easy - there is 32.768kHz crystal between XTAL1 and XTAL2 and led on PC0. When I don't use sleep_cpu(); application works fine - TIMER2 interrupt is executed every 8 seconds. But once I put my microcontroller to a sleep it never wakes up.

Any ideas what can be wrong with my code?

Thanks a lot in advance!

Fuses: -U lfuse:w:0xc2:m -U hfuse:w:0xd6:m -U efuse:w:0x07:m
Code:
#include <avr/io.h> // deal with port registers
#include <util/delay.h> // used for _delay_us function
#include <stdlib.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include <avr/sleep.h> //Needed for sleep_mode
#include <avr/power.h> //Needed for powering down perihperals such as the ADC/TWI and Timers 
#include <avr/wdt.h>

#define F_CPU 					8000000
#define LED   				PC0 // Indikacni LED - blika, kdyz bezi odpocet


void msDelay(int delay) // put into a routine
{ // to remove code inlining
	for (int i=0;i<delay;i++) // at cost of timing accuracy
	_delay_ms(1);
}

void FlashLed(void)
{
		PORTC &= ~(_BV(LED)); //ON
		msDelay(50);
		PORTC |= _BV(LED);  //OFF
}

void Timer2Init(void)
{
	TCCR2B= (1<<CS22)|(1<<CS21)|(1<<CS20);
	TCNT2=0x00;
	OCR2B=0x00;
	ASSR = (1<<AS2); //Enable asynchronous operation
	TIMSK2 = (1<<TOIE2); //Enable the timer 2 interrupt
}

ISR(TIMER2_OVF_vect)
{
//	sleep_disable();
	asm volatile("nop"); 
//	FlashLed();
}

int main(void)
{
	DDRC |= _BV(LED);				//LED pin is output
	Timer2Init();

	while(1)
	{

	msDelay(100);
    set_sleep_mode (SLEEP_MODE_PWR_DOWN);  
//	set_sleep_mode (SLEEP_MODE_STANDBY);
	cli();
	sleep_enable();
	sei();
	sleep_cpu();
	sleep_disable();
	sei();
	FlashLed();
		}
	}
 

OK, I will reply myself. It's because datasheet says for Atmega328p: "When the asynchronous operation is selected, the 32.768kHz Oscillator for Timer/Counter2 is always running,
except in Power-down and Standby modes.
"

When I do use: " set_sleep_mode(SLEEP_MODE_PWR_SAVE);" it's working fine!
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…