Atmega16 crashing from interrupt

Discussion in 'Embedded Systems and Microcontrollers' started by allahjane, Jan 26, 2013.

  1. allahjane

    Thread Starter Member

    Sep 19, 2012
    75
    1
    I am using timer 2 in my atmega16 for a Compare match interrupt but for some reason the program crashes as soon as it reaches the statement sei();
    then it restarts again ..

    here's the code



    #define F_CPU 8000000
    Code ( (Unknown Language)):
    1. #include <avr/io.h>
    2. #include <util/delay.h>
    3. #include <avr/interrupt.h>
    4.  
    5. int main(void)
    6.  
    7. {
    8. DDRB=0xff;[COLOR="Blue"]
    9.                 PORTB=0xff;
    10.          _delay_ms(500);
    11.         PORTB=0;
    12.         _delay_ms(500);[/COLOR]
    13. TIMSK|=(1<<OCIE2);
    14. TCCR2|=(1<<WGM21)|(1<<CS22)|(1<<CS21);
    15.         [COLOR="Red"]sei();[/COLOR]
    16. OCR2=25;
    17.     while(1){
    18.     }
    19. }
    20.  
    21.  
    22. ISR(TIMER2_COMPA_vect){
    23.  
    24. }
    the code in blue which glows a LED for 500millisec one time goes on repeating forever indicating restarts

    if I remove the sei(); statement then its fine the LED glows only one time

    what is the problem here
     
  2. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    I can´t see any problem in there, are you sure you don´t have some other interrupt enabled?
     
  3. Papabravo

    Expert

    Feb 24, 2006
    10,178
    1,799
    It sounds like the processor is doing a soft RESET via the watchdog timer. Take a look at your fuse settings. Since the interrupt is null (consists of only a return from interrupt instruction) and the while loop is also null (has no statements) it is hard to imagine how you can tell the difference between "working" and "crashing".
     
  4. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    I think he can tell the difference between "staying in the while loop and doing nothing" and "blinking the LED on each reset".
     
  5. user007

    New Member

    Dec 16, 2011
    7
    0
    Try moving sei() to be the first line in main() and see if that changes anything.
     
  6. allahjane

    Thread Starter Member

    Sep 19, 2012
    75
    1
    [​IMG]

    It worked out !

    As the user above said making sei(); first statement in main function solves the reset problem
    Can't think of a possible explanation though!

    Does anyone know why it went wrong earlier
     
Loading...