Atmega16 crashing from interrupt

Thread Starter

allahjane

Joined Sep 19, 2012
75
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
Rich (BB code):
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

int main(void)

{
DDRB=0xff;
                PORTB=0xff;
	     _delay_ms(500);
		PORTB=0;
		_delay_ms(500);
TIMSK|=(1<<OCIE2);
TCCR2|=(1<<WGM21)|(1<<CS22)|(1<<CS21);
		sei();
OCR2=25;
    while(1){
	}
}


ISR(TIMER2_COMPA_vect){

}
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
 

Papabravo

Joined Feb 24, 2006
22,111
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".
 

kubeek

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

Thread Starter

allahjane

Joined Sep 19, 2012
75
Try moving sei() to be the first line in main() and see if that changes anything.


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
 
Top