Understanding PWM phase shift code

Status
Not open for further replies.
I am trying to create several PWM signals (4) with the same frequency but slightly shifted from each other.

I found this code in an old forum post:
42158-PWM-phase-shift-help
is it possible to create 2 PWM-signals with one of them have a 90 degrees phase shift?
i am looking for a solution that uses the teensy's internal hardware timers.
i use a teensy 3.6.
Code:
void setup()
{
        FTM3_SC = 0;    // halt timer
        FTM3_COMBINE = FTM_COMBINE_COMBINE1; // ch 2+3 = adjustable high/low points
        FTM3_C0V = 59;  // high at 50% of period
        FTM3_C2V = 29;  // high at 25% of period
        FTM3_C3V = 89;  // low at 75% of period
        FTM3_CNT = 0;   // reset timer
        FTM3_MOD = 119; // timer period (MOD+1 clocks of F_BUS)
        FTM3_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0); // start timer
        // configure the pins to be controlled by FTM3 channels
        CORE_PIN2_CONFIG =  PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE; // ch0
        CORE_PIN7_CONFIG =  PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE; // ch2+3
}


void loop()
{
}
could someone explain to me what actually is done here?
I don't get several parts of this code. I am guessing the high and low periods are subdivisions of FTM_MOD so for a different mod those values would be different aswell.
We have to set the 75% low period but not the 50% low period because we want it to be adjustable thats why we need to use 2ch for the second pwm?

FTM3_MOD = 119; Is that the subdivision of the clockspeed (so clockspeed devided by 120)
Where is the phase shicht happening and how could one change it from 90degrees to lets say for fun 12 degrees
what is actually happening in the last two lines?

So if I would like to have a 200khz frequency I would need to set the mod to 299?
So a duty cycle of 50% would be 149?
A duty cycle of 1µs @ 200khz would look like this:
Code:
FTM3_COMBINE = FTM_COMBINE_COMBINE1;
FTM3_C2V = 60;  // 1µs high
FTM3_C3V = 239;  // 4µs low
FTM3_MOD = 299; // timer period (MOD+1 clocks of F_BUS)

How would you turn of the pwm signals and turn them back on in the main code? (keeping frequency phase shift and duty cycle)

would this Teensy 3.6 code be ported for a teensy 4.1?
 
Status
Not open for further replies.
Back
Top