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

Thread: PWM on Teensy 4.0?

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    17

    PWM on Teensy 4.0?

    I'm trying to generate PWM signals on Teensy 4.0. Is the information on this page still valid for 4.0?

  2. #2
    Junior Member
    Join Date
    Dec 2017
    Posts
    17
    For example, will FrequencyTimer2 library work on Teensy 4.0?

  3. #3
    Junior Member
    Join Date
    Dec 2017
    Posts
    17
    I guess I'm trying to figure out, if I know the frequency I want to pulse, and the duty cycle, what do I need to call in order to make that happen using analogWrite()? I want to use PWM to pulse a Stepper Motor driver.

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,930
    RE p#1 : PWM on those pins with analogWrite and associated functions of Freq and Res should work as expected.

  5. #5
    Junior Member
    Join Date
    Dec 2017
    Posts
    17
    Thanks. That page doesn't indicate the PWM clock / prescalar for T4, though. Where can I find that?

  6. #6
    Junior Member
    Join Date
    Oct 2019
    Posts
    1
    Quote Originally Posted by roach374 View Post
    Thanks. That page doesn't indicate the PWM clock / prescalar for T4, though. Where can I find that?
    It looks like the formula for PWM clock frequency (F_BUS_ACTUAL aka IPG_CLK_ROOT in the docs [Table 13-4]) as given in clockspeed.c is essentially:

    Code:
    F_BUS_ACTUAL = F_CPU_ACTUAL / min(4, floor((F_CPU_ACTUAL + 149999999) / 150000000))
    where the maximum frequency is 150 MHz (as listed for IPG_CLK_ROOT in the docs [Table 13-5])

    @PaulStoffregen, is there any reason that the formula for div_ipg in clockspeed.c doesn't try to maximize F_BUS_ACTUAL? For instance, for the following frequencies of F_CPU_ACTUAL you get these values for F_BUS_ACTUAL:

    CPU -> BUS
    600 -> 150 MHz
    500 -> 125 MHz
    400 -> 100 MHz
    300 -> 100 MHz
    200 -> 100 Mhz
    100 -> 50 MHz
    50 -> 50 MHz

    But if div_ipg were set to 2 and 1 when CPU is 300 and 100 MHz respectively, then BUS would be 150 and 100 MHz instead. Is there a reason not to do this?

    Also, since the BUS (IPG) clock is derived from the CPU (AHB) clock, and the clockspeed is dynamic, how do you ensure that you get a stable PWM frequency? I probably misunderstood something here!

  7. #7
    To add to this discussion. Is the bit resolution 16 bit for all frequencies on the Teensy 4? I have looked through the chip specs and cannot tell definitively, but it looks like it always has 16 bit counter.

Posting Permissions

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