ATtiny13 – blinky with timer COMPA

The previous blinky project is very similar to what is presented here, with one exception. The delay function has been replaced by internal timer.  In our circuit a LED is connected to PB0 and it is made to blink for roughly every second by using timer COMPA. The code is on Github, click here.

List of elements:

Circuit Diagram

blinky-one-led

Firmware

This code is written in C and can be compiled using the avr-gcc. More details on how compile this project is here.

#include <avr/io.h>
#include <avr/interrupt.h>

#define LED_PIN PB0

ISR(TIM0_COMPA_vect)
{

        PORTB ^= _BV(LED_PIN); // toggle LED pin
}

int
main(void)
{

        /* setup */
        DDRB = 0b00000001; // set LED pin as OUTPUT
        PORTB = 0b00000000; // set all pins to LOW
        TCCR0A |= _BV(WGM01); // set timer counter mode to CTC
        TCCR0B |= _BV(CS02)|_BV(CS00); // set prescaler to 1024 (CLK=1200000Hz/1024/256=4.57Hz, 0.22s)
        OCR0A = 255; // set Timer's counter max value
        TIMSK0 |= _BV(OCIE0A); // enable Timer CTC interrupt
        sei(); // enable global interrupts

        /* loop */
        while (1);
}

Leave a Comment