# PWM Phase Shift

``````unsigned long duty1,duty2;
// Duty Cycle in terms of a percentage.
unsigned long plus;
// Value read from A1, in case plus mode is activated
float xxx;
// Float numbers to calculate duty for PWM 1 and PWM 2
float yyy;
unsigned long pwm1;
// Value read from A0 and A2 to give PWM duty cycle output in terms // of 0-5V
unsigned long pwm2;
void setup(){
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
TCCR1A = _BV(COM1A1) | _BV(COM1B1) ; // phase and frequency correct mode. NON-inverted mode
//TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1A0) | _BV(COM1B0) ;
TCCR1B = _BV(WGM13) | _BV(CS11);

}
void loop(){

ICR1 = 50; // Default value to 1kHz for A3 = 5V

xxx = float(pwm2);

yyy = float(pwm1);
xxx = xxx * ICR1;

yyy = yyy * ICR1;
xxx = xxx / 1023;
yyy = yyy / 1023;

OCR1B = int(xxx/2);
OCR1A = int(yyy/2);
}
``````

According to the code,If I want to do a 180 degree phase shift . How can I modify the code ?

want

The COM1A1/COM1A0 bits control pin 9 - set to 10 is one sense, set to 11 is the inverse. Ditto COM1B1/COM1B0 bits control for pin 10.

Try

``````  TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1B0) ;
``````

Then one pin will be inverted mode and the other not.

Hi, will doing that just invert the PWM which would be OK for 50%. What about 10% duty, does it produce two outputs one phase shifted 180Deg and both with 10%, or two PWM signals, one 10% duty and the other 90%.

Tom.... :)