Inverted PWM with ATTiny10

atmelattinypwm

On a 10MHz ATTiny10, the following code generates the same 40kHz 50% duty PWM on both pins 0 and 1:

DDRB = (1 << PB0) | (1 << PB1); //Output on PB0 and PB1
TCCR0A = (1 << COM0A0) | (1 << COM0B0); //Toggle OC0A and OC0B on compare match
TCCR0B = (1 << CS00) | (1 << WGM02); //Clear on compare, use unscaled clock
OCR0A = (1000000L /40000 / 2) - 1; //Overflow twice per 40kHz period
OCR0B = (1000000L /40000 / 2) - 1; //Overflow twice per 40kHz period

I want pin 1 to be exactly inverted: both pins should be PWM 40kHz 50% duty, but inverted of each other. How can I achieve that?

Best Answer

Use wavegen mode 14, set COM0Ax to 0b10 and COM0Bx to 0b11, and adjust ICR0, OCR0A, and OCR0B to give 40kHz 50% DC.