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

Thread: T4 inconsistent pwm width on different pins

  1. #1
    Junior Member
    Join Date
    May 2017
    Posts
    3

    T4 inconsistent pwm width on different pins

    I encountered a strange issue regarding the actual output length of PWM pulses from Teensy 4.0:
    Code:
    void setup() {
    	analogWriteResolution(7);
    	pinMode(6, OUTPUT);
    	pinMode(7, OUTPUT);
    	pinMode(8, OUTPUT);
    	pinMode(9, OUTPUT);
    	pinMode(10, OUTPUT);
    	pinMode(11, OUTPUT);
    	pinMode(12, OUTPUT);
    	pinMode(13, OUTPUT);
    	analogWriteFrequency(6, 101867.58);
    	analogWriteFrequency(7, 101867.58);
    	analogWriteFrequency(8, 101867.58);
    	analogWriteFrequency(9, 101867.58);
    	analogWriteFrequency(10, 101867.58);
    	analogWriteFrequency(11, 101867.58);
    	analogWriteFrequency(12, 101867.58);
    	analogWriteFrequency(13, 101867.58);
    	// 1s/101867.58/128 = 76.69ns, *2=153.39ns, *3=230.08ns, *4=306.77ns
    	analogWrite(6, 1); //73ns
    	analogWrite(7, 2); //153.2ns
    	analogWrite(8, 3); //226.5ns
    	analogWrite(9, 4); //306ns
    	
    	analogWrite(10, 1); //73ns
    	analogWrite(11, 2); //146.5ns
    	analogWrite(12, 3); //226.5ns
    	analogWrite(13, 4); //299ns
    }
    
    void loop()                     
    {
    }
    PWM pins 1,2,3,4,5,6,7,8,9,22,23 behave slightly differently than pins 10,11,12,13,14,15,18,19 at a frequency = 101867.58 and higher

    at 101867.57Hz the pins 10-15,18,19 start to behave the way 1-9,22,23 do

    anyway the odd values are still slightly off of what I would expect (73ns instead of 76-77ns, 226.5ns instead of 230ns)

    what's going on? why do the two groups of pins behave differently? what's special about 101867.58Hz? And what's the highest frequency I should use if I want consistent pulse widths?

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,110
    Quote Originally Posted by charonme View Post
    what's going on? why do the two groups of pins behave differently?
    There are 2 different types of timers inside the chip which generate PWM. Some pins are controlled by FlexPWM. Other are QuadTimer.

Posting Permissions

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