MsTimer2 killing a pwm pin

Hi, I'm using the MsTimer2 library in my Arduino 0017 Duemilanove Atmega328 to set a blinking initial time while it performs another tasks.

The problem is just after I stop the timer (MsTimer2::stop()), one (and only one) of my PWM pins doesn't respond to the analogWrite(PIN,value) function.

If I just remove the MsTimer2 library (using TimedAction for instance), the pin works fine.

Does anybody know what is the problem and how to fix it??

Thank you all for this great forum!

I haven't tried this but the call to MsTimer2::stop() and the function resetTimer2 should set the timer2 back to its initial state

#include <MsTimer2.h>

// Switch on LED on pin 13 each second


void flash() {
  static boolean output = HIGH;
  
  digitalWrite(13, output);
  output = !output;
}

void setup() {
  pinMode(13, OUTPUT);

  MsTimer2::set(500, flash); // 500ms period
  MsTimer2::start();
  delay(2000);
  MsTimer2::stop();
  resetTimer2();
}

void loop() {

  // add pwm test here....
}

void resetTimer2()
{
        // set timer 2 prescale factor to 64
#if defined(__AVR_ATmega8__)
        TCCR2 |= (1<<CS22);
#else
       TCCR2B |= (1<<CS22);
#endif
      // configure timer 2 for phase correct pwm (8-bit)
#if defined(__AVR_ATmega8__)
      TCCR2 |= (1<<WGM20);
#else
      TCCR2A |= (1<<WGM20);
#endif
  
}

Is your non functioning PWM pin either pins 3 or 11?

Mstimer2 uses timer2 which is the timer that also controls PWM writes to pins 3 and 11, so it's just a conflicting of use of the timer.

http://www.arduino.cc/playground/Main/TimerPWMCheatsheet

Lefty

Lefty, the code posted above should disable msTimer2 and restore PWM to the timer2 pins.

At first I used pin #3. As I had in use also pwm pins 5, 6, 9 and 10, I tried pin #11.

In both cases they failed, as you've just explained, Lefty.

As I don't have any free pwm pins I guess I have to try mem's solution.

I'll let you know.

Thanks both of you for your fast responses!

Lefty, the code posted above should disable msTimer2 and restore PWM to the timer2 pins.

Yes, I was composing while you posted. I didn't attempt to fix his problem, just to explain the why of the problem he was seeing.

Lefty