Вы находитесь на странице: 1из 4

Atmel AVR development on GNU/Linux

Roman Pavelka, December 5, 2010

1 Introduction

A microcontroller (µC) is a complete computer on one chip. It typically consists of a processor


core, separated memories for program and data and some input and output peripherals.
8-bit Atmel AVR architecture is often used by hobbyists for its simplicity, low price and
rich peripheral set. ATMEGA8 µC can be clocked up to 16 MHz, there is 8 KB ash program
memory, 1 KB RAM, 6-channel ADC (measurement device), USART and SPI (communication
devices), timers and others...
It is available in hobbyist-friendly DIP28 package for about 5 USD in small quantities.

2 Required software

AVR binutils consists of essential tools as GAS assembler, linker and other utilities needed to
obtain HEX le suitable to load to AVR µC.
The great advantage is we don't have to develop our software in assembly language, but
The C Programming Language can be used. Simply install AVR-GCC compiler and AVR Libc
to allow C programming.
Use your package manager for installation, additional informations can be easily found.
Gentoo user will refer to detailed Gentoo Cross Development Guide.
The last required software tool is the downloader. It gets your program into the micropro-
cessor. I'm satised with avrdude (AVR Downloader and UploaDEr).

3 Programmer hardware

Physical program transfer must be done by some hardware programmer. There is a lot of com-
mercial programmers: ocial STK500, USBtinyISP by Adafruit Industries and lot of others...
It is easy to build your own. Buered version should be used, but I found and build
extremely simple STK200-compatible programmer for parallel port from gure 1 and I didn't
noticed any problems (I sadly can't nd author.). Keep the length of cable shorter than 1
meter. Of course, ISP header pinout should only t to your circuit. Parallel port driver should
be installed with properly adjusted permissions to /dev/parport0. In troubles or for more
details refer to manual page of avrdude.

4 Hello, world!

The blinking led is the analogy to the clasicall "Hello, world!" program in the world of µCs.
The wiring is on gure 2.
There is properly decoupled µC, power supply, switch between programing/reset and oper-
ating state, standard ISP programming connector and LED diode as output device on pin 0 of the port C.
Figure 1: STK200-compatible programmmer

Figure 2: Blinking LED project


Capacitors should be placed as near to decoupled devices as possible
I can recommend to use a solderless breadboard for prototyping and testing purposes.
The code of blinking_led.c is here:

1 #define F_CPU 1000000


2 #include <avr/io.h>
3 #include <util/delay.h>
4
5 int main (void)
6 {
7 DDRC=0b00000001; // set the PORTC, pin 0 as output pin, rest as input
8
9 while (1) { // forever repeat
10 PORTC=0b00000000; // switch the LED off,
11 _delay_ms(1000); // wait a second,
12 PORTC=0b00000001; // switch the LED on
13 _delay_ms(1000); // and wait a second again.
14 }
15
16 return 0;
17 }

Here is listing of Makele. Adjust rst three lines for your project name and target µC. The
last line is command to download the project to ash. m8 refers to µC ATMEGA8, stk200
to programmer hardware and should be adjusted too. List of possible programmers can be
obtained by

avrdude -c list

and list of supported target µC's by

avrdude -c stk200 -p list

Makele is here:

1 PRG = blinking_led
2 OBJ = blinking_led.o
3 MCU_TARGET = atmega8
4 OPTIMIZE = -O1
5
6 # You should not have to change anything below here.
7
8 CC = avr-gcc
9
10 # Override is only needed by avr-lib build system.
11
12 override CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS)
13
14 OBJCOPY = avr-objcopy
15 OBJDUMP = avr-objdump
16
17 all: hex
18
19 $(PRG).elf: $(OBJ)
20 $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
21
22 clean:
23 rm -rf *.o $(PRG).elf $(PRG).hex
24
25 hex: $(PRG).hex
26
27 %.hex: %.elf
28 $(OBJCOPY) -j .text -j .data -O ihex $< $@
29
30 install: load
31
32 load: $(PRG).hex
33 avrdude -p m8 -c stk200 -U flash:w:$<

By simply typing

make

the code should be compiled. Now set your circuit to programming mode by switching
reset to ISP header and by

make install

download your program to the device. When the reset is switched to the power line, the
LED has to blink with 2 second period.
There is a lot of inspiration on the net. The most required technical informations can be
found in datasheets of used devices.

Вам также может понравиться