Skip to content

Instantly share code, notes, and snippets.

@dranger003
Last active January 12, 2017 14:11
Show Gist options
  • Save dranger003/838bae8aef695b0947c5 to your computer and use it in GitHub Desktop.
Save dranger003/838bae8aef695b0947c5 to your computer and use it in GitHub Desktop.
ATtiny85 Hardware PWM, pins 0, 1 & 4
// ATtiny85
// Hardware PWM, pins 0, 1 & 4
int main()
{
sei();
DDRB = _BV(PORTB0); // OC0A
DDRB |= _BV(PORTB1); // OC0B
DDRB |= _BV(PORTB4); // OC1B
// Timer 0 is fixed frequency? (appears to be 30.6kHz)
// Timer 0, Channel A
TCCR0A = _BV(COM0A1) | _BV(WGM01) | _BV(WGM00);
TCCR0B = _BV(CS00);
OCR0A = 256 * 0.75 - 1; // Duty Cycle
// Timer 0, Channel B
TCCR0A |= _BV(COM0B1);
OCR0B = 256 * 0.25 - 1; // Duty Cycle
// Timer 1
TCCR1 = _BV(CS10);
GTCCR = _BV(COM1B1) | _BV(PWM1B);
const uint8_t f = 38000;
OCR1C = 8000000.0 / f - 1; // Frequency
OCR1B = f * 0.33; // Duty Cycle
for (;;)
{
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment