Avr LED Blinking

This program explains hello world LED blinking program.In this program an LED is connected through a resistor to PINB0.

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

int main()
{
	DDRB |= 1<<PINB0;
	while(1)
	{
		PORTB |= 1<<PINB0;
		_delay_ms(100);
		PORTB &= ~(1<<PINB0);
		_delay_ms(100);
	}
}

The above program makes PINB0 blink with 100ms delay.
Compile and upload the hex file.You can use the blow make file for the whole process

CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
AVRDUDE = avrdude
REMOVE = rm -f

MCU =atmega8

TARGET = blink
SRC = $(TARGET).c

AVRDUDE_PROGRAMMER = dapa
AVRDUDE_PORT = /dev/parport0 
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex

elf:
	$(CC) -mmcu=$(MCU) -Os -o $(TARGET).elf $(SRC)
hex:
	$(OBJCOPY) -j .text -j .data -O ihex  $(TARGET).elf $(TARGET).hex
flash:
	$(AVRDUDE) -p m8 -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) $(AVRDUDE_WRITE_FLASH)
clean:
	$(REMOVE) $(TARGET).hex $(TARGET).elf $(TARGET).c~

When your program name changes change name of the TARGET variable in Makefile.
Use the commands in sequence

make elf

make hex

make flash

HelloWorld in action

Happy Hacking 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s