0% found this document useful (0 votes)
64 views5 pages

Using Winavr - Writing The First Program: Tutorial By: Sohail Alam WWW - Robozaa.Co - CC

The document provides instructions for writing a first program using WinAVR to blink LEDs connected to an AVR microcontroller. The steps include: 1. Creating a project folder on the desktop to save code files. 2. Opening the Programmer's Notepad and saving a new code file with a .c extension in the project folder. 3. Opening the MFile makefile editor and configuring options like the CPU frequency, microcontroller type, programmer, and saving the makefile in the project folder. 4. Writing code in the code file to blink two LEDs connected to separate ports of the microcontroller by toggling the port pins on and off.

Uploaded by

Shreyas Tater
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
64 views5 pages

Using Winavr - Writing The First Program: Tutorial By: Sohail Alam WWW - Robozaa.Co - CC

The document provides instructions for writing a first program using WinAVR to blink LEDs connected to an AVR microcontroller. The steps include: 1. Creating a project folder on the desktop to save code files. 2. Opening the Programmer's Notepad and saving a new code file with a .c extension in the project folder. 3. Opening the MFile makefile editor and configuring options like the CPU frequency, microcontroller type, programmer, and saving the makefile in the project folder. 4. Writing code in the code file to blink two LEDs connected to separate ports of the microcontroller by toggling the port pins on and off.

Uploaded by

Shreyas Tater
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Tutorial by: Sohail Alam

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.

Scroll to the section AVRDUDE_PROGRAMMER = stk500v2. Change stk500v2 by usbasp if you


are using a USBasp programmer. It is the most commonly used programmer. Hence it looks like
AVRDUDE_PROGRAMMER = usbasp

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.

Please refer the following if you haven't already, before proceeding.


1.
2.

Basics of MCU
Ports and Registers

Now we write our code for blinking some LEDs. The circuit diagram is given as :

Explanation of the above circuit:


There are two LEDs D1 and D2. D1 connected to PA0 (Port A Pin 0) and Vcc (+5V) through a resistor.
Similarly D2 connected to PB0 (Port B Pin 0) but grounded (0V) through a resistor.
It is important to know when will these two LEDs will glow.
Both will glow ONLY when Port A and B, Pin 0 are made OUTPUT. Also remember when a port pin is made
output two conditions may arise: pin may be made high (+5V) or low (0V) as and when required.
D1 will glow only when it receives LOW (0V) from PA0. Similarly D2 will glow only when it receives HIGH
(+5V) from PB0.
Now we write the code in Programmer's Notepad [WinAVR].
#include <avr/io.h>
#include <util/delay.h>

// This contains the definitions of the terms used


// This contains the definition of delay function

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
{

PORTA = 0b00000000; // led (D1)


_delay_ms(200); // delay of 200
PORTA = 0b00000001; // led (D1)
_delay_ms(200); // delay of 200

glows
millisecond
is off
millisecond

PORTB = 0b00000001; // led (D2)


_delay_ms(100); // delay of 200
PORTA = 0b00000000; // led (D2)
_delay_ms(100); // delay of 200

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.

Hoooollaaa.... you are done.

You might also like