Projects‎ > ‎ATMega328P‎ > ‎

Blinky

posted Nov 2, 2010, 2:57 PM by Bill Bai   [ updated Nov 2, 2010, 3:03 PM ]
When I program, I like to get one small piece of the puzzle working and then start digging into the meaty bits.  This is just a simple program that blinks and LED on PORTD.  It's based on a 1ms Timer 0 interrupt.  Good times.

/*
Bill Bai

Blinking LED based on tutorial from Sparkfun
http://www.sparkfun.com/commerce/tutorial_info.php?tutorials_id=93
*/

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

//Define functions
//======================
void ioinit(void);      //Initializes IO
void init_Timer0(void);
//======================

int main (void)
{
    ioinit(); //Setup IO pins and defaults
init_Timer0();
sei(); //enable interrupts

    while(1){}
   
    return(0);
}

unsigned int count = 1;
ISR(TIMER0_OVF_vect)
{
if(count == 500)
{
count = 1;
PORTD ^= 0xFF;
}
count++;
TCNT0 = 0xB2; //reset timer 0 counter
}

void ioinit (void)
{
    //1 = output, 0 = input
    DDRB = 0xFF; //All outputs
    DDRC = 0xFF; //All outputs
    DDRD = 0xFF; //PORTD (RX on PD0)
}

//Initialize Timer0
void init_Timer0(void)
{
TCNT0 = 0xB2;
TCCR0A = 0;
TCCR0B = 4;
TIMSK0 = 1;
}
Comments