Arduino Mega 2560 PWM output

Dear all,

I am doing a project for half bridge dc-dc converter which need to firing 2 MOSFET at alternate interval at 20kHz.

Each period will be 50usec. I have configure PWM pin 11 and PWM pin 12 for High and Low MOSFET firing.

The desired required output is PWM11 will turn on (less than half period) with the correct dutycycle for High MOSFET, and PWM12 will turn on after half period for the same dutycycle.

my code is:
analogwrite(11, dcycle);
delaymicroseconds(25);
analogwrite(12, dycle);

But i cant get it working using the code

do i need to add in any <***.h> header file for the thing to work?

Thanks!

my code is:
analogwrite(11, dcycle);
delaymicroseconds(25);
analogwrite(12, dycle);

I seriously doubt that.

The analogWrite() handles the on/off duty cycle of pin 11. Starting PWM output on the other pin 25 microseconds later does NOT assure that the pins are out of phase by 25 microseconds.

Can i do it in this manner:

constraint (dcycleL, 0, 122); constraint (dcycleH, 123, 255);

analogwrite(11, dcycleL); analogwrite(12, dcycleH);

Can i do it in this manner:

You can do what you like, as long as you keep the compiler happy.

Whether that will do what you want, or not, is a different matter. That won't ensure that the pins are turned on and off on the same duty cycle but out pf phase, either. That will ensure that they are on and off for different amounts of time,

Ya, you are right. the result is not i wanted. they are on and off for a different amount of times.

Is there any reference which i can follow, so that i can do for half bridge dc-dc converter MOSFET firing?

Thanks

Anyone can help?? I need to meet the deadline for my project...

You wont get 20Khz using this and your feedback loop...

analogwrite(11, dcycle);
delaymicroseconds(25);
analogwrite(12, dycle);

If you want to do it properly and quickly get a Mosfet half bridge driver with dead time generation and create a loop for the voltage regulation. I must say an SG2525 would do what you need with no programming too, so if you have a good reason to be using an Arduino I think we should know to best advise you.

Lastly you need to user timer 1 or 2 PWM outputs and set that timer to operate at 20KHz by changing the pre-sclaler and if required an ISR to preload the timer counter to get the exact 20KHz

Hi Casemod,

The reason for using Arduino is, I have a feedback from the output voltage which need to be maintained, so the duty cycle of the mosfet shall increase whenever the output voltage drop and decrease whenever the output voltage increase.

The PWM output from the arduino will be fed into mosfet driver and to the mosfet.

My code as follow:

int pwmH = 11; // Assign pwm pin 11 to PWM-High
int pwmL = 12; // Assign pwm pin 12 to PWM-Low
int volt_sense = A1; //Assign analog input pim A1 to output voltage as voltage feedback

// Establish variable used within the program and initialize to zero
int dcycle = 5; // set pwm width as 5
int volt_feedback = 0; // Set voltage feedback as 0
int volt_feedback_ref = 900; // Set voltage feedback reference as 4.892
unsigned int time = 25;

void setup()
{
// Initialize the Inputs and Outputs Pin
pinMode(pwmH, OUTPUT);
pinMode(pwmL, OUTPUT);
pinMode(volt_sense, INPUT);
Serial.begin(9600);
}

void loop()
{
volt_feedback = analogRead(volt_sense); // Read value from volt_sense (pin A1) and save in volt_feedback
dcycle = constrain(dcycle, 5 , 120); // Limit the value of the increment of PWM from 0 to 120
Serial.println (“start loop”);
delay(1000); // Delay the start of the loop for 1 sec
Serial.println(volt_feedback); //ADC converter 0 - 4.8v, 0 - 1024, 0.1v = 21
SetPinFrequencySafe(11, 20000);
SetPinFrequencySafe(12, 20000);

if (volt_feedback == volt_feedback_ref)
{statusquo();}

else if (volt_feedback < volt_feedback_ref - 42)
{increase();}

else if (volt_feedback > volt_feedback_ref + 42)
{decrease();}
}

void statusquo()
{
dcycle = dcycle; // pwm status quo when feedback voltage is equal to reference point
analogWrite(pwmH, dcycle); // Maintain PWM for High side MOSFET
delayMicroseconds(25); // Pause for 25uSec before start pwm for Low side MOSFET
analogWrite(pwmL, dcycle); // Maintain PWM for Low side MOSFET
}

void increase()
{
dcycle = dcycle ++; // pwm increment when feedback voltage is less than reference point
analogWrite(pwmH, dcycle); // increase PWM for High side MOSFET
delayMicroseconds(25); // Pause for 25uSec before start pwm for Low side MOSFET
analogWrite(pwmL, dcycle); // Increase PWM for Low side MOSFET
}

void decrease()
{
dcycle = dcycle --; // PWM decrement when feedback voltage is more than reference point
analogWrite(pwmH, dcycle); // Decrease PWM for High side MOSFET
delayMicroseconds(25); // Pause for 25uSec before start pwm for Low side MOSFET
analogWrite(pwmL, dcycle); // Decrease PWM for Low side MOSFET
}

Please advise.

Could it be due to both pin 11 and pin 12 are from the same timer, which is why the delayMicroseconds(25) cannot work???

  dcycle = dcycle ++; // pwm increment when feedback voltage is less than reference point

http://www.gammon.com.au/forum/?id=12153#trap8

The result of that is not defined.

When an instance of undefined behavior occurs, so far as the language specification is concerned anything could happen, maybe nothing at all.

I get to saw this page:

The phase correct PWM might work for me, if i am able to flip (invert) the output of OCnA.

Can anyone who is well versed guide me? I am lost inside…

Thanks & Regards.

Yes, you can invert the output.

Timers

Scroll down to "Timer 0, mode 1 (PWM phase correct mode, top at 255)". The concept will be similar for the other timers.

Hi Nick,

Please see attached for the desired output waveform.

This is the type of waveform which i want.

The duty cycle shall be variable according to the output voltage against reference voltage. The duty cycle shall be increase or decrease accordingly.

How can I invert the output waveform for OCnA, and also achieve the 20kHz?

Thanks & Regards

0078-150x150.png