Installing and Compiling With WinAVR in Windows
Installing and Compiling With WinAVR in Windows
JUST DO IT
☰ Menu
Table of Contents:
1) installing WinAVR
2) simple example/tutorial of compiling & uploading code to atmel
chip
1) installing WinAVR
just follow the screen captures (click the images to zoom in)
https://kwagjj.wordpress.com/2014/07/21/installing-and-compiling-with-winavr-in-windowsa-simple-avr-compilinguploading-exampletutorial/ 1/13
3/12/23, 10:37 AM installing and compiling with WinAVR in Windows(+a simple avr compiling&uploading example/tutorial) – JUST DO IT
(https://kwagjj.files.wordpress.com/2014/07/1.jpg?w=300)
(https://kwagjj.files.wordpress.com/2014/07/2.jpg?w=300)
(https://kwagjj.files.wordpress.com/2014/07/3.jpg?w=300)
https://kwagjj.wordpress.com/2014/07/21/installing-and-compiling-with-winavr-in-windowsa-simple-avr-compilinguploading-exampletutorial/ 2/13
3/12/23, 10:37 AM installing and compiling with WinAVR in Windows(+a simple avr compiling&uploading example/tutorial) – JUST DO IT
(https://kwagjj.files.wordpress.com/2014/07/4.jpg?w=300)
(https://kwagjj.files.wordpress.com/2014/07/5.jpg?
w=260)
https://kwagjj.wordpress.com/2014/07/21/installing-and-compiling-with-winavr-in-windowsa-simple-avr-compilinguploading-exampletutorial/ 3/13
3/12/23, 10:37 AM installing and compiling with WinAVR in Windows(+a simple avr compiling&uploading example/tutorial) – JUST DO IT
(https://kwagjj.files.wordpress.com/2014/07/6.jpg?w=300)
(https://kwagjj.files.wordpress.com/2014/07/7.jpg?w=300)
(https://kwagjj.files.wordpress.com/2014/07/8.jpg?w=300)
https://kwagjj.wordpress.com/2014/07/21/installing-and-compiling-with-winavr-in-windowsa-simple-avr-compilinguploading-exampletutorial/ 4/13
3/12/23, 10:37 AM installing and compiling with WinAVR in Windows(+a simple avr compiling&uploading example/tutorial) – JUST DO IT
(https://kwagjj.files.wordpress.com/2014/07/9.jpg?w=300)
(https://kwagjj.files.wordpress.com/2014/07/10.jpg?w=300)
https://kwagjj.wordpress.com/2014/07/21/installing-and-compiling-with-winavr-in-windowsa-simple-avr-compilinguploading-exampletutorial/ 5/13
3/12/23, 10:37 AM installing and compiling with WinAVR in Windows(+a simple avr compiling&uploading example/tutorial) – JUST DO IT
(https://kwagjj.files.wordpress.com/2014/07/11.jpg?w=300)
after installation a webpage will be automatically opened. Its just a readme page but it’s good to check
out the “2.1 Manifest” part. There’s a list of what kind of programs are installed. You may not know
what does what, but later on when you understand how the compiling is done, you’ll understand why
these programs are needed and installed.
(https://kwagjj.files.wordpress.com/2014/07/13.jpg?w=300)
https://kwagjj.wordpress.com/2014/07/21/installing-and-compiling-with-winavr-in-windowsa-simple-avr-compilinguploading-exampletutorial/ 6/13
3/12/23, 10:37 AM installing and compiling with WinAVR in Windows(+a simple avr compiling&uploading example/tutorial) – JUST DO IT
(https://kwagjj.files.wordpress.com/2014/07/14.jpg?w=300)
https://kwagjj.wordpress.com/2014/07/21/installing-and-compiling-with-winavr-in-windowsa-simple-avr-compilinguploading-exampletutorial/ 7/13
3/12/23, 10:37 AM installing and compiling with WinAVR in Windows(+a simple avr compiling&uploading example/tutorial) – JUST DO IT
(https://kwagjj.files.wordpress.com/2014/07/15.jpg?w=246)
type ‘avrdude’ and press enter. If the WinAVR is successfully installed, a bunch of introduction about
‘avrdude’ will appear. Of course this isn’t the ultimate way of checking if the WinAVR is installed. If you
have check the readme after the installation, there’s a description saying that ‘avrdude’ is one of the
programs installed during the installation of WinAVR. So, typing ‘avrdude’ is just a way to see if this
avrdude program works or not.
In my case, I am using ATmega8-16PU chip. Initially, the chip seems to be working with clock frequency
of 1MHz.(This is something that I found out later. You should check the fusebit and compare it with the
datasheet to check the frequency that your chip is operation with. For checking out the fusebits, here
(http://kwagjj.blogspot.kr/2014/07/how-to-read-fuse-bits.html) is post about how to read/write fusebits.
But try this after understanding the tutorial on this page first.)
https://kwagjj.wordpress.com/2014/07/21/installing-and-compiling-with-winavr-in-windowsa-simple-avr-compilinguploading-exampletutorial/ 8/13
3/12/23, 10:37 AM installing and compiling with WinAVR in Windows(+a simple avr compiling&uploading example/tutorial) – JUST DO IT
I’m going to save this code as ‘main.c’ in a directory called ‘avrprj’ which I made new.
I’ll show you this again in steps:
※ Again, if you have read the part I recommended to read in the readme, ‘cygwin’ dlls allow us to use
simple unix command lines in cmdlet!!! this means you can use the well-known ‘ls’, ‘mkdir’, ‘rm’, ‘cp’
commands!! FYI, cygwin is another open-source project that allows window users to simulate a unix-like
environment in Windows OS. Of course, there are limitations but its really cool and one should check
this out when you have free time.
(https://kwagjj.files.wordpress.com/2014/07/a1.jpg?w=240)
In my ‘username(Black)’ directory, first I’ll show a list of files/dirs in this path.
(https://kwagjj.files.wordpress.com/2014/07/a2.jpg?w=234)
now I’ll make a new directory named ‘avrprj’. I typed ‘mkdir avrprj’. After that I typed ‘dir’ again to
check if the directory is created. You can see that a directory named ‘avrprj’ has created successfully.
https://kwagjj.wordpress.com/2014/07/21/installing-and-compiling-with-winavr-in-windowsa-simple-avr-compilinguploading-exampletutorial/ 9/13
3/12/23, 10:37 AM installing and compiling with WinAVR in Windows(+a simple avr compiling&uploading example/tutorial) – JUST DO IT
(https://kwagjj.files.wordpress.com/2014/07/a3.jpg?w=232)
move to ‘avrprj’ by typing ‘cd avrprj’. Then I typed ‘dir’ to see the contents. Clearly, there are no
contents in this directory. now, It’s time to create a file named ‘main.c’ to put in the code shown
previously. I type in ‘notepad main.c’
(https://kwagjj.files.wordpress.com/2014/07/a4.jpg?
w=300)
The notepad program executes but a popup alerting that there is no such file named ‘main.c’ exists and
asks the user if he would like to create the file ‘main.c’. Click ‘yes’ to create one.
(https://kwagjj.files.wordpress.com/2014/07/a5.jpg?
w=300)
copy and paste the example code shown previously. Save it and close the notepad.
https://kwagjj.wordpress.com/2014/07/21/installing-and-compiling-with-winavr-in-windowsa-simple-avr-compilinguploading-exampletutorial/ 10/13
3/12/23, 10:37 AM installing and compiling with WinAVR in Windows(+a simple avr compiling&uploading example/tutorial) – JUST DO IT
(https://kwagjj.files.wordpress.com/2014/07/a6.jpg?w=239)
now lets check if the ‘main.c’ has been created. type ‘dir’ again and check that ‘main.c’ has been created
successfully.
2-3) compile and upload ‘main.c’
here I present ‘three’ command lines that will compile and upload the code to the atmel chip. Here I am
using arduino uno as the avr programmer.
(https://kwagjj.files.wordpress.com/2014/07/1.png?w=300)
the first command is: “avr-gcc -mmcu=atmega8 -Wall -Os -o main.elf main.c”
after executing you will find that an object file named ‘main.elf’ has been created.
(https://kwagjj.files.wordpress.com/2014/07/2.png?w=300)
the second command: “avr-objcopy -j .text -j .data -O ihex main.elf main.hex”
this command picks up the .text and .data parts of main.elf and converts them into ihex format file
named main.hex. After execution, you’ll notice “main.hex” has been newly created.
https://kwagjj.wordpress.com/2014/07/21/installing-and-compiling-with-winavr-in-windowsa-simple-avr-compilinguploading-exampletutorial/ 11/13
3/12/23, 10:37 AM installing and compiling with WinAVR in Windows(+a simple avr compiling&uploading example/tutorial) – JUST DO IT
(https://kwagjj.files.wordpress.com/2014/07/3.png?w=300)
the third command is : “avrdude -c arduino -b 19200 -p m8 -P COM3 -U flash:w:main.hex”
this ones got a lot of options.
frst, the ‘-c’ option declares the type of avr programmer you are using. Since I’m using the arduino-
transformed avr programmer I wrote ‘arduino’. If you’re using a different avr programmer, you need to
write the correct option.
‘-b’ switch declares the baud rate I’m going to communicate with. In my case, I’m guessing because I’m
currently communicating with the arduino with a baud rate of 19200, I have to write down ‘-b 19200’
here. In other cases which I saw from googling, some don’t write ‘-b’ switch at all. I guess this depends
on other external factors.
‘-p’ switch declares what kind of chip I’m using. Since I’m using atmega8-16pu, this model is included in
the category of ‘m8’. If you have a different chip, you probably should type in a different category name
for this switch. The list of compatible chip categories are written in this webpage
(http://www.nongnu.org/avrdude/user-manual/avrdude_4.html).
‘-P’ switch declares the serial port which you will be sending the hex file to. In my case, when I opened
the sketch program(for arduino programming) I noticed that the arduino is connected to COM3 port. So,
I just wrote down ‘COM3’ for this switch. Please check which COM port the avr programmer is
connected to.
https://kwagjj.wordpress.com/2014/07/21/installing-and-compiling-with-winavr-in-windowsa-simple-avr-compilinguploading-exampletutorial/ 12/13
3/12/23, 10:37 AM installing and compiling with WinAVR in Windows(+a simple avr compiling&uploading example/tutorial) – JUST DO IT
‘-U’ declares what kind of action you will do after doing all the setting to communicate with the chip. In
this case, we are trying to shove the hex file into the chip. This is what the ‘flash:w:main.hex’ means.
‘flash’ means you’re ‘flashing’ the code in to the chip. “:w” means ‘with’ literally. “:main.hex” means that
the ‘main.hex’ file is what you wish to flash with.
After pressing enter, you’ll see a bunch of lines that show the progress of flashing. In the image above,
the flash process has been successful. If not, you might get some error messages. Here’s a tip: even
though an error occurs, that “avrdude done. Thank you” message always come up at the end. So don’t
be fooled that you’ve succeeded in uploading the code just by this message.
If the uploading is done successfully, the chip will immediately work as the code says. Here’s a video of
my atmega8 chip blinking the led on PD6 pin. You may notice that it blinks much faster than 1 second.
This is because our sample code was written under the assumption that the operation clock is 1MHz. But
recently I’ve changed the chip operation frequency to 8MHz by changing the fuse bits. This is why the
led is blinking 8 times faster than we intended.
//www.youtube.com/get_player (//www.youtube.com/get_player)
congratulations! now you know the basic steps to program an atmel chip! The procedures I wrote here
are not the only way to program the chip. You might feel a bit uncomfortable using a command line
directly. If so, you could just use the ‘programmer’s notepad’ which is installed as a part of WinAVR.
You can lookup at other WinAVR tutorials using such programs. However, I found directly using the
command lines are much useful and helpful(+and more awesome) for understanding how avr
programming works.
By the way, about the three command lines that I showed you, aren’t they kind of irritating to write
down everytime you want to compile and upload?? Here’s a really good tip: the command lines can be
stored in a script called ‘Makefile’ and you can use ‘make’ program(which is installed as a part of
WinAVR) to make this procedure much simpler!
Check out this post (http://kwagjj.blogspot.com/2014/07/what-and-how-to-usemake-your-own.html) on
how to use Makefiles. Understanding this is directly related to you’re understanding of using Mfile
program later on.
Published by mainuser
https://kwagjj.wordpress.com/2014/07/21/installing-and-compiling-with-winavr-in-windowsa-simple-avr-compilinguploading-exampletutorial/ 13/13