Timer interrupt goes to boot sector on the ATMEGA 2560 (Arduino MEGA)

Dear Arduino Forum

This is my first time posting so please be gentle :slight_smile:

I have a problem when trying to do timer interrupts on my Arduino MEGA (ATMEGA 2560). The code is attached below.

This program should make 4 LED’s blink which are connected to the digital pins 37, 36 and 35. The only problem is that it doesn’t.

The byte that I deffine in the bootsector the register OCRB5 I set to zero in the setup section, and I never touched the OCRC5 (only when I increment it in the ICR).

The three variables previously mentioned all have an attached pin, which is low when they are under half their maximum and is high when they are above.
This should make the pins toggle and the LED’s conected to them blink.

However the pins (37, 36) connected to the variable (a) and OCRB5 never goes high, only the pin 35 blinks.

My theory is this:
When the timer inturrupt occurs it will not jump back to the code it left, but instead go to the boot sector and run that code again, essentially restarting the program, but not resseting the OCRC5.

I don’t know why though, and I am open to suggestions on how to fix this :wink:

I just tried inserting the code beneath if this does not work it is also put in as an attachment

Thank you in advance!

volatile uint8_t a = 0; // I use this to illustrate that the bootsector
                        // is run when an interrupt occurs

void setup() {

  TCCR5A = 0; //seting all the settings for the clock off.
  TCCR5B = 0;
  TCCR5C = 0;
  OCR5A = 1000; //This is when the interrupt happens
  OCR5B = 0; // I use this to illustrate that the setup is run too   

  TIMSK5 = (1<<OCIE5A);// enabels the inturrupt A on timer 5. 

  TCCR5B = 0x01; //I set the prescaler to 1 because the registers 
                 // OCR5B and OCR5C are huge and i want them to overflow
  sei(); //enable the global interrupts

  DDRC = 0xFF; //I am using port C and I set everything to output
  PORTC = 0x00; //I set everything low

void loop(){
  if(a > 130){
    PORTC |= (1 << PC0);
    PORTC &= ~(1 << PC0);

  if(OCR5B > 300000){
    PORTC |= (1 << PC1);
    PORTC &= ~(1 << PC1);

  if(OCR5C > 300000){
    PORTC |= (1 << PC2);
    PORTC &= ~(1 << PC2);


sketch_apr05b.ino (1.02 KB)

You may want to check these limits:

  if(OCR5B > 300000){

  if(OCR5C > 300000){