Problem waking up from SLEEP_MODE_PWR_SAVE using Timer 2 overflow interrupt

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include <avr/io.h> //avr library
#include <avr/interrupt.h> // library for interrupt
#include <util/delay.h> //delay library
#include <avr/sleep.h> //library for sleep

volatile uint8_t count=0;
volatile uint16_t counter=0;
volatile uint8_t led_is_on=0;
	
int main(void) //main starts

{	
	 //Initialize Counter
   TCNT2=0;
   
   //Enable Overflow Interrupt Enable
   TIMSK|=(1<<TOIE2);
   
      
	 // Prescaler = FCPU/1024
   TCCR2|=(1<<CS22)|(1<<CS21)|(1<<CS20);
    //wait to load
	//while (ASSR & ((1 << TCN2UB)|(1 << TCR2UB)));	
	
	//	TIFR &=~((1<<TOV2)|(1<<OCF2));  //flags cleared
   //Initialize our varriable
   count=0;

   /*All port-pins are set to OUTPUT*/
  DDRB = 0xff;
  DDRC = 0xff;
  DDRD = 0xff;

  /*All port-pins are set to LOW*/
  
  PORTB = 0x00;
  PORTC =0x00;
  PORTD = 0x00;
  
	set_sleep_mode(SLEEP_MODE_PWR_SAVE);	 //Powersave mode
		ADCSRA = 0; 
	 DDRC=0xff;

   //Enable Global Interrupts
	sei();
	
	while (1) //initialize while loop

	{	
		
		sleep_enable();
		//_SEI();
		//asm("sleep");
		//_SLEEP();
		//cli();
		sleep_cpu();
		
	//	sleep_disable();
		//sleep_mode();
		
		
	} // while loop ends
	
}//main end
	
ISR(TIMER2_OVF_vect)
{
   //This is the interrupt service routine for TIMER2 OVERFLOW Interrupt.
   //CPU automatically call this when TIMER2 overflows.

   //Increment our variable
   count++;
   if(count==230)     //setting count to 230 for 1 min delay
   {	count=0;
		counter++;
     if((counter==1) && (led_is_on==0)){
				led_is_on=1;
				counter=0;
				PORTC = PORTC | ( 1<<3) ;//pC4 of port C is high led
				
				
			}
			
			if((counter==1)&&(led_is_on==1))
			{
				led_is_on=0;
				counter=0;
				PORTC = PORTC & ~( 1<<3);
				
			}
   }
}

I am using Atmega8. I am trying to wake up mcu from power save mode using Timer 2 overflow interrupt.

I needed to introduce sleep in project for power optimization ,I read in datasheet that it can be woke up from sleep using either Timer2 overflow or compare interrupt.

I chose overflow interrupt But its not waking up.

Need your experienced insights in Atmega .

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Riario wrote:
/*All port-pins are set to OUTPUT*/ DDRB = 0xff; DDRC = 0xff; DDRD = 0xff;

Kind of ... interesting.

 

 TCCR2|=(1<<CS22)|(1<<CS21)|(1<<CS20);

So, you are using the system clock for Timer2?

 

What speed is your AVR running at?

 

What results do you get if you do not use sleep?

 

I might suggest, when working on this situation, to use your LED every overflow...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am using Atmega Internal clock of 1 Mhz, and using led at Port C pin 3 to check overflow .

If I dont use sleep code is working as expected led is getting on for 1 min and then off for next one min.

Thing I did with setting all ports as output and making them low is related to setting pins to high impedance state .

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Riario wrote:
Thing I did with setting all ports as output and making them low is related to setting pins to high impedance state .

Ummm--that is the opposite of high impedance.

 

Wakeup via timer2 in PowerSave mode is only when the timer is in asynchronous mode.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I read about ASSR  bit there is no need to set it if using internal 1 Mhz clock .

When AS2 is written to zero, Timer/Counter 2 is clocked from the I/O clock, clkI/O.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Riario wrote:
I read about ASSR bit there is no need to set it if using internal 1 Mhz clock . When AS2 is written to zero, Timer/Counter 2 is clocked from the I/O clock, clkI/O.

But the wakeup you are trying to use is only valid when timer2 is in asynchronous mode.  (most common is a "watch crystal" 32kHz)

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When working with sleep modes and wake up sources I always start with idle sleep mode, once it is working with idle mode, move to lower sleep modes.

Yes, T2 as a wakeup source needs async mode and external watch xtal. 

As for low power, drop the M8 and get a newer M88PA, much lower power draw!

 

Jim

 

Last Edited: Thu. Oct 26, 2017 - 06:23 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

When working with sleep modes and wake up sources I always start with idle sleep mode, once it is working with idle mode, move to lower sleep modes.

Yes, T2 as a wakeup source needs async mode and external watch xtal. 

As for low power, drop the M8 and get a newer M88PA, much lower power draw!

 

Jim

 

Thanks I will try to start from idle sleep mode and then move to lower sleep modes.

As for Timer 2 as wake up I thought Internal clock could be set in async mode.

But now I am clear from Datasheet that "The Timer/Counter can be clocked by an internal synchronous or an external asynchronous
clock source. The clock source clkT2 is by default equal to the MCU clock, clkI/O. When the AS2
bit in the ASSR Register is written to logic one, the clock source is taken from the Timer/Counter
Oscillator connected to TOSC1 and TOSC2."

Can  you please give me details on M88PA power difference compared with M8 haven't used it yet.

Or should I go for watchdog timer in Power Down Mode.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks now I understand that internal clock can't be used for Async mode and I have to use external 32khz clock for it.