1
\$\begingroup\$

I have managed to output 500kHz PWM at 50% duty cycle on PA2 TOCC1 of ATTiny841.

DDRA = (1 << PA2);//PA2 pin as an output
TOCPMSA0 = (1 << TOCC1S0);//TOCC1 linkage
TOCPMCOE = (1 << TOCC1OE);//Enable PWM
TCCR1A = (1 << COM1A1) | (1 << WGM11);//Fast PWM 1110
TCCR1B = (1 << CS10) | (1 << WGM12) | (1 << WGM13);//Fast PWM 1110
ICR1 = 1;//Not sure how clock is calculated but this gives me 500kHz on scope

I would like now to generate a 450kHz PWM on that same pin. 444.44kHz = 8MHz/18 would be acceptable. How can I do that?

The datasheet of ATTiny 841 is available here: http://www.atmel.com/Images/Atmel-8495-8-bit-AVR-Microcontrollers-ATtiny441-ATtiny841_Datasheet.pdf

\$\endgroup\$
2
  • \$\begingroup\$ A link to the datasheet and comments in the code what each line is supposed to do would help. \$\endgroup\$
    – jippie
    Jul 12 '15 at 6:16
  • \$\begingroup\$ Done with edits in the code and datasheet link \$\endgroup\$ Jul 12 '15 at 7:04
3
\$\begingroup\$

First, either unprogram the CKDIV8 fuse or set the clock prescaler to /1 so that you're actually running at 8MHz instead of 1MHz. Then set ICR1 to 17 in order to get the timer to count 18 clock pulses per cycle.

\$\endgroup\$
1
  • \$\begingroup\$ I was just looking for a reference in the datasheet. Paragraph 6.2.6 in the linked datasheet. "The default setting gives a 1MHz system clock" \$\endgroup\$
    – jippie
    Jul 12 '15 at 7:27

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.