Teensy 4.0 and 16 bit resolution PWM duty cycle issue

Status
Not open for further replies.

Yochanon

Member
I am trying to get lowest duty cycle PWM output at 16 bit resolution on a Teensy 4.0, but output ceases when I specify analogWrite(pwmPin, 1).
This is not a problem if resolution of 15 bits is used.
I calculated Ideal Frequency using f = 600,000,000 / (2**BitResolution * 4), as it appears was used for the td_pulse Ideal Frequency table.

15 bit resolution works.
PHP:
analogWriteResolution(15);  
analogWriteFrequency(pwmPin, 4577.64);
analogWrite (pwmPin, 1);                               // This produces output with duty cycle 1/32768
16 bits does not.
PHP:
analogWriteResolution(16);  
analogWriteFrequency(pwmPin, 2288.818);
analogWrite (pwmPin, 1);                               // This produces no pulses
analogWrite (pwmPin, 2);                               // This produces the narrowest pulse and lowest duty cycle is also (1/32768)
I may as well use 15 bits since duty cycles less than 1/32768 (2/65536) are unattainable.
I hope it is I who is doing something wrong.
I do note that on the td_pulse Ideal Frequency table, 16 bit resolution is included only for Teensy 3 series but not for Teensy 4. Perhaps there's a reason for that.
 
Yes, there seems to be an issue with analogWrite(pin, value) function for value = 1 on Teensy 4.
See also this thread. See PaulStoffregen's explanation and advice in message #14.

Paul
 
Thanks, Paul. Quick followup.
Can I lower the CPU clock to match what seems to be indicated by td_pulse, that 16 bit PWM works for Teensy 3.6 CPU speed of 180MHz or 120MHz?
16 bit PWM at lower CPU clock.jpg
with some code like:
PHP:
extern "C" uint32_t set_arm_clock(uint32_t frequency); // required prototype

void setup() {
    set_arm_clock(180'000'000);
}
Yochanon
 
On the T_3.6 selecting the CPU clock and BUS clock control secondary clocks in some defined way.

On the T_4.x CPU speed doesn't relate to the clocks used throughout the system in the same way. That's part of what allows the T_4.x to change clock speed at runtime when that isn't possible without side effect on the T_3.6.
 
Thank you. I have a Teensy LC and will try 16 bit PWM on that.
I'll report back. Also, I've been doing Metro type non-blocked timing manually for years, but didn't know about it.
You mentioned that in one of your answers. Metro is simple, powerful, and makes the code so readable.
Thank you for that, too.
 
Paul,
Yes!!:eek: Teensy LC 16 bit PWM works perfectly!
My 2004 I2C display no longer worked on the LC, so I tracked down your advice on that successfully for 2 more problems.
1) I figured it might be the pullups again, because it seemed the I2C was not being received.
Success! You recommended different pullups for the LC (4.7k) rather then the 2.5k for the 4.0.
That worked and most of my menus displayed worked, except for those with live floating point updating numbers.
All floating points in sprintf were not producing any string changes. I tried everything till late, so I slept on it.
<Post Correction> It was another expert, KurtE in "Teensy LC - MPU6050 I2C. How to pullup +3.3V?"
With I2C, especially with the data line (SDA), ... And on a 3.3v circuit 4.7k appears to work well.

2) This afternoon I searched for Teensy LC sprintf floating point problems and found your quote
...make sure you have not selected "Smallest Code" in Tools > Optimize. That causes the linker to use the "nano" libc specs, which uses a very small printf implementation that omits floating point support to save code size.
That worked, too!

All is fine. status is Happy_Camper!

I would like to recommend to people looking for answers to strange behavior on this forum to:
1 - Search this thread, where many problems have been seen before and addressed; and
2 - Especially seek out comments from the most knowledgeable, of which you, Paul, and KurtE are arguably tops and yet still have time for us Teensy newbies!

So, thank you again, for all three successful pieces of advice.
I'm especially glad, as you may be, that I didn't have to take any of your time for these last two head-scratchers.
Best,
Yochanon (Mitch)
 
Last edited:
Status
Not open for further replies.
Back
Top