ATtiny13 – blinky with Watchdog Timer

Yet another ATtiny13  blinky project. In our circuit a LED is connected to PB0 and it is made to blink for roughly every second by using Watchdog Timer. The code is on Github, click here.

Parts Required

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/wdt.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>

#define LED_PIN PB0

ISR(WDT_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
        wdt_enable(WDTO_500MS); // set prescaler to 0.5s and enable Watchdog Timer
        WDTCR |= _BV(WDTIE); // enable Watchdog Timer interrupt
        sei(); // enable global interrupts

        /* loop */
        while (1);
}

2 thoughts on “ATtiny13 – blinky with Watchdog Timer

  1. Hi.I tired your example but after wdt_enable pin WDE is also set and after event handler processor is restarted (bit WDE and WDTIE are set ). Based on Attiny13a datasheet I change a little configuration part.
    cli();//disable interrupt
    wdt_reset();//reset Watchdog timer
    WDTCR |= _BV(WDCE)|_BV(WDE);// clearing sequence before setup time
    WDTCR = _BV(WDTIE)|_BV(WDP2)|_BV(WDP0); // enable Watchdog Timer interrupt and set up time
    sei(); // enable global interrupts

    I’m wondering

    • Hi there. The blinky project using Watchdog Timer might be a tricky one to a first glance . It uses a fact that registers states after Watchdog reset are not cleaned but.. you already figured it out!

Leave a Comment