I cannot immediately see your problem, but as I suggested before you would do better posting this on the AVR Freaks forum. There are few
embedded systems specialists here, let alone AVR specialists.
If you are stepping this with the debugger have you configured the debugger to stop the timer while the processor is halted? If not, every time you halt, a timer interrupt will become pending an the code in main may never run while stepping. Moreover if you break in the ISR, an interrupt will become pending and teh ISR may re-enter, eventually overflowing the call stack, and causing a crash.
The comment for the baud rate suggests you are running at 8MHz; so the Timer 0 overflow will occur at 32.768ms intervals, not 10ms as in the comment - that would explain the value of 153 in your ISR! If you are going to post the code for help, at least keep the comments consistent, since that only adds confusion!
Any how, you are making your timer far less flexible that necessary. I suggest that you make the 1 second event counter internal to the ISR (if you need it at all), and create a separate continuous counter for timing, as follows:
Code:
volatile unsigned char systick = 0 ;
ISR(TIMER0_OVF_vect)
{
static counter = 0 ;
tick++ ;
counter++ ;
// every second (approx)...
if ( counter == 31 )
{
blink_led() ;
counter = 0;
}
}
Then your timing look can look more like:
Code:
for(;;)
{
unsigned char start = systick ;
while( systick - start < 153 ) /* do nothing for 5 seconds (approx)*/ ;
...
}
Note that I have used an unsigned char for systick so that access is atomic. If you need a larger counter (this one is good for intervals of up to ~8.39 seconds), you will need to disable the interrupt to read systick, or use a mechanism where by you must read the same value twice in succession to validate it, for example:
Code:
unsigned int getSysTick()
{
int now ;
do
{
now = systick ;
} while( systick != now ) ;
return now ;
}
or
Code:
unsigned int getSysTick()
{
int now ;
TIMSK=0x00;
now = systick ;
TIMSK=0x01;
return now ;
}