Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 11 of 11

Thread: PWM phase shift - help

  1. #1
    Junior Member
    Join Date
    Feb 2017
    Posts
    8

    PWM phase shift - help

    hi,

    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.

    kind regards
    manuel

  2. #2
    Yes. Depending on the frequency and accuracy you need there would be different ways to do it.
    The Library interval timer gives you pretty robust access to up to 4 hardware timers.
    What frequency/PWM step size/accuracy were you looking for?

  3. #3
    Junior Member
    Join Date
    Feb 2017
    Posts
    8
    i need two 500kHz PWMs with 50% duty cycle.

  4. #4
    Member
    Join Date
    Oct 2016
    Location
    Colchester, Essex, UK
    Posts
    55
    In the Flextimer section of the manual, look up "Complementary mode".

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,454
    Try this:

    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()
    {
    }
    The signals will be on pins 2 and 7.

  6. #6
    Junior Member
    Join Date
    Feb 2017
    Posts
    8
    yes thanks it works well!!!! i was just studying the manual, its really complicated.
    my last newbie question, why do the timers work only with 60MHz?

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,454
    The FTM timers run from the "bus clock", which is limited to 60 MHz. See chapter 6 for details.

  8. #8
    Junior Member
    Join Date
    Feb 2017
    Posts
    8
    thank you very much for your help.

    is there an elegant way to trigger a "single shot"-signal as soon as the PWM-signal repeated x-times?

    my first idea was to create an ISR that counts the modulo-compare matches (PWM-periods) and after a specific count of repetitions i would output a short pulse,
    but maybe there are better ways? Can someone give me an advise where to look in the manual?
    Last edited by manuwestern; 02-15-2017 at 01:44 PM. Reason: spelling mistake

  9. #9
    Junior Member
    Join Date
    Feb 2017
    Posts
    8
    no ideas :-(???

  10. #10
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,454
    Well, have you tried the obvious way of just writing zeros after a fixed amount of time? Did that not work? If so, what happened that wasn't good?

    Look, I wrote some code for you last night. But that's not the norm. I can't do every step for you, and I can't quick answer every question. You're going to have to try to take it from here and solve some of these things.

  11. #11
    Junior Member
    Join Date
    Feb 2017
    Posts
    8
    i understand you and thanks for your help... I want to emulate an shaft encoder, it should output a small pulse everytime when the encoder turns 360 degrees (equate to e.g. 1000 pulses). It should be possible to variate the angular-speed of the shaft-encoder during the simulation. But now i found out by myself how to count my PWM-cycles with an additional timer, without using interrupts. ... when i finish my little project i will post it in the forum :-)

    regards
    manuel
    Last edited by manuwestern; 02-15-2017 at 06:33 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •