PWM complementary outputs

I now have this working, using the Standard Peripheral IO Library. The code placed in setup() looks like:

int freq = 2000;
int dutyCycle = 128;
pinMode(D3, OUTPUT);
pinMode(D2, OUTPUT);
analogWrite(D3, dutyCycle, freq); // Timer 3 channel 1
analogWrite(D2, dutyCycle, freq); // Timer 3 channel 2
// Now we want to invert he output of channel 2. Particle firmware doesn’t give access, use IO library
TIM_OC2PolarityConfig(TIM3, TIM_OCPolarity_Low);

Note that the Photon PWM outputs on D3, D2 use Timer 3 channels 1 & 2, which is what I use here. D0/D1 use Timer 4 channels 1&2, and the TX/RX pins use Timer1 channels 2&3 so the same strategy could be used for those. As noted earlier, these are true complementary outputs without a deadband.

2 Likes