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?


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


The best way to get the exact inverted PWM signal is to generate one signal with the MCU pin and then use an external inverter chip to logically invert the signal. A chip like a 74HC04 may be in order.

  • \$\begingroup\$ I would not have asked the question if I could have accepted an external component... \$\endgroup\$ – gregoiregentil Oct 27 '14 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.