TechplexEngineer
3/27/2014 - 12:35 PM

Simple code to blink LEDs on an AVR atMega88A

Simple code to blink LEDs on an AVR atMega88A

#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 1000000UL //processor frequency 1MHz
#include <util/delay.h>

#include <stdbool.h>

#define output_low(port,pin) port &= ~(1<<pin)
#define output_high(port,pin) port |= (1<<pin)
#define set_input(portdir,pin) portdir &= ~(1<<pin)
#define set_output(portdir,pin) portdir |= (1<<pin)

int main(void)
{
	set_output(DDRB, PB0);
	set_output(DDRB, PB1);
	set_output(DDRB, PB2);
	
	output_high(PORTB, PB0);
	output_high(PORTB, PB1);
	output_high(PORTB, PB2);
	
	while(1);
	return 0;
}
CC=avr-gcc
CFLAGS=-g -Os -Wall -mcall-prologues -mmcu=atmega88
OBJ2HEX=avr-objcopy
#UISP=/usr/local/bin/uisp
TARGET=main
#ADFLAGS=-p m8 -c FT232 -P /dev/tty.usbserial-A8003POq 
ADFLAGS=-p m88 -c avrispmkII -P usb 

# .PHONY means that these are not a target
.PHONY: fuses prog erase 


prog: $(TARGET).hex $(TARGET).eeprom  #this programs the chip, depends on Target.hex and Target.eeprom
	avrdude $(ADFLAGS) -V -U flash:w:$(TARGET).hex:i

# make .obj (any obj) if corresponding .o is newer
%.obj: %.o   
	 $(CC) $(CFLAGS) $< -o $@  # $@ means target of rule, $< means prerequisite of rule

# similarly, make any .hex if corresponding .obj is newer
%.hex: %.obj  
	$(OBJ2HEX) -R .eeprom -O ihex $< $@  # remove  eeprom output ihex translate .obj into hex file

# any eeprom depends on same .obj
%.eeprom: %.obj  
	$(OBJ2HEX) -j .eeprop -O ihex $< $@ 

# this target will just erase avr
erase:   
	avrdude $(ADFLAGS) -E noreset -e

# this target will remove all non-source files (will cause a complete re-build on next make)
clean:  
	rm -f *.hex *.obj *.o *.eeprom

# program the fuses (NOT done by regular make)
fuses:
	avrdude $(ADFLAGS) -U lfuse:w:0x62:m -U hfuse:w:0xdf:m -U efuse:w:0xf9:m 
	#www.engbedded.com/fusecalc

# $@ -- is the target
# $^ -- is a list of all the prerequisites of the rule
# $< -- is the first prerequisite