timerthree duty cycle wont go past 50%

Status
Not open for further replies.

ol boy

Member
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
 
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
 
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)
 

Attachments

  • Timer.h
    10.3 KB · Views: 67
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.
 
TimerThree does the same thing. The PWM frequency on both pins changes. That's fundamentally how the PWM hardware works.
 
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?
 
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:
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
 
Status
Not open for further replies.
Back
Top