I'm using a 12 MHz ATtiny10 and I would like to generate a square wave on PB0 at 40 kHz. I think that I need CTC. What should be the values of TCCR0A, TCCR0B, OCR0A and OCR0B?


1 Answer 1


Square wave at 40 kHz toggles every 12.5 us, which is exactly 150 oscillator clock periods. The code is:

TCCR0A = COM0A(0x01); //Toggle OC0A on compare match
TCCR0B = WGM0_32(0x01) | CS0(1); //clear on compare with OCR0A, use unscaled system clock
OCR0A = (12000000ul/40000u/2u)-1u; //overflow twice per 40kHz period
  • \$\begingroup\$ It's definitely working. I'm wondering if it would be possible to output on a second pin the exact same signal BUT inverted? \$\endgroup\$ Oct 20, 2014 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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