Trying to create a 30kHz square wave with a variable phase shift or delay in the signal rise using Teensy 4.0. I am able to see a signal using eFlexPWM on an oscilloscope however can't get the phase shift to work correctly. Is there a better option for creating a variable delayed square wave with consistent frequency and rise time? The following code is the closest I've got.
#include <Arduino.h>
#include <eFlexPwm.h>
using namespace eFlex;
const uint8_t PIN_A = 5; // PWM1_2_A
const uint8_t PIN_B = 6; // PWM1_2_B
SubModule pwmA(PIN_A);
SubModule pwmB(PIN_B);
void setup() {
Config cfg;
cfg.setClockSource(kPWM_BusClock); // 150 MHz clock
cfg.setPrescale(kPWM_Prescale_Divide_1); // No prescale
cfg.setMode(kPWM_EdgeAligned);
cfg.setReloadLogic(kPWM_ReloadPwmFullCycle);
cfg.setPwmFreqHz(30000); // 30 kHz
pwmA.configure(cfg);
pwmB.configure(cfg);
// Manually calculate MOD value (150MHz / 30kHz = 5000 ticks)
const uint16_t mod = 5000;
const uint16_t dutyTicks = mod / 2; // 50% duty
const uint16_t phaseTicks = mod / 4; // 25% phase shift = 90°
pwmA.setInitValue(0); // No phase shift
pwmA.updateDutyCycle(dutyTicks, ChanA);
pwmB.setInitValue(phaseTicks); // Shifted start
pwmB.updateDutyCycle(dutyTicks, ChanB);
// Start timers together — crucial for alignment
pwmA.timer().begin();
pwmB.timer().begin();
}
void loop() {
// Nothing here — PWM runs in hardware
}
#include <Arduino.h>
#include <eFlexPwm.h>
using namespace eFlex;
const uint8_t PIN_A = 5; // PWM1_2_A
const uint8_t PIN_B = 6; // PWM1_2_B
SubModule pwmA(PIN_A);
SubModule pwmB(PIN_B);
void setup() {
Config cfg;
cfg.setClockSource(kPWM_BusClock); // 150 MHz clock
cfg.setPrescale(kPWM_Prescale_Divide_1); // No prescale
cfg.setMode(kPWM_EdgeAligned);
cfg.setReloadLogic(kPWM_ReloadPwmFullCycle);
cfg.setPwmFreqHz(30000); // 30 kHz
pwmA.configure(cfg);
pwmB.configure(cfg);
// Manually calculate MOD value (150MHz / 30kHz = 5000 ticks)
const uint16_t mod = 5000;
const uint16_t dutyTicks = mod / 2; // 50% duty
const uint16_t phaseTicks = mod / 4; // 25% phase shift = 90°
pwmA.setInitValue(0); // No phase shift
pwmA.updateDutyCycle(dutyTicks, ChanA);
pwmB.setInitValue(phaseTicks); // Shifted start
pwmB.updateDutyCycle(dutyTicks, ChanB);
// Start timers together — crucial for alignment
pwmA.timer().begin();
pwmB.timer().begin();
}
void loop() {
// Nothing here — PWM runs in hardware
}