ISR not uploading

Hi, when I try to upload my code with both ISR(TIMER0_OVF_vect) along with ISR(TIMER1_COMPA_vect) in my arduino, it keeps in giving me an error and I do not know why. When I remove it however, it works fine with just the ISR(TIMER1_COMPA_vect). Does anyone know why? Many thanks

#include <SoftwareSerial.h> 
#include <avr/io.h>

int i=0;
int r_pin = A0;
int sample_output;
int Sfreq = 1;
unsigned long Timer_count;

void setup (){
  noInterrupts();
  
  Serial.begin (9600);
  ADCSRA |= (1 << ADPS0) |  (1 << ADPS1) | (1 << ADPS2);  // ADC Prescaler of 128

// ------ For Sampling Frequency ------ //
  //set timer1 interrupt at Sfreq Hz
  TCCR1A = 0;// set entire TCCR1A register to 0
  TCCR1B = 0;// same for TCCR1B
  TCNT1  = 0;//initialize counter value to 0
  // set compare match register for 1hz increments
  OCR1A = 156;// = (16*10^6) / (Sfreq*Timer Prescaler) - 1 (must be <65536)
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS12 and CS11 and CS10 bits for prescaler <----
  TCCR1B |= (1 << CS12) | (1 << CS10); //-- 1024
//  TCCR1B |= (1 << CS12);               //-- 256
//  TCCR1B |= (1 << CS11) | (1 << CS10); //-- 64
//  TCCR1B |= (1 << CS11);               //-- 8
//  TCCR1B |= (1 << CS10);               //-- 1

  // enable timer COMPARE interrupt
  TIMSK1 |= (1 << OCIE1A);


// ----- Timer Overflow ----- //
  TCCR0A = 0;
  TCCR0B = 0;
  
  TCNT0 = 250;                                // preload timer (65536-16MHz/256/10Hz)
  TCCR0B |= (1 << CS00) | (1 << CS02);        // 1024 prescaler 
  TIMSK0 |= (1 << TOIE0);                     // enable timer OVERFLOW interrupt

  interrupts();
}  // end of setup

ISR(TIMER1_COMPA_vect){
// code
}
ISR(TIMER0_OVF_vect) {
// code
}

void loop (){}

Does it work when you comment this out: #include <SoftwareSerial.h>

Anyway, post the error message.

saaqib_m:
Does anyone know why? Many thanks

Yes. The Arduino core uses the Timer0 overflow interrupt for millis() and micros(), Switching to Timer2 instead of Timer0 works around the problem.

#include <SoftwareSerial.h>


int i = 0;
int r_pin = A0;
int sample_output;
int Sfreq = 1;
unsigned long Timer_count;


void setup ()
{
  noInterrupts();


  Serial.begin (9600);


  ADCSRA |= (1 << ADPS0) |  (1 << ADPS1) | (1 << ADPS2);  // ADC Prescaler of 128


  // ------ For Sampling Frequency ------ //
  //set timer1 interrupt at Sfreq Hz
  TCCR1A = 0;// set entire TCCR1A register to 0
  TCCR1B = 0;// same for TCCR1B
  TCNT1  = 0;//initialize counter value to 0
  // set compare match register for 1hz increments
  OCR1A = 156;// = (16*10^6) / (Sfreq*Timer Prescaler) - 1 (must be <65536)
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS12 and CS11 and CS10 bits for prescaler <----
  TCCR1B |= (1 << CS12) | (1 << CS10); //-- 1024
  //  TCCR1B |= (1 << CS12);               //-- 256
  //  TCCR1B |= (1 << CS11) | (1 << CS10); //-- 64
  //  TCCR1B |= (1 << CS11);               //-- 8
  //  TCCR1B |= (1 << CS10);               //-- 1


  // enable timer COMPARE interrupt
  TIMSK1 |= (1 << OCIE1A);




  // ----- Timer Overflow ----- //
  TCCR2A = 0;
  TCCR2B = 0;


  TCNT2 = 250;                                // preload timer (65536-16MHz/256/10Hz)
  TCCR2B |= (1 << CS20) | (1 << CS22);        // 1024 prescaler
  TIMSK2 |= (1 << TOIE2);                     // enable timer OVERFLOW interrupt


  interrupts();
}  // end of setup


ISR(TIMER1_COMPA_vect)
{
  // code
}
ISR(TIMER2_OVF_vect)
{
  // code
}


void loop () {}

johnwasser:
Yes. The Arduino core uses the Timer0 overflow interrupt for millis() and micros(), Switching to Timer2 instead of Timer0 works around the problem.

Hi John, thanks! That has worked perfectly!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.