I used the PWM library to generate two PWM signals at a desired frequency. The problem is that I am not able to phase shift the PWM signal. For example I am generating a PWM signal at a frequency of 30Hz on PIN 9 and i need a phase shift of 45 degrees of the same PWM on PIN 10. Anyone who knows how to come around this ?
P.S. I am using an arduino uno.
Thanks in advance.
#include "PWM.h"
int frequency1 = 120;
int frequency2 = 120;
int pin1 = 9; // Timer1 for pins 9 and 10 (One frequency for both pins)
int pin2 = 3; // Timer2 for pins 3 and 11
void setup() {
InitTimersSafe();
SetPinFrequencySafe(pin1,frequency1);
SetPinFrequencySafe(pin2,frequency2);
}
void loop() {
pwmWrite(pin1,127);
pwmWrite(pin2,127);
}
allanhurst:
I know it's clunky, but can't think of a better way offhand.
This if probably going to be beyond the OP if he needs to ask this, but could you use Timer1 and Timer2 in FastPWM mode and preload the TCNT value of one to to change the phase angle? You'd need to set TOP of Timer2 I imagine.
TOP must stay the same for both timers, but the timers have to be started with different counter (TCNT) values. If one starts in Fast PWM at BOTTOM and the other one at TOP/2, the phase shift is 180°.