Getting the ATmega8 to run our Program
We saw in our earlier posts, how to wire up the programmer and how to write a C Program. Now, let us see how to compile and download the program onto the device. gcc, as most of you may know, is a cross compiler. A cross compiler is usually used for off target compiling. Thus, a program can be compiled by gcc targeting any platform, not just the host platform. In short, eventhough gcc runs and compiles on a PC, it can be asked to compile for ATmega8. Thus, effectively, we can generate a hex file readable by the device; but not by the PC.
Save the program that we just wrote with a proper filename, say blink.c. To compile the program for the Atmega8 Device,
avr-gcc -mmcu=atmega8 -Os blink.c -o blink.out
This generates an object file called blink.out as given in the command line option. Try running this generated file in your PC. The file will generate an error. This is expected, as we compiled the file for ATmega8. Now, the machine code prepared by the compiler have complex structures, making it difficult for a burning program like avrdude to decipher them. Thus we need a way to convert the code into simpler Motorola or Intel hex format. Thus, we need to convert the file we just generated into one of these formats. Well, how do you do it ? Enter avr-objcopy. Issue the following command to convert the machine code into Intel Hex format.
avr-objcopy -j .text -j .data -O ihex blink.out blink.hex
This converts the complex machine code into readable hexadecimal numbers. One can easily open blink.hex in a editor of his choice to verify the result.
To program the microcontroller, or to upload the firmware, we make use of a nifty little utility called uisp. First, we need to erase the device.
uisp -dlpt=/dev/parport0 --erase -dprog=dapa
The above code does a handful of things. First, it tells the port it is using by the -dlpt option. Then it tells the program we need to --erase the device. And lastly, it specifies the method of programming we are using via the -dprog option. Here we are using dapa which means direct parallel access. As soon as this command is executed, the device is erased and ready to accept new firmware. To program the device, use
uisp -dlpt=/dev/parport0 --upload if=blink.hex -dprog=dapa -v=3 --hash=32
The option -v sets the verbosity level of the output. If all goes well, you will have the microcontroller programmed and executing your program.
And, if you ever want to reset your controller without pushing the reset switch on your board, use
uisp -dprog=dapa
Related
Comments
Leave a Reply