Go Down

Topic: ShiftPWM and Tone Problem (Read 621 times) previous topic - next topic

dehlen

Hey, i got this ShiftPWM Library: https://github.com/elcojacobs/ShiftPWM/downloads
At the beginning of the code i got this statement:
Code: [Select]
#define SHIFTPWM_USE_TIMER2
If i just uncomment this i get this error message:
Code: [Select]
"The avr you are using does not have a timer3"
The problem is that i also want to play a tone on my piezo buzzer. Therefore i got this code:
Code: [Select]
for (int thisNote = 0; thisNote < 8; thisNote++) {
    int noteDuration = 1000/noteDurations[thisNote];
    tone(12, melody[thisNote],noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(12);
    }


Which is working fine in a single project, but combined with the shiftpwm library and its define statment i get the following error message:
Code: [Select]

core.a(Tone.cpp.o): In function `__vector_7':
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Tone.cpp:523: multiple definition of `__vector_7'
ShiftPWM_RGB_Example.cpp.o:/Users/david/Documents/Arduino/libraries/ShiftPWM/ShiftPWM.h:190: first defined here


So i went to the Tone.cpp file and i saw that there is also an timer2 statement which (in my opinion) causes this error.

I am new to this arduino stuff and i can make some minor bug fixes or something like that, but this error is too complicated for me at this time.
Do you have an idea how i can solve the puzzle ?

Btw if it helps you these are line 190 following from ShiftPW:
Code: [Select]
#elif defined(SHIFTPWM_USE_TIMER2)
//Install the Interrupt Service Routine (ISR) for Timer1 compare and match A.
ISR(TIMER2_COMPA_vect) {
ShiftPWM_handleInterrupt();
}

and line 523 following from Tone.cpp:
Code: [Select]
ISR(TIMER2_COMPA_vect)
{

  if (timer2_toggle_count != 0)
  {
    // toggle the pin
    *timer2_pin_port ^= timer2_pin_mask;

    if (timer2_toggle_count > 0)
      timer2_toggle_count--;
  }
  else
  {
    // need to call noTone() so that the tone_pins[] entry is reset, so the
    // timer gets initialized next time we call tone().
    // XXX: this assumes timer 2 is always the first one used.
    noTone(tone_pins[0]);
//    disableTimer(2);
//    *timer2_pin_port &= ~(timer2_pin_mask);  // keep pin low after stop
  }
}

Nick Gammon

The short answer is that both libraries are trying to use the same interrupt (Timer 2, compare match "A").

You can't always just combine libraries and have them work, if they both are trying to use the same piece of the hardware.

I don't really know the ShiftPWM library, but maybe it can be configured to use Timer 1, or something. It can be solved, but not necessarily by changing one or two lines of code.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

dehlen

Hm this is getting complicated... do you know a library which plays tone with timer1 ? ShiftPWM Library says it would use timer1 by default and just because i uncomment the define statement it would use timer 2. But if i uncomment the define statement again he tries to use timer3 and not timer1 by default... so maybe i should contact the developer of this library.
Quote
// ShiftPWM uses timer1 by default. To use a different timer, before '#include <ShiftPWM.h>', add  #define SHIFTPWM_USE_TIMER2  // for Arduino Uno and earlier (Atmega328)

Nick Gammon


Hey, i got this ShiftPWM Library: https://github.com/elcojacobs/ShiftPWM/downloads
At the beginning of the code i got this statement:
Code: [Select]
#define SHIFTPWM_USE_TIMER2


Where did you see that, exactly? I just downloaded the library and can't find that line.

Also what are you trying to do? This library seems very complex. Are you trying to fade LEDs and control a servo motor? Or what?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

elcojacobs

#4
Dec 08, 2012, 11:46 pm Last Edit: Dec 09, 2012, 03:18 am by elcojacobs Reason: 1
Hi,

Author of the library here. This error is my fault.

The library uses timer 1 by default, but I made an error in a recent update, which I corrected the next day:
https://github.com/elcojacobs/ShiftPWM/commit/809c749e22066bc66e7d673e4cce22142e0a7232

You downloaded the library at just the wrong moment.

This error made the compiler complain about not having a timer3, even when it was not used.

So just update to the newest version and you will be fine.

dehlen

Ah i see... thanks for the quick and easy help :) !

Go Up