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

Thread: timerthree duty cycle wont go past 50%

  1. #1
    Junior Member
    Join Date
    Jul 2018
    Posts
    8

    timerthree duty cycle wont go past 50%

    Ive set up timer3 per the example code as a pwm out on a 3.2. I need a period of 20ms. So i did the timer3.initialize(20000); and timer3.pwm(pin, 0) to start the function. If i write any value larger than 512ish to timer3.setPwmDuty(pin, value) in the main the output goes back to low or zero. i tried 15000 us period and i could get to 66ish percent duty before the output would default to zero.

    Thanks Ryan

  2. #2
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    1,991
    Timer3 is an old library which was intended to allow an easy transition from old 8bit AVR stuff to the modern 32bit ARM processors a few years ago. For new developments, the following functions which come with Teensyduino should be used instead:
    /* configure everything once in setup() */
    analogWriteResolution(10); // sets the resolution to 10bit corresponding 0 to 1023
    analogWriteFrequency(pin, 50); // sets the period to 20ms

    /* and then, wherever needed */
    analogWrite(pin, value); // for example a value of 768 will give 75% duty cycle

    This works on the pins which are labeled as PWM on the Teensy 3.2 pin layout card which comes with it: 3, 4, 5, 6, 9, 10, 20, 21, 22, 23, 25, 32

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    18,511
    Wow, looks like Timer3.pwm() has been broken on this library for a long time!

    And yeah, you can get this much better with analogWriteFrequency.

    But here's a fix for TimerThree. (not sure why this is attaching as only Timer.h, rename it to TimerThree.h)
    Attached Files Attached Files

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    18,511
    I've committed the fix on github too.

    https://github.com/PaulStoffregen/Ti...58169346ccba99

  5. #5
    Junior Member
    Join Date
    Jul 2018
    Posts
    8
    Awesome, thanks. Ill give the new .h a try later today. I would do the freq change but i need the rest of the pwm pins to stay at the 488hz.

    This project is driving a fuel injector for a water injection pre turbo and 50hz is a good period for that. 488hz is just too fast for the mechanical parts of the injector.

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    18,511
    TimerThree does the same thing. The PWM frequency on both pins changes. That's fundamentally how the PWM hardware works.

  7. #7
    Junior Member
    Join Date
    Jul 2018
    Posts
    8
    Yep, its easier to manage 2 pwm outs at the same freq than 10 that need to be higher. So the only change is adding the resolution to 15bit? How does that change the way the registers get loaded with the zero to 1023 value for duty?

  8. #8
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    1,991
    No need to change to 15bit. Just set analogWriteFrequency to 50Hz and the resolution to 10 bits. Then, analogWrite will automatically re-scale your values from 0 to 1023 into the 0 to timerMax, where the latter is internally set when you set the Frequency.

    If you do this for pin 3 or 4, it will only affect those both pins while the FTM0 pins 5,6,9,10,20,21,22,23 might get a different common analogWriteFrequency setting.
    Last edited by Theremingenieur; 07-01-2018 at 02:18 PM.

  9. #9
    Junior Member
    Join Date
    Jul 2018
    Posts
    8
    Timer3 only does pins 25 and 32. Timer1 is 3,4. Im using CAN bus so pins 3,4 are allready used up. If i can get away with a single pwm out at 50 hz with out affecting the other pwm channels then thats what im shooting for. I could not get softpwm to work for me so i want to try timer3. Ill test the changes later today.

    Thanks Ryan

  10. #10
    Junior Member
    Join Date
    Jul 2018
    Posts
    8
    The patched timerthree.h file fixed the limited duty cycle problem. Thanks again

Posting Permissions

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