Note: The DAPA programming circuit used here needs a parallel port. If you donot have an parallel port you cannot program the microcontroller using DAPA (Direct AVR Parallel Access) programmer. In that case you need to go for an USB programmer.

Introduction

Programming a 8-bit microcontroller like ATMEL's (AVR) ATMega8 is the firt step to your embedded system world in GNU/Linux. Now there are a wide variety of free software tools available for programming a microcontroller in GNU/Linux. Microcontroller programming is a fun. I will be dealing with ATMega8, the 8-bit microcontroller from ATMEL. The programming method is same for other ATMEL microcontroller's with only a small change in its pins and registers. So you can use this to program other ATMEL microcontrollers also.

STEP 1

In order to load the program to the ATMega8 microcontroller, you need a programming circuit and a set of software tools. The programming circuit is shown below. Build this circuit. This is the DAPA (Direct AVR Parallel Access) programmer circuit. If you don't have an parallel port go and get an USB programmer.

Download the schematic diagram in PDF from here .

STEP 2

Now we will write a small C program to glow an LED connected to the 28th pin of ATMega8.

#include<avr/io.h>
main()
{
DDRC  = 0xff;
PORTC = 0xff;
}

Save this program as glow.c in your home directory.

The program first sets the Port C as output by loading '0xff' to Data Direction Register (DDR). Then it loads '0xff' to Port C. This will produce a high output at all pins of Port C. for more details, refer the data sheet.

STEP 3

Next step is to compile the code. We can compile it using avr-gcc.

avr-gcc -mmcu=atmega8 -Os glow.c

This command will cross-compile the code for ATMega8. avr-gcc is our cross-compiler. The parameter 'mmcu=atmega8' is used to tell the cross-compiler for which microcontroller we are compiling. glow.c is our program file.

STEP 4

Next step is to create a hex file. The compiled output from above step cannot be understood by the microcontroller. So we need to convert to hex file.

avr-objcopy -j .text -j .data -O ihex a.out a.hex

STEP 5

The hex file can be now burnt to the microcontroller using the DAPA programmer circuit. You can use uisp to load the hex file to the microcontroller. If you donot have an parallel port, the steps from here will change. Go search some tutorials from the internet to load program the ATMega8 using USB. Even if you are using the USB port, UISP can be used. Only the command line parameters will change.

Connect the programmer circuit to the parallel port. Be sure the wires are properly connected to the ATMega8. Now run the following command to check whether the programmer has been detected or not.

uisp –dprog=dapa –dlpt=0x378

Running this command will get you this output.

 [root@localhost ~]# uisp -dprog=dapa -dlpt=0x378 
 Atmel AVR ATmega8 is found.

You will get an error message if the connection is wrong. Rewire the circuit.

 [root@localhost ~]# uisp -dprog=dapa -dlpt=0x378
 An error has occurred during the AVR initialization.
 * Target status:
 Vendor Code = 0xff, Part Family = 0xff, Part Number = 0xff
 Check if the programmer is properly connected.
 The wiring may be incorrect or target might be 'damaged'.

Once detected by your PC, we are ready to load the hex file.

Erase the microcontroller. You need to do this step if it is a microcontroller that has a program loaded in it. If it is a new microcontroller you can skip this step. use this command to erase the ATMega8.

uisp -dprog=dapa -dpart=atmega8 -dlpt=0x378 –erase

This is the output from uisp

 [root@localhost ~]# uisp -dprog=dapa -dpart=atmega8 -dlpt=0x378 --erase
 Atmel AVR ATmega8 is found.
 Erasing device ...
 Reinitializing device
 Atmel AVR ATmega8 is found.

Now we can load the program into ATMega8. Use the following command

uisp -dprog=dapa -dpart=atmega8 -dlpt=0x378 –upload if=a.hex

You can also verify the programmed data by adding '–verify' parameter. In a single command you can do a erase, upload and verify. Use the command shown below.

uisp -dprog=dapa -dpart=atmega8 -dlpt=0x378 –erase –upload if=a.hex –verify

This the output from uisp.

 [root@localhost ~]# uisp -dprog=dapa -dpart=atmega8 -dlpt=0x378 --erase --upload if=a.hex --verify
 Atmel AVR ATmega8 is found.
 Erasing device ...
 Reinitializing device
 Atmel AVR ATmega8 is found.
 Uploading: flash
 Verifying: flash

STEP 6

The programming side is over. Now you will see the LED connected to the 28th pin of ATMega8 glowing.

That's all folks. Congrats you have successfully programmed an AVR ATMega8 microcontroller.

More Info...

The tutorial given here are just about how to compile and load the program to ATMega8 microcontroller using GNU/Linux. You can get more C programming tutorials for threATMega8 form the internet. These are some of the links.

  1. To know what avr-gcc, avr-objcopy does, refer this link
  2. To know more about ATMEL programming visit AVR Freaks Forum

More links

 
program_atmega_micro_controllers_in_gnu_linux.txt · Last modified: 2010/02/17 22:09 by jeffrey
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki