Tuesday, July 06, 2010

Compilation Of code

Compiler / IDE (Integrated Development Enviornment)

An IDE has following functions:

  • Preprocessing
  • Compilation
  • Assembly
  • Linking
  • Object Translation
  • Text Editor

If we just use compiler and linker independently we still need to get a text editor. So combining everything will actually mess things up. So the best way is to get a Software which has it all. Thats called an Integrated Development Enviroment, in short IDE.

I consider Code-Vision-AVR to be the best IDE for getting started with AVR programming on Windows XP, Vista. It has a very good Code Wizard which generate codes automatically ! You need not mess with the assembly words. So in all my tutorials I will be using CVAVR. You can download evaluation version for free which has code size limitation but good enough for our purpose.

For all my examples I will be using Atmega-16 as default microcontroller because it very easily available and is powerful enough with sufficent number of pins and peripherals we use. You can have a look on the datasheet of Atmega-16 in the datasheet section.

Lets take a look on the software. The main window looks like following (click to enlarge the pic)

cvavr avr ide

cvavr avr ide

Now click on File ---> New --->Project

A pop up window will come asking whether you want to use Code Wizard AVR, obviously select yes because that is the reason we are using CVAVR !

Now have a look on this Wizard. It has many tabs where we can configure PORTS, TIMERS, LCD, ADC etc. I am explaining some of them

CHIP:

Select the chip for which you are going to write the program. Then select the frequnecy at which Chip is running. By default all chips are set on Internal Oscialltor of 1 Mhz so select 1 MHz if that is the case. If you want to change the running clock frequency of the chip then you have to change its fuse bits (I will talk more about this in fuse bits section).

cvavr avr ide

PORT:

PORT is usually a collection of 8 pins.

From this tab you can select which pin you want to configure as output and which as input. It basically writes the DDR and PORT register through this setting. Registers are basically RAM locationswhich configures various peripherals of microcontroller and by changing value of these registers we can change the function it isperforming. I will talk more about registers later. All the details are provided inthe datasheet.

So you can configure any pin as output or input by clicking the box.

For Atmega-16 which has 4 Ports we can see 4 tabs each corresponding to one Port. You can also set initial value of the Pins you want to assign. or if you are using a pin as input then whether you want to make it as pullup or tristated, again I will talk in details about these functions later.

Similarly using this code wizard you can very easily configure all the peripherals on the Atmega.

Now for generating code just go to File ---->Generate, Save and Exit (of the code wizard obviously !)

Now it will ask you name and location for saving three files. Two being project files and one being the .C file which is your program. try to keep same names of all three files to avoid confusion. By default these files are generated in C:\CVAVR\bin

The generated program will open in the text editor. Have a look it has some declarations like PORT, DDR, TCCR0 and many more. These are all registers which configures various functions of Atmega and by changing these value we make different functions. All the details about the registers are commented just below them. Now go down down and find following infinite while loop there. We can start writing our part of program just before the while loop. And as for most of the applications we want microcontroller to perform the same task forever we put our part of code in the infinite while loop provided by the code wizard !

while (1)
{
// Place your code here

};
}

See how friendly this code wizard is, all the work (configuring registers) automatically done and we dont even need to understand and go to the details about registers too !

Now we want to generate the hex file, so first compile the program. Either press F9 or go to Project--->Compile.

It will show compilation errors if any. If program is error free we can proceed to making of hex file. So either press Shift+F9 or go to Project---->Make. A pop up window will come with information about code size and flash usage etc.

So the machine file is ready now ! It is in the same folder where we saved those 3 files. Now we want to make a programmer and feed our code to the microcontroller (Atmega-16)

No comments:

Post a Comment