Attiny2313 PWM flashing LED example

A basic PWM example using an Attiny2313

Schematic

attiny2313 pwm led

Code

[c]

#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRB   |= (1 << PB2);                   // PWM output on PB2
TCCR0A = (1 << COM0A1) | (1 << WGM00);  // phase correct PWM mode
OCR0A  = 0x10;                          // initial PWM pulse width

TCCR0B = (1 << CS01);   // clock source = CLK/8, start PWM

while(1)
{
// change PWM pulse width every 2 seconds
for(int i = 0;i<=255;i++)
{
_delay_ms(200);
OCR0A  = i;
}
}
}

[/c]

Links

 

Related posts

ATmega128 and MAX7219 8 x 8 matrix

Atmega128 and a 7 segment display

Atmega16 and MAX7219 seven segment display