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

Thread: PWM synchronization T4.1

  1. #1
    Junior Member
    Join Date
    Nov 2017
    Posts
    8

    PWM synchronization T4.1

    Hello,

    I want to control a brushless motor driver with 3 PWM signals from a Teensy 4.1. I use pins 7, 8 and 25 which are controlled by FlexPWM1 Module3. These outputs needs to be in sync with each other. The code below produces a 30kHz PWM output at 25%. Pin 7 and 8 are in sync and high in the beginning of the PWM interval, but pin 25 output is high at the end interval, so its falling edge coincides with the rising edge of pin 7 and 8. This is an unwanted behavior for my application. Does anyone know how to make the pin 25 align with pin 7 and 8?

    Code:
    void setup() {
    	analogWriteResolution(11); // 
    	pinMode(7, OUTPUT);
    	analogWriteFrequency(7, 30000);
    	pinMode(8, OUTPUT);
    	analogWriteFrequency(8, 30000);
    	pinMode(25, OUTPUT);
    	analogWriteFrequency(25, 30000);
    
    	analogWrite(7, 512);
    	analogWrite(8, 512);
    	analogWrite(25, 512);
    }
    void loop() {
    }
    Click image for larger version. 

Name:	7 and 8.jpg 
Views:	2 
Size:	82.5 KB 
ID:	27212
    Output from pin 7 (yellow) and 8 (purple) aligns.

    Click image for larger version. 

Name:	7 and 25.jpg 
Views:	4 
Size:	81.9 KB 
ID:	27213
    Output from pin 7 (yellow) and 25 (purple) does not align. Output from pin 25 is shifted to the end of the PWM interval.

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,553
    Pending knowing useful reply ...

    It seems this question was once answered ... maybe Bing or g00gle can find it?

    bing.com/search?q=site:forum.pjrc.com%20pwm+synchronization << With proper key words?

  3. #3
    Junior Member
    Join Date
    Nov 2017
    Posts
    8
    The thread is the closest I have found so far, but not exactly.

    I have read parts of the IMXRT manual related to the PWM timers. If there was a way to set the VAL0, VAL1 ... comparator registers, I think I will be able to solve my problem. I haven't made any low level programming yet, so if I can just get a starting point on how to change these registers, or find the names of them. Maybe the source code for analogWrite would be helpful, since it should load these registers at some point. Can you give me a hint where to find it?

  4. #4

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,553
    Quote Originally Posted by DonFredriko View Post
    The thread is the closest I have found so far, but not exactly.

    I have read parts of the IMXRT manual related to the PWM timers. If there was a way to set the VAL0, VAL1 ... comparator registers, I think I will be able to solve my problem. I haven't made any low level programming yet, so if I can just get a starting point on how to change these registers, or find the names of them. Maybe the source code for analogWrite would be helpful, since it should load these registers at some point. Can you give me a hint where to find it?
    All sources are installed by TeensyDuino installer in the chosen Arduino install folder.

    Going to that install folder this should be the source for the 1062 AnalogWrite: \hardware\teensy\avr\cores\teensy4\pwm.c:

    In general, or even specifically - register and device names found in the processor manuals are presented in a Header file in those source directories to present the relevant address or masking type info.

    An editor that can do a folder search can make discovery possible.

  6. #6
    Quote Originally Posted by DonFredriko View Post
    Does anyone know how to make the pin 25 align with pin 7 and 8?
    I did some work a few months ago on producing 3 complementary pairs of PWM signals, and to do that, I had to get into lower level code. For 3 synchronized complementary pairs, you need 3 pairs of A/B outputs from the same FlexPWM module. Pins 7/8 are the A/B signals of FlexPWM module 1, submodule 3, and pin 25 is the X output of the same submodule. The capabilities of the X output are a little different from the A/B outputs, and they are not clocked by VAL0-5 in same way as the A/B outputs. You can read about this in section 55.4.2.4 of the iMXRT 1060 reference manual (rev 2).

    If they are available, I suggest trying pins 4,5,6 instead, which are the A outputs of FlexPWM module 2, submodules 0,1,2. I'm not sure if you will be able to get synchronized output via the Arduino API. You may have to get into the low-level code, setting up the VAL registers for each submodule and then enabling them at the same time.

    Just FYI, if you ever need 3 complementary pairs, I don't think you can do it with T4.1 because the only FlexPWM module for which 3 sets of A/B pins are available is FlexPWM1, submodules 0,1,2, and those are used for the SD card interface. Those pins are available on the T4.0 via pads on the bottom, and also on Teensy Micromod. I ended up using the Teensy Micromod and the "ATP" board. It may be possible to reconfigure pins on the T4.1, but that's not something I know how to do.

  7. #7
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,553
    Thanks @joepasquariello was hoping somebody with a clue might see the added messages ...

  8. #8
    Junior Member
    Join Date
    Nov 2017
    Posts
    8
    Thanks @defragster for pointing out where to start. I will dig into the low level part and see what I can accomplish. I will post a solution when I find one.

    Thanks @joepasquariello for you advice on using the pin 4,5,6. In fact, using these pins seems to be a better choice. I'm using the Texas Instruments DRV8332 as brushless motor driver. It handles the control and deadtime for the upper and lower FETs of each half bridges, so I don't have to worry about generating complementary outputs.

    My problem is related to controlling a motor in either fast-decay mode (coasting the motor for the non-driving part of the PWM period) or slow-decay (braking the motor for the non-driving part of the PWM period). For the fast-decay mode, I need to set each half-bridge output to high-impedance after each respective sourcing or sinking period to avoid current recirculation through the lower FETs. For this I need 3 synchronous rising edge PWM signals.

Posting Permissions

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