Atmega 16 – Timer0 with interrupt

atmegaavravr-gccledmicrocontroller

In this code the one LED is glowing at one pin PC0 with flash every 50 ms. It has XTAL of 16 MHz. With CPU frequency 16 MHz, even a maximum delay of 16.384 ms can be achieved using a 1024 prescaler. Every 256 prescalar value, it is achieving interrupt.

  1. DDRC |= (1 << 0)…. In the given code, what does it mean.
  2. I want to glow all LED at PORTC. For this, what i have to change in
    DDRC |= (1 << 0).

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

volatile uint8_t tot_overflow;

void timer0_init()
{
    TCCR0 |= (1 << CS02);
    TCNT0 = 0;
    TIMSK |= (1 << TOIE0);
    sei();
    tot_overflow = 0;
}

ISR(TIMER0_OVF_vect)
{
    tot_overflow++;
}

int main(void)
{
    DDRC |= (1 << 0);
     timer0_init();
    while(1)
    {
        if (tot_overflow >= 12)  
        {
            if (TCNT0 >= 53)
            {
                PORTC ^= (1 << 0);    
                TCNT0 = 0;            
                tot_overflow = 0;     
            }
        }
    }
}

Best Answer

  1. DDRC |= (1 << 0) - It means that you're setting pin PC0 as output
  2. You need to set PC0-PC7 pins as outputs to make all LEDS glow by doing something like this DDRC = 0b11111111

Read Bitwise operations!

Read a datasheet!