Showing posts with label AVR LED blink. Show all posts
Showing posts with label AVR LED blink. Show all posts

Thursday, February 28, 2013

My first AVR LED Blink program in C

Hi,

I recieved my AVR programmer device last week so, i wrote my first LED blinking program in C (AVR). I used ATMEGA328P-PU chip. Earlier i have assembled my own arduino board so i used the same board for AVR by erasing the bootloader. below is my code

 
#include <avr/io.h>
#include <util/delay.h>

/*
  @author Ujjwal Soni
  Connect a LED between pin 13 and GND.
 */


void delay()
{
  unsigned char counter = 0;
  while (counter != 50)
  {
    /* wait (30000 x 4) cycles = wait 120000 cycles */
    _delay_loop_2(30000);
    counter++;
  }
}


int main(void)
{
  /* Initialization, set PB5 (arduino digital pin 13) as output */
  DDRB |= (1<<PB5);

  while (1) 
  {
    PORTB |= (1<<PB5);  //arduino digital pin 5 -> 5V
    delay();
    PORTB &= ~(1<<PB5);  //arduino digital pin 5 -> GND   
    delay();
  }
  return 0;
} 

Please note that my LED is connected on PIN13.  
I will be also writing pushbutton code which i will be posting here soon.

Cheers,

Ujjwal Soni