Monday, January 22, 2018

Compiling avr program in Linux


there are many ways to do to compile C/C++ program Avr, for example IDEs (Integrated Development Environment) like Eclipse, Avr-studio, Codevision Avr, or any other else out there. but for you having been familiar with linux, or you have just known about this avr. it will be better to know how to compile any C/C++ code directly using commands on a terminal. Here we use an open-source compiler i.e., GNU avr-gcc compiler.

so, just let's begin how to do it..!
now when we want to compile any code, there must be a code to compile, right :) so I take a simple case here as to make a led lighting using atmega8535 MCU (Microcontroller Unit). then make a circuit like below, here I'm using Proteus simulator.

Figure 1 atmega8535 led Circuitry

then type a program below, here I use Geany text editor, I love to use it. you can use your favourite text editor here:

#include <avr/io.h>

int main(){

   DDRA |= 0x01;        //activate A register, the first bit 
   PORTA |= 0x01;      //tassign the HIGH logic to the first bit of A Register

   while(1);            //avoid program counter to keep increasing
   return 0;
}

Now, save the program above with main.c name, open the terminal (ctrl+alt+t), align to directory/folder where we save this main.c and type these 3 scripts below, one-by-one:

avr-gcc -Wall -Os -mmcu=atmega8535 -DF_CPU=2000000UL -c main.c (enter)

avr-gcc -mmcu=atmega8535 -o main.out main.o (enter)

avr-objcopy -O ihex -R .flash main.out main.hex (enter)

after we execute the three scripts above, the main.hex file will be generated (this file is usually called by hex file), and then drop the hex file onto the MCU on our Proteus simulation by double clicking the MCU figure on the Proteus simulation, FINISH... to test just run the simulation.

Figure 2 the hex file and clock setting

For more detail, here is a bit explanation about the three scripts above.

The compilation process happens on the first line of the 3 scripts:
  • -Wall indicates compiler to show warning message when compiling, e.g., if there is unused variable declared.
  • -Os indicates compiler to optimized code meaning that, for efficient space usage (at the possible expense of code execution speed).
  • -mmcu=atmega8535 tells the compiler to use the type of MCU, in this case atmega8535.
  • -DF_CPU=2000000UL tells compiler that the clock speed of MCU is 2MHz, and UL here means the integer type used, Unsigned Long.
  • -c indicates compiler to compile and stop - do not do any linking.
This, the first line of the 3 scripts above will create us an object file, that is main.o
  • The second line of the 3 scripts above is linking process. Note that linking process needs to specify the MCU type, mmcu . Without doing it, compiler will use 8515 processor as default.
  • The last line of the 3 scripts above is the process to generate hex file of the program we have made above, by avr-objcopy.

No comments:

Post a Comment

Arduino & seven-Segment: Counter-Down / Counter-Up

To be easy to understand, a counter-up or counter-down is a term to mean 'to count by incrementing' or 'to count by decreme...