Home AVRAVR Code Attiny2313 PWM flashing LED example

Attiny2313 PWM flashing LED example

A basic PWM example using an Attiny2313

Schematic

attiny2313 pwm led

attiny2313 pwm led

Code


#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;
}
}
}

Links

 

You may also like