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

Thread: Is it possible to change the PWM frequency for the analog output pins on a Teensy4?

  1. #1

    Is it possible to change the PWM frequency for the analog output pins on a Teensy4?

    I am planning to make a 3 phase motor driver on a Teensy4. I measured the PWM frequency with a scope. It was about 7kHz. Normally in motor drivers use upwards 10kHz. The Teensy4 will be in the motor cotrol loop, so fast response is desirable at every point in the circuit.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,111
    Yup, use analogWriteFrequency(pin, frequency).

  3. #3
    Thank you, I just found the answer here https://www.pjrc.com/teensy/td_pulse.html
    I am new to this forum, I can't find out how to delete this post. Maybe it is helpful to other newbies.

  4. #4
    It can go very fast I just took it up to 10MHz, I bet the resolution is bad taking it that high though.

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,111
    The timers run from F_BUS which is 150 MHz, so at 10 MHz you should get only 16 levels (counting all low and all high).

  6. #6
    So if I want the highest frequency for 8 bit resolution, I should use 150MHz/256 ?

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,111
    Yup, use 585937.5.

    analogWriteFrequency takes a float input for the frequency, so you can specify exactly the frequency you want, not limited by integer round off.

  8. #8
    How do you find detailed info about the Teeny4? There is not much online. Would I have to go to the ARM M7 data sheet?

  9. #9
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    11,806
    The processor manuals and datasheets are here : pjrc.com/teensy/datasheets.html

  10. #10
    I have a loop to ramp the pulse width from 0% to 100% at 12 bit resolution. Is it possible to avoid the divide by 16 within the loop to save processor time. I tried to increment the loop by a fixed constant of 1/16 0.625 but it appears that I can only use integers for this. I know the Teensy4 can handle this amount of division easily at this stage, but I am just trying to get into good habits for later projects.

    Code:
    int v = 0;
    
    void setup()   {                
        analogWriteFrequency(12, 36621.09375);
      pinMode(12, OUTPUT);
    }
    
    void loop()                     
    {
     {
      for (int v = 0; v <= 4095; v=v+1) {
        analogWrite(12,v/16);
        delay(1);
      }
    } 
    }

  11. #11
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,111
    There's a saying about premature optimization...

    But to answer your question, if you really want to avoid integer division, why not just edit your loop and delay to use the numbers you want?

    If you use an unsigned integer, the compiler can usually automatically replace division by powers of 2 by bit shift. Or if you really want to do that yourself, you could write "v >> 4".

  12. #12
    Thanks I would never have thought of using bit shit. I though that the output for analogWrite had to be between 0-255.

  13. #13
    Ahh, 0-255 is only for 8 bit resolution. I see it's possible to change the number of bits for this from 1-32 by using analogWriteResolution()

Posting Permissions

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