Manish Verma
Manish Verma

Reputation: 21

AVR Timer Overflow ISR not running

MCU: Atmega328P IDE: Microchip Studio v7.0.2594 Toolchain: Atmel AVR 8-bit (C language) Native I am trying to run Timer0 Overflow interrupt. However, it seems like ISR is not getting executed. The Pin toggles inside while(1) but stays LOW when operated inside ISR Same kind of issues happen when I try to run Atmega2560 Timers. Here is my basic code. I have worked on many Atmega MCUs but this has never happened before. Note: Observing the signal pin using a Logic Analyzer.

#define F_CPU 16000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>


ISR(TIMER0_OVF_vect)
{
    PORTB ^= (1<<5);  //Toggle once in every 8.12ms
    TCNT0 = 127;
}
int main(void)
{
    /* Replace with your application code */
    DDRB = (1<<5);
    PORTB = 0;
    
    TCCR0A = 0;
    TCNT0 = 127;
    TIMSK0 = (1<<TOIE0);
    TIFR0 = 0;
    TCCR0B = (1<<CS02)|(1<<CS00); // CLKio Divide by 1024
    //Duration of 1 count = 16MHz/1024 = 15.625KHz = 64us   
    //Duration of 127counts = 64*127 = 8.12ms

    sei();
    
    while (1);
    {
        PORTB ^= (1<<5);
        _delay_ms(10);
    }
}

When I disable the interrupt using //sei(); or //TIMSK0 = (1<<TOIE0); The code inside while 1 works fine

Upvotes: 0

Views: 99

Answers (2)

Manish Verma
Manish Verma

Reputation: 21

After going through the datasheet of ATmega2560, it said: " The Interrupt Vectors can be moved to the start of the Boot Flash section by setting the IVSEL bit in the MCU Control Register (MCUCR). Refer to “Interrupts” on page 101 for more information. The Reset Vector can also be moved to the start of the Boot Flash section by programming the BOOTRST Fuse"

When I unprogrammed(set to 1) the BOOTRST bit of High Fuse Byte, it resolved the issue and the ISR is working now with the above code.

Upvotes: 1

KIIV
KIIV

Reputation: 3739

There is no issue with your code it's only blinking really fast - it's around 1kHz (so you'll have to move the led fast before your eyes to see it blinking).

Your code will be pulsing sligtly when you use clock divided by 1024, but with current divider 64 it's too fast to spot anything without that fast LED movement. (Or you can change led to lit on the high side and it'll be blinking too)

Upvotes: 0

Related Questions