Need a help in changing PWM duty cycle (ARDUINO MEGA 2560)

Hi all,

Please refer the below code. i dont know how to get 50% duty cycle using the below code.

By changing the values of OCR1A & OCR1B.

EXAMPLE:

OCR1A = 21

OCR1B = 19 , i get 120nsec dead time between two PWM signals and but the duty cycle is not 50% (Pls refer the attached snap for Ton and Toff)

by changing the values of OCR1A & OCR1B i can get the difference in Dead time but i dont know how to make it as 50% duty cycle.

pls guide me what to be done to get 50% Duty cycle

Thanks in Advance and looking for your valuable reply.

Regards,

Veeramanikandan


//Timer1 Mode 10 PWM to ICR1

//Dual pin 200KHz PWM generator

//47.5% duty cycle 125ns dead band between pulses

void setup() {

pinMode(11, OUTPUT); //output A (this was 9 changed it for mega)

pinMode(12, OUTPUT); //output B (this was 10 changed it for mega)

TCCR1A = 0; //clear timer registers

TCCR1B = 0;

TCNT1 = 0;

//ICR1 and Prescaler sets frequency

//no prescaler .0625 us per count @ 16Mh

//80 counts x .0625 = 5 us = 200Khz

//for 50KHz it is 20uS and 320 counts

TCCR1B |= _BV(CS10); //no prescaler

ICR1 = 160;// 40 PWM mode counts up and back down for 80 counts

// need to changed to 320 counts / 2 = 160 counts for 50KHz

OCR1A = 21;

//output A set rising/clear falling

//Rise at TCNT 21 upslope, High 38 counts, Fall at TCNT 21 downslope

//47,5% Duty Cycle Pulse centered on TCNT 40. High 38 Low 42

TCCR1A |= _BV(COM1A1) | _BV(COM1A0); //output A set rising/clear falling

OCR1B = 19; //Pin 10 match UNO, for mega its 12

//output B clear rising/set falling

//Fall at TCNT 19 upslope, Low 42, Rise at TCNT 19 downslope

//47.5% Duty Cycle Pulse centered on TCNT 0. High 38 Low 42

TCCR1A |= _BV(COM1B1); //output B clear rising/set falling

TCCR1B |= _BV(WGM13); //PWM mode with ICR1 Mode 10

TCCR1A |= _BV(WGM11); //WGM13:WGM10 set 1010

}

void loop() {}


Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

I think to get 50% duty cycle you would set OCR1x to 50% of TOP (ICR1 in Mode 10). Since you have ICR1 set to 160 you would get 50% duty cycle by setting OCR1A (Pin 11) or OCR1B (Pin 12) to 80.

Your comment says "Dual 200 kHz PWM" but your setting of TOP gets you 50 kHz:
16 MHz / 2*160 = 50 kHz

To get 200 kHz you would use a TOP value of 40. Then to get 50% duty cycle you would need an OCR1x value of 20 instead of 80.

by changing the values of OCR1A & OCR1B i can get the difference in Dead time but i dont know how to make it as 50% duty cycle.

pls guide me what to be done to get 50% Duty cycle

You can not get a dead band and two pulses at 50%. @johnwasser has told you how to get 50%, but the dead band will go away.

johnwasser:
I think to get 50% duty cycle you would set OCR1x to 50% of TOP (ICR1 in Mode 10). Since you have ICR1 set to 160 you would get 50% duty cycle by setting OCR1A (Pin 11) or OCR1B (Pin 12) to 80.

Your comment says "Dual 200 kHz PWM" but your setting of TOP gets you 50 kHz:
16 MHz / 2*160 = 50 kHz

To get 200 kHz you would use a TOP value of 40. Then to get 50% duty cycle you would need an OCR1x value of 20 instead of 80.

Thank you Sir .... After changing OCR1A and OCR1B to 80 , i get 50% duty cycle with frequency of 50Khz.

But there is no dead time in between two PWM signals . Is it possible to get a dead time?

Please think this through.
If each pulse takes up exactly half of the period, where is there room for a dead band?
You can not divide 160 ticks into two 80 tick pulses and make a gap between them?

TomGeorge:
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

Hi Tom,

I am new to Forum ... and just now referred your link how to post the code...

Thank you ....

cattledog:
Please think this through.
If each pulse takes up exactly half of the period, where is there room for a dead band?
You can not divide 160 ticks into two 80 tick pulses and make a gap between them?

Thank you Sir....But After changing OCR1A = 80 and OCR1B = 78 , i get 50% duty cycle with frequency of 50Khz for both the PWM signals.

And there is dead time of 120nSec in between two PWM signals.

veeramanikandan:
After changing OCR1A = 80 and OCR1B = 78 , i get 50% duty cycle with frequency of 50Khz for both the PWM signals.

And there is dead time of 120nSec in between two PWM signals.

Oh... So what you wanted was 50% Duty Cycle on one signal an 48.75% duty cycle on the other.

johnwasser:
Oh... So what you wanted was 50% Duty Cycle on one signal an 48.75% duty cycle on the other.

Yes sir..... and its my mistake said two signals are 50% duty cycle

cattledog:
Please think this through.
If each pulse takes up exactly half of the period, where is there room for a dead band?
You can not divide 160 ticks into two 80 tick pulses and make a gap between them?

After reading the reply from Cattledog Sir, i understood that same frequency pulses do not have dead time...

Johnwasser Sir,

is it possible to shift/delay one signal for abt 100nsec rather than reducing the duty cycle to 48.75%

so that i would get a dead time ???

veeramanikandan:
is it possible to shift/delay one signal for abt 100nsec rather than reducing the duty cycle to 48.75%

I don't think you can adjust the phase of two PWM signals on the same timer. You will need two timers that have the same period but which have an initial count (TCNTn) 1 or 2 clock cycles apart.

The 16 MHz clock produces intervals of 62.5 nanoseconds so you can get 62.5 or 125 nS but not 100.

You will need to use GTCCR for synchronizing multiple timers so they can be started at different initial counts.

johnwasser:
I don't think you can adjust the phase of two PWM signals on the same timer. You will need two timers that have the same period but which have an initial count (TCNTn) 1 or 2 clock cycles apart.

The 16 MHz clock produces intervals of 62.5 nanoseconds so you can get 62.5 or 125 nS but not 100.

You will need to use GTCCR for synchronizing multiple timers so they can be started at different initial counts.

johnwasser:
I don't think you can adjust the phase of two PWM signals on the same timer. You will need two timers that have the same period but which have an initial count (TCNTn) 1 or 2 clock cycles apart.

The 16 MHz clock produces intervals of 62.5 nanoseconds so you can get 62.5 or 125 nS but not 100.

You will need to use GTCCR for synchronizing multiple timers so they can be started at different initial counts.

Sir.... Thanks a lot for guiding me and i would try for 125nSec dead time.