Right edge-aligned pwm

I want to generate 2 PWM signals at 100 kHz as shown in this image. However, I want to align the falling edges of the PWM signal with a low duty cycle with those of the PWM signals with a high duty cycle.

This code allows me to generate a 100 kHz PWM signal with a 65% duty cycle. How can I generate the other PWM signal?

#include <avr/io.h>

void setup() {
  // Set Timer/Counter 1 for Fast PWM mode, non-inverted output
  TCCR1A = (1 << WGM11);
  TCCR1B = (1 << WGM13) | (1 << WGM12);
  TCCR1A |= (1 << COM1A1); // Non-inverted PWM output on OC1A (Pin 9 on Arduino Uno)

  // Set prescaler to 1 (no prescaling)
  TCCR1B |= (1 << CS10);
  
  // Set the initial values for PWM frequency and duty cycle
  ICR1 = (F_CPU / ( 100000)) - 1; // Calculate ICR1 for 100 kHz frequency
  OCR1A = (ICR1 + 1) * 0.65 - 1; // Calculate OCR1A for 65% duty cycle

  // Set OC1A (Pin 9 on Arduino Uno) as output
  DDRB |= (1 << PB1);
  
}

void loop() {
  
}

Use Fast PWM timer mode and COMxA1=COMxA0=COMxB1=COMxB0=1

2 Likes

Use inverted output and the OCR Values so that the time between compare match and reaching ICR1 is your pulse length.
Than you will get the trailing edge for all pulses at the overflow of the timer. The leading edge varies according to your settings.

1 Like

for suggestion thanks a lot

for suggestion thanks a lot:)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.