Setting Timers to Toggle PWM Pins on Arduino Uno, Can A Different Initial Value Be Used?

I am trying to generate two 100KHz square waves to drive a full-bridge inverter using an Arduino Uno, with the following code:

TCCR1A = bit(COM1A0)  // toggle OC1A on Compare Match
           | bit (COM1B0);  // toggle OC1A on Compare Match
    TCCR1B = bit(WGM12) 
           | bit(CS11);   // CTC, /8 prescaling
    OCR1A =  9;       // (9 + 1) * 8 CPU cycles  -> 16 MHz / 80 = 100 KHz
}

This works well, and both OC1A and OC1B (9 and 10) display a 100KHz square waveform on my oscilloscope. However, I need to invert one of the signals. Is there any way to set the initial value of one of the pins to be inverted? The datasheet is making me think that 0 is always the starting value, based on this table:

Is my only option to use an external logic inverter?

For 180° phase difference and variable duty cycle or non-overlapping you have to use Phase (and Frequency) Correct Mode.

This should get you two opposed 100KHz 50% duty-cycle square waves.


/**
  * URL: https://dbuezas.github.io/arduino-web-timers/#mcu=ATMEGA328P&timer=1&timerMode=FPWM&topValue=ICR1&ICR1=15&CompareOutputModeA=set-on-match%2C+clear-at-max&OCR1A=79&CompareOutputModeB=clear-on-match%2C+set-at-max&OCR1B=79
  * Mode     : FPWM
  * Period   : 10 us
  * Frequency: 100 kHz
  * Outputs  : 
  *  - B1: 50.00%, set-on-match, clear-at-max
  *  - B2: 50.00%, clear-on-match, set-at-max
  */
void setup(){
  noInterrupts();
  TCCR1A = 
    1 << COM1A1 |
    1 << COM1A0 |
    1 << COM1B1 |
    1 << WGM11;
  TCCR1B = 
    1 << WGM13 |
    1 << WGM12 |
    1 << CS10;
  DDRB = 
    1 << DDB1 |
    1 << DDB2;
  OCR1A = 79;
  OCR1B = 79;
  ICR1 = 159;
  interrupts();
}

Wow, thank you for the great replies! Also finding out about the web timers app which looks awesome for testing. Originally I was trying to the simpler mode rather than the fast PWM mode, is the fast PWM mode the only way to achieve what I want here? If so, why is that? I'm not looking for any duty cycle control for just a simple square wave (just a constant toggle which would give me the default 50%), which was why I assumed I shouldn't even use the PWM mode.

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