1

I would like to use three timers in CTC mode to launch three Interrupt Service Routines at different frequencies.

In this application I need to use the servo library, millis() and micros() functions.

Timer3 (Working)

Thanks to this question, Timer3 has been set up in the following way:

  cli();          // disable global interrupts
  TCCR3A = 0;     // set entire TCCR3A register to 0
  TCCR3B = 0;     // same for TCCR3B

  // set compare match register to desired timer count:  @~744 Hz
  OCR3A = 20; 
  // turn on CTC mode:
  TCCR3B |= (1 << WGM32);
  // Set CS10 and CS12 bits for 1024 prescaler:
  TCCR3B |= (1 << CS30) | (1 << CS32);
  // enable timer compare interrupt:
  TIMSK3 |= (1 << OCIE3B);
  // enable global interrupts:
  sei();

  ISR(TIMER3_COMPB_vect)
  {
     cont++;
  }

Timer4 I've tried with this configuration:

cli();          // disable global interrupts
TCCR4A = 0;     // set entire TCCR3A register to 0
TCCR4B = 0;     // same for TCCR3B

// set compare match register to desired timer count: @ ~744 Hz
OCR4A = 20; 
// turn on CTC mode:
TCCR4B |= (1 << WGM42);
// Set CS10 and CS12 bits for 1024 prescaler:
TCCR4B |= (1 << CS40) | (1 << CS42);
// enable timer compare interrupt:
TIMSK4 |= (1 << OCIE4B);
//TIMSK4 |= (1 << OCIE4A); same issue
// enable global interrupts:
sei();

ISR(TIMER4_COMPB_vect) //TIMER4_COMPA_vect same issue
{
  cont++;
}

But the compiler returns an error caused by the servo library conflict:

Servo\Servo.cpp.o: In function '__vector_42': C:\Program Files (x86)\Arduino\libraries\Servo/Servo.cpp:117: multiple definition of '__vector_42' Timer4B_800Hz.cpp.o:C:\Program Files (x86)\Arduino/Timer4B_800Hz.ino:49: first defined here c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions

As Gerben suggested in a comment in this question:

They are just hogging all timers. I guess you have to alter the library slightly by removing the #define _useTimer3 line, or try putting a #undef _useTimer3 right after the include. – Gerben

I would avoid changing the servo library since motors play a critcal role in the application.

Timer 5 Same issue

Which timers and output compare registers can I use to set up the three ISR correctly?

4

1 Answer 1

1

The problem is the Servo library. This library is claiming all the timers, so you can't use them yourself. What I do to fix this is edit the Servo.h (or ServoTimers.h) file.

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
//#define _useTimer5
#define _useTimer1
#define _useTimer3
//#define _useTimer4
//typedef enum { _timer5, _timer1, _timer3, _timer4, _Nbr_16timers }   timer16_Sequence_t;
typedef enum { _timer1, _timer3, _Nbr_16timers } timer16_Sequence_t;

This will remove the 2 timers (4 and 5) for the Mega boards, but you can still attach 24 servo's. If you need more, you should edit this file again.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.