Atmega 2560 ,Timer2, Output compare ,wake up from power save

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi my systems wakes up from power save mode on the atmega2560, but i cant get it do work correctly with the output compare mode. It is running on a external 32Khz clock and i want it to wakeup evrey 5 seconds. In the datasheet it says that a lot of preconditions must be taken when dealing with sleep modes, but i cant realy make sens of it.  When i set trough it interrupts the ISR(TIMER2_COMPB_vect) once but after that it never returns. maybe it sleeps forever? This is my settup : (ignore some of the comments written.)

(this does not work)

void Setup_timer(void)
{

TIMSK2 &= ~((1<<OCIE2B)||(1<<TOIE2)); /* 1. Disable the Timer/Counter2 interrupts by clearing OCIE2 and TOIE2. */
    //ASSR = (1<<EXCLK);    //set Timer/counter0 to be asynchronous from the CPU clock
    //with a second external clock (32,768kHz)driving it.
    ASSR |= (1<<AS2);
    TCNT2 = 0;    // clear TCNT2
    TCCR2A= (1<<WGM01) ;
    OCR2B=160; //=5 sec
    TCCR2B |=    (1<<CS22) | (1<<CS21)| (1<<CS20);    //prescaler 128=1 sec //(1<<CS21)|(1<<CS22) | (1<<CS20); //prescaler 1024=8sec 
    while ((ASSR & (1<<OCR2BUB)) ||ASSR & (1<<TCR2BUB));
    //while (ASSR & ((1<<TCN2UB)|(1<<OCR2BUB)|(1<<TCR2BUB)));
    TIFR2 |= (1<<OCF2B);
    TIMSK2 |= (1<<OCIE2B);

}

 

THis works: 

void Setup_timer(void)
{
    TIMSK2 &= ~(1<<TOIE2); /* 1. Disable the Timer/Counter2 interrupts by clearing OCIE2 and TOIE2. */
    //ASSR = (1<<EXCLK);    //set Timer/counter0 to be asynchronous from the CPU clock
    //with a second external clock (32,768kHz)driving it.
    ASSR |= (1<<AS2);
    TCNT2 = 0;    // clear TCNT2
    
    TCCR2B |=    (1<<CS22) | (1<<CS20);   //prescaler 128=1 sec //(1<<CS21)|(1<<CS22) | (1<<CS20); //prescaler 1024=8sec 
    while (ASSR & (1<<TCR2BUB));
    //while (ASSR & ((1<<TCN2UB)|(1<<OCR2BUB)|(1<<TCR2BUB)));
    TIFR2 |= (1<<TOV2);
    TIMSK2 |= (1<<TOIE2);
    
}

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

Mr_logger wrote:
 When i set trough it interrupts the ISR(TIMER2_COMPB_vect) once but after that it never returns. maybe it sleeps forever? This is my settup

Post a complete test program that demonstrates the symptoms.  Along with the usual schematic language toolchain version optimization level build settings ...

 

I hope that you are not going to sleep in an ISR, ans interrupts are disabled and you will sleep with the fishes forever.

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: 1

To get help with this you need to provide a complete small program that demos the problem, that way a freak can compile and test your code.

Use the "<>" code tag in the edit menu to upload your code, that way it will preserve indentation and format.

Remove any commented out code, before posting.

 

good luck with your project.

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

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

I guess ATMega2560's Timer2 is very similar to Timer2 ATMega328. In ATMega328 the non-working code will fail because

TCCR2A= (1<<WGM01) ;

sets the Timer into CTC mode - it is cleared to 0 each time it counts to OCR2A. Since the default value for OCR2A is 0 the counter value is constantly reset to 0 - meaning the counter never reaches OCR2B value and the interrupt is never triggered. You need to use OCR2A instead.