Problems changing PWM with Timer3 library

Hello all,

I am having some trouble using the Timer3 library's pwm functions on the Arduino Mega. I want to create a pulse with a 12.5 us period. I want to start with an 80% duty cycle, but the rest of my software will automatically change the duty cycle based on other calculations.

Right now I can't get anything working but a 50% duty cycle. I also have another issue - when I set the period as 20 us, I get a 40 us period. When I set the period as 40 us, I get an 80 us period. However, when I set the period to 10 us or 12.5 us, I get a 32 us period. The Timer1 page from which I downloaded the Timer3 library says that the pwm has a minimum frequency of 1us, so I'm not below the minimum frequency.

Here is my code - all I want this to do right now is generate a pulse and be able to get the correct duty cycle and frequency.

include "TimerThree.h"

void setup() { pinMode(10, OUTPUT); Timer3.initialize(40); // initialize timer3 and set period Timer3.pwm(9, 820); // setup pwm on pin 9, 80% duty cycle Timer3.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt }

void callback() { digitalWrite(10, digitalRead(10) ^ 1); }

void loop () { }

I think I sort of understand what is happening -- I initialize Timer3 with a 40 us period. When it reaches 40us it overflows (and causes pin 10 to go high?) It then interrupts the routine, and goes to the callback function, which writes digital pin 10 to whatever value it became when Timer3 overflowed and keeps it that way for 40 us.

Is that why my period is always twice what I give it? Then the duty cycle would also make sense -- it's always 50% because it's high for one period and low for another period, making the apparent period twice the given period.

So -- is there an easy way to set the duty cycle using Timer3? Why is this code ignoring the command to set the duty cycle? And why can't I get a period that makes sense when I designate the period much lower than 40 us?

Any help is greatly appreciated. Thanks!