Skip to main content
Arduino

Return to Question

replaced http://arduino.stackexchange.com/ with https://arduino.stackexchange.com/
Source Link

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

As Gerben suggested in a comment in this question question:

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

As Gerben suggested in a comment in this question:

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

As Gerben suggested in a comment in this question:

Tweeted twitter.com/#!/StackArduino/status/545643825671438336
Source Link
UserK
  • 559
  • 1
  • 11
  • 24

Using multiple timers and avoiding conflicts

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?

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /