Using Winavr - Writing The First Program: Tutorial By: Sohail Alam WWW - Robozaa.Co - CC
Using Winavr - Writing The First Program: Tutorial By: Sohail Alam WWW - Robozaa.Co - CC
www.robozaa.co.cc
Using WinAVR - Writing the first program
Let us assume that you have installed WinAVR-20090213 (2009 version) on your C: drive. The default install
folder is C:\WinAVR-20090313. Let us also assume that you are using Microsoft Windows (any version) as your
OS.
Let us now take a look at how to begin writing codes for our microcontroller.
The very first step should be creating a folder (by the name say AVR) at say Desktop. This is the place
where you shall save your works. Please create separate folders for each program you write for eg. make a folder
led_blink.
Next open Programmers Notepad [WinAVR] from START -> Programs -> WinAVR-20090313.
Save the new file in your above folder (Desktop/AVR/led_blink) by any desired name with .c as file
extension. As an example save as led.c
Next open MFile (Make File) from START -> Programs -> WinAVR-20090313. Be very careful in
the next few steps.
Click MakeFile (from menu) -> Enable Editing MakeFile (Last Option).
Click MakeFile (from menu) -> main file name -> give the same name as above without .c extension.
As for the above example name it as led -> Click OK
Now scroll a little above to the section F_CPU = 8000000 -> Make the desired change be editing it. If
you are using a 16MHz crystal then F_CPU should be equal to 16000000.
Click MakeFile (from menu) -> MCU type -> ATmega -> atmega16 (If you are using ATmega16).
Click MakeFile (from menu) -> Port -> USB (if you are using a usb type of programmer) else choose
the COM port where you have connected the programmer.
Click MakeFile (from menu) -> Programmer (if you are NOT using a USBasp programmer) and choose
your programmer.
Now the last step -> file -> save as -> browse to the folder where you have saved the file led.c
(Desktop/AVR/led_blink) then save it. Make sure the file name is not changed, it should be named as Makefile
Now you are all done to write your code and compile it and even burn it to your MCU with the help of
WinAVR.
Basics of MCU
Ports and Registers
Now we write our code for blinking some LEDs. The circuit diagram is given as :
void main()
{
DDRA = 0b00000001; // Port A Pin 0 made output
PORTA = 0xff; // Initially Pin 0 is high so led is off
DDRB= 0b00000001; // Port B Pin 0 made output
PORTB= 0; // Initially Pin 0 is low so led is off
while(1) // Infinite loop
{
glows
millisecond
is off
millisecond
glows
millisecond
is off
millisecond
}
}
Now we are ready to compile and burn our program to the mcu.
Follow the steps shown in the picture. Make sure your programmer (usbasp) is connected to the PC or Laptop
and also to your MCU and also make sure that the power (Vcc) of your MCU is ON before proceeding to Step 4.