PWM with Timer1 Library not co-operating!

Hi

Im trying to use the timer1 library: Arduino Playground - Timer1 to generate interrupts and a PWM signal.
The interrupts are running fine however the PWM signal has issues.

I am creating a interrupt every 58us that reads data from buffer and changes the period of the PWM.
Heres the code:

void setup()
{
  bufferSetup();
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("***** Setting Up *****");
  Timer1.initialize(58);
  Timer1.attachInterrupt(timerInterupt,58);
  Timer1.pwm(TRACKP1,512,58);
}

void timerInterupt()
{
  char data = bufferGet();
  if(data == '1')
  {
      setOne();
  }
  else if(data == '0')
  {
      setZero();
  }
}

void setOne() 
{
  Timer1.setPwmPeriod(58);
  Timer1.setPwmDuty(TRACKP1, 512);
}
void setZero() 
{ 
  Timer1.setPwmPeriod(116);
  Timer1.setPwmDuty(TRACKP1, 512);
}

The issue is when the period changes the next bit i'm transmitting is malformed.
Its reasonably difficult to explain what is happening but it seems like the change of period resets the signal and starts it high again, resulting in a 87us high! (the 58us of the '0' bit and the 29us of the '1' bit)

If anyone has a way of fixing this I would greatly appreciate this as its halted my assignment in its tracks!

Thanks Anthony

it seems like the change of period resets the signal and starts it high again,

Yes it will.

If anyone has a way of fixing this

That is the way it works, so it is not going wrong, therefore there is nothing to fix. What is going wrong is your concept of the solution to your problem. As you haven't said what you problem actually is then it is a bit hard to offer any alternative solutions.

I don't know what you want to do with your code, but the PWM is for motors and so.

It is possible to change the frequency and the phase of the PWM output without any glitch. The ATmega328 has special PWM modes with a "double buffer" for updating the TOP of the timer. Read the datasheet of the ATmega328.

If you use a interrupt ISR to handle the signal, the interrupt doesn't have a steady pace. The Arduino uses already TIMER0 for millis(), and that interrupt is sometimes in the way.

but the PWM is for motors and so.

No you can generate passable audio from it as well.

Grumpy_Mike:
...No you can generate passable audio from it as well.

Yes, but that's the limit.

An interrupt every 58us, that's a very short time.
I have some timing in my SpeedTest of this sketch: Arduino Playground - HomePage
I'm looking at the code of the TimerOne library, and it used long variables (TIMER1 is 16-bit).
And then there is bufferGet(), of which I don't know how long it takes.
So that 58us might be too fast.

Yes, but that's the limit.

No I also got it to drive the 125KHz signal needed to drive an RFID matrix of readers.

If the timer1 library isn't co-operating you can go and change it to suit - this is an open source platform!