0

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

3
  • "The code inside while 1 works fine". There is no code inside the while(1) because it is just an empty while (1) ;, noticed the ; at the end? Commented Apr 2, 2024 at 14:26
  • @hcheung yes I know about that semi colon. I put that there so as to avoid contention between while(1) and ISR (in case the ISR is working). When I say while(1) works fine, I mean that the ; is removed and the code inside works fine. Should I remove this ; from post if it is creating a confusion.? Commented Apr 3, 2024 at 4:04
  • It certain confused me, especially when rest of code seems to be okay. In fact you mentioned both ATmega328P and ATmega2560, which is also confusing. Commented Apr 3, 2024 at 14:29

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)

2 Comments

I am using a logic analyzer to observe the generated signal.
@ManishVerma I use high side LED and multimeter to measure frequencies. Only way counter didn't work was when I set all the CS to 1 (so it was expecting external clock). Basically while must work in any case (unless it's not complete code and there is some other uncatched ISR)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.