Can PWM phase be controlled on ATMega32u4?

Go To Last Post
20 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


I'm building a switch-mode power supply (synchronous boost voltage regulator), and want to control the duty cycle and the phase of two PWM outputs (frequency in the 100kHz - 125kHz range). For proof-of-concept, I've been using an Arduino-compatible dev board (from Adafruit) with an ATMega32u4. I've been using Timer4, and I can control the duty cycle of the two PWM4A / PWM4B outputs independently but I haven't been able to adjust the phase relationship. I've spent some time looking through the ATmega16U4/ATmega32U4 datasheet and testing Fast PWM mode,  Frequency Correct PWM mode, as well as various settings for the DT4 Dead Time value registers. I have not attempted using PWM6 mode yet.

I don't necessarily need to use the 32u4 processor. If it's not possible to adjust the phase relationship of the two PWM outputs, I'm interested to know if there are other AVR (or non-AVR but still Arduino-compatible) controllers that might be more suitable. When I first started the project I thought I could get by with a simple boost regulator with a single PWM output, but it turns out I need a synchronous FET in parallel with the catch diode to keep the efficiency high.

 

Attached oscilloscope screenshot shows the PWM4A and PWM4B outputs, with scratch notes showing how I want to adjust the phasing.

 

 

Thanks in advance,

Scott

Attachment(s): 

Last Edited: Wed. Jun 9, 2021 - 06:27 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
// 2021-06-08, ATMega32u4 PWM output demo, for avrfreaks.net forum question
// Using Arduino 1.8.13 IDE, with board = Adafruit ItsyBitsy32u4 5V 16MHz

// global variable definitions. Global variables might be bad practice
byte lowSideDuty = 10;
byte highSideDuty = 20;
const int serialComms = 1;

void setup()
{
  pinMode(13, OUTPUT);    // Arduino pin 13, 32U4 pin PC7,  OC4A for timer 4, this is also the red LED on the dev board so it will be active when flashing Arduino code
  pinMode(10, OUTPUT);    // Arduino pin 10, 32U4 pin PB6,  OC4B for timer 4 (high-speed PWM)
  pinMode(5, OUTPUT);     // Arduino pin 5,  32U4 pin PC6, ~OC4A for timer 4, inverted from the OC4A output pin PC7

  // set PLL registers
  PLLCSR = _BV(PINDIV) | _BV(PLLE);
  // PINDIV = 1, this must be set to 1 for 16MhZ clock before enabling PLL
  // PLLE = 1, this is the PLL Enable bit.

  // changing to 96MHz PLL
  PLLFRQ = _BV(PLLTM1) | _BV(PLLUSB) | _BV(PDIV3) | _BV(PDIV1);
  // PDIV3=1, PDIV2=0, , PDIV1=1, PDIV0=1, this should set PLL output frequency to 96Mhz, per pg 42 of ATMega16U4/32U4 datasheet
  // PLLUSB = 1, (divide by 2 for USB postscaler, use this if PLL output is 96 MHz. This results 48MHz for the USB to work properly)
  // PLLTM1=1, PLLTM0=0, this should set PLL post-scaler factor 1.5, which will result in (64Mhz) high-speed timer input freq, per pg 41 of ATMega16U4/32U4 datasheet

  // set Timer4 registers
  TCCR4A = _BV(COM4B0) | _BV(COM4A0)| _BV(PWM4A) | _BV(PWM4B);   //changing this 2021-04-30, want both the PWM4B and PWM4B(inv) pins connected
  //COM4B1=0, COM4B0=1, clear on Compare Match and OC4B / OC4B (inv) pins connected)
  //COM4A1=0 and COM4A0=1, clear on compare match (want pin 4A to match pin 4B if they are set to the same duty

  TCCR4B = _BV(CS40) | _BV(DTPS41) | _BV(DTPS40);
  // CD43=0, CS42=0, CS41=1, CS40=0 is prescalar 'clk/1', results in 125Khz Fast PWM output when PLL timer is 64 MHz
  // CD43=0, CS42=0, CS41=0, CS40=1 is prescalar 'clk/1', results in 125Khz Phase Correct PWM output when PLL timer is 64 MHz
  // DTPS41=0, DTPS40=0, dead time prescale = 1x (no division)
  // DTPS41=1, DTPS40=0, dead time prescale = 4x (deadtime adjustments will be a 4x larger change than 1x)
  // DTPS41=1, DTPS40=1, dead time prescale = 8x (deadtime adjustments will be a 8x larger change than 1x)

  TCCR4D = 1;
  // WGM41=0, WGM40=0, sets mode to Fast PWM
  // WGM41=0, WGM40=1, sets mode to Phase and Frequency Correct PWM

  // DT4 = 16 | _BV(DT4L2);
  // DT4 = _BV(DT4L3) | _BV(DT4L2);
  // dead time values add dead time between the PWM4A and ~PWM4A (inverted) outputs, but don't shift the PWM4A vs PWM4B signals far enough, commented out for now.
  // DT4L3/DT4L2/DT4L1/DT4L0 is the 4-bit dead time value for the low outputs (inverted). There is also another register with deadtime prescale values

  // initialize PWM 4A and 4B outputs to low duty cycle
  OCR4B = 1;
  OCR4A = 1;  // OCR4A appears to affect Output13 (red LED) duty depends on 8- or 9- or 10-bit scale

  Serial.begin (115200);
  delay (200); // wait 200 ms for serial comms
  Serial.println (F("Initializing"));
}
// end of void setup()

void loop()
{

  OCR4B = lowSideDuty;    // set output duty, 8-bit timer 0-255 is 0-100%
  OCR4A = highSideDuty;   // want to adjust phase for this output, haven't figured it out yet

  // delayMicroseconds(8000 - loopTime);        // removing from simple example code, loop time is not critical
  delay(2);                                     // total delay combined with line above results in ~10.0 milliseconds loop time. May target faster or slower times later

  /*
  if (serialComms == 1)
  {
    Serial.print ("Low Side Duty: ");
    Serial.print (lowSideDuty);
    Serial.print (", High Side Duty: ");
    Serial.print (highSideDuty);
    Serial.println ();
  }
  */

}
// end of void loop()

Here's the code used for generating the screenshot in the first post.

Last Edited: Tue. Jun 8, 2021 - 09:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Sure PWM phase can be controlled while adjusting duty cycle.

 

For example - By configuring the timer1 in mode 8.

 

 

Here is the code for one PWM signal on pin PB5

 

PORTB |= (1 << PB5);    // Set PB5 as output

TCCR1A |= (1 << COM1A1);                // Configure PWM pin
TCCR1B |= (1 << WGM13) | (1 << CS10);   // Start timer with mode 8

OCR1A = 80;	// Adjust duty cycle
ICR1 = 160;	// Adjust frequency

 

Adjusting OCR1A will give you the duty cycle control, similarly ICR1 will give you the frequency control.

 

Configure the other timer3 like this to get your another PWM signal going.

 

 

 

 

Last Edited: Wed. Jun 9, 2021 - 03:34 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Heisen wrote:
Adjusting OCR1A will give you the duty cycle control, similarly ICR1 will give you the frequency control.
But what adjusts the phase that OP was asking about?

 

I think that for phase control you are going to need to use soft PWM aren't you?

 

(having said that there are some "motor control" AVRs that may have even more than than the usual options for things like this- however if OP needs to stick with 32U4 for other things (presumably the USB?) then I think "soft" is the solution perhaps?)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

 If it's not possible to adjust the phase relationship of the two PWM outputs,

Why do you need that?---if you have dead time control, that is what it is for.

You can also use a parallel diode/resistor pair on each gate...the diode ensures fast turnoff & the resistor ensure slightly slow turn-on....resulting in no cross-conduction.  The sync fet, can be especially be slow turnon since it is along for the ride with the parallel power diode & is acting like a "helper"

 

some fet gate driver chips will provide a sync gate drive; you only provide one pwm signal & it produces two gate drive signals (one inverted with dead-time)

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


avrcandies wrote:

 

some fet gate driver chips will provide a sync gate drive; you only provide one pwm signal & it produces two gate drive signals (one inverted with dead-time)

One example, though maybe not the best is an IR2104, looks like this:

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Thanks for the replies, all. I already have the circuit built and working reasonably well. Simplified circuit diagram below:

 

[edit, thanks @ ki0bk for the tip about copy/paste for inline images]

Attachment(s): 

Last Edited: Wed. Jun 9, 2021 - 06:23 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


 

The attached oscilloscope screenshot matches the 'red_probe' and 'blue_probe' labels in the simplified circuit diagram above.

 

 

 

I want to shift the PWM4A output so it happens earlier (to avoid the diode delivering energy soon after the inductor output goes high). Unfortunately the PWM4A output can't be a simple mirror of the PWM4B output, if the high-side FETs are active too long that can decrease the circuit output voltage rather than increase it.

 

Attachment(s): 

Last Edited: Wed. Jun 9, 2021 - 07:15 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

I think that for phase control you are going to need to use soft PWM aren't you?

 

(having said that there are some "motor control" AVRs that may have even more than than the usual options for things like this- however if OP needs to stick with 32U4 for other things (presumably the USB?) then I think "soft" is the solution perhaps?)

 

Thanks, I will look into soft PWM.

 

I would be happy to switch to an AVR intended for motor control (especially if I can find a development board that will allow me to keep using Arduino environment and USB for flashing code). I don't mind paying $50-100 USD for a different dev board if that saves me a dozen hours. The circuit is designed with an expansion connector, from the hardware side of things it would be easy to swap to any other micro that operates at 5V. Switching to a 3V micro is possible I would need to reconfigure some circuits that assumed 0-5V operation. On the software side of things I don't write code often, so it will take me some time to switch away from the Arduino environment. I use the 'Serial Monitor' (via USB) to display status and debug-type information on my PC, and I'm worried it would be tricky for me to replicate that in another environment.

 

Thanks again,

Scott

Last Edited: Wed. Jun 9, 2021 - 05:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Scott_has_questions wrote:
sorry but I haven't figured out how to embed images inline on this forum.

copy/paste works for me!

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

copy/paste works for me!

 

Thanks so much, I've edited all my previous posts to add images inline.

Last Edited: Wed. Jun 9, 2021 - 06:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
But what adjusts the phase that OP was asking about?

Hmm, I got confused between frequency and phase. 

 

So isn't this just a matter of delaying the start of PWM signal relative to other signal? If OP wants to set it just once, can this be done by starting the timer just a bit late or early?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use a p-chan for your  second FET....otherwise you will need to provide around 3 to 5 to 10V gate drive above the boosted output voltage (if you want to turn on the fet hard).  P channel will be good enough & does''t need it own "boost"

 

WHO uses a 1N4148 for a switching supply?  Use a 1-3Amp schottky,  1N4148 isn't a power diode at all.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Thu. Jun 10, 2021 - 03:29 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

Use a p-chan for your  second FET....otherwise you will need to provide around 3 to 5 to 10V gate drive above the boosted output voltage (if you want to turn on the fet hard).  P channel will be good enough & does''t need it own "boost"

 

WHO uses a 1N4148 for a switching supply?  Use a 1-3Amp schottky,  1N4148 isn't a power diode at all.

 

Good catch, some of the part numbers on that simplified schematic are not accurate. The actual part is a schottky diode rated for 90A, and even with a heat sink it's getting warmer than I prefer. I'd like the circuit to deliver over 300W, so efficiency is partly to keep the parts at reasonable temperatures. I'm also using P-channel FETs for the second FET, plus gate driver ICs to switch the FETs quickly. The P-FET and N-FET temperatures look OK plus I can add parallel FETs if needed to keep their temperature down. I've learned (during this project) that parallel diodes don't share current nicely.

 

Last Edited: Thu. Jun 10, 2021 - 05:04 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Heisen wrote:
So isn't this just a matter of delaying the start of PWM signal relative to other signal?
Yeah but he's talking about A and B channels on the same timer PWM.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

The P-chan fet on-time isn't super critical---even if it is on only 80% of the possible time, you eliminated 80% of the diode heating/loss.  Since it is a "helper"  it also doesn't need to turn on quite as fast---it's just sopping up the 0.2-0.4V across the diode.

 

What gate driver are you using??  look at the  MIC4605-2: Single PWM input      has integrated charge pump to drive two n-mos fets

features adaptive dead time and shoot-through protection. The adaptive dead-time circuitry actively monitors the half-bridge outputs to minimize the time between high-side and low-side MOSFET transitions, thus maximizing power efficiency. Shoot-through protection circuitry prevents erroneous inputs and noise from turning both MOSFETS on at the same time.

 

You will have to investigate whteher it will support your boos topology...but this gives the idea---there are many  such chips

 

https://ww1.microchip.com/downlo...

that parallel diodes don't share current nicely.

Parallel FET s (& diode & LEDS) should have series equalizing resistors   Motorola has an app note about it...usually just  a smidge of an ohm or two.  The positive FET channel tempco, means they will balance themselves as they heat up.

Two fets cut the current in half, and each fet's heat by 4x.  Three fets cut the current by 3x & the fet's heat by 9x, so it's a bargin.   

 

The actual part is a schottky diode rated for 90A ​​​​​​​--is it a stud-mount diode??  Best for heat, besides a hockey-puck style

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Thu. Jun 10, 2021 - 10:54 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks again, all. I don't have the phasing doing exactly what I envisioned, but simply increasing the PWM4A duty has helped with diode temperatures.

 

@avrcandies, I didn't expect so much hardware advice here, but I appreciate it. I've been using Infineon 1EDN8511BXUSA1 single-channel gate drivers for the PFETs and NFETs, digikey suggests the switching time and max voltage compare well against other options. I'll look into an NFET for the M2 transistor, the PFETs are getting warm at high loads.

 

I'll try adding a resistor in series with the diode, and will also look into other diode packages.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The  1EDN8511BXUSA1 isn't going to be much use for the high-side (non-grounded FET), at least not without adding other goodies.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Scott_has_questions wrote:

... When I first started the project I thought I could get by with a simple boost regulator with a single PWM output, but it turns out I need a synchronous FET in parallel with the catch diode to keep the efficiency high.

Freely varying the phase of either edge between two PWM outputs, it not usually easy to do.

 

However, you may be trying to use the MCU to solve a problem that is common, and already has a better solution.

 

Look for Synchronous Rectifier Gate drivers, those are small mosfet driver parts that sense a FET, or FET pair, operated in diode mode, and they dynamically adjust the gate drive to get as close to ideal-diode operation.

 

For best  MOSFET as DIODE SYNC operation, there is another problem you have not seen yet, but it is very important :

Besides turning ON the mosfet, a more important detail is to turn the MOSFET OFF, when the desired current has dropped to zero, otherwise current then flows in a very not-ideal-diode manner !!

 

The Synchronous Rectifier Gate drivers, include voltage sense comparators, that take care of the vital turn off of the FET. A MCU lacks the voltage sense and speed to manage that properly.

 

If your power needs are modest, you can get ICs that include the MOSFETS for an even simpler solution

Or, you may decide a decent Schottky Rectifier is 'good enough' as that is simpler. 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!