3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Build your own (cheap!) multi-function wireless camera controller.

Step 11Writing and Compiling AVR Code

Writing and Compiling AVR Code
Writing the code is very simple, you can do it in C or Assembly Language. I chose C because i'm more comfortable in it and it's easier for others to see what i'm doing. You can use any text editor to make the .c files (and any headers, etc) and my preferred method for compiling/linking is using the command line (it is pretty easy).

First of all, download a copy of WinAVR - http://winavr.sourceforge.net/ or MacAVR http://www.harbaum.org/till/macavr/index.shtml

WinAVR is a suite of useful tools such as the compiler, code headers/libraries and more. Basically all you need to start programming and uploading your code.

Coding for AVRs is very simple. You simply write out your code using normal C, you can include standard libraries for sorting, string manipulation, maths and much more. You need to include the relevant AVR libraries such as the io, sleep, interrupt packages depending on what your code contains (the library is always necessary).

Once the code is written, it must be compiled and linked. The easiest way to do this is using a makefile. The makefile does all the hard work for you, all you do is specify the name of your C files and any assembly files (must be called .S - case sensitive) and the processor you're using. This file goes in the same directory as your source code.

There is a good template here: http://electrons.psychogenic.com/modules/arms/art/8/AVRProjectOrganizationStandardizedAVRMakefileTemplate.php

I won't go through the process of writing a make file, the template above is commented and should be obvious :)

To actually compile your code into the .hex binary that your micro can understand, we just use the command prompt. Navigate to the directory you need using the "cd" command, replacing with the directory of the file. Then simply type "make hex" and press enter. What should result is a few lines of writing that you can ignore and a .hex file should appear in the directory with your source code. If anything went wrong, the compiler will spit out an error usually with a line number and the file that the error was in. You can then go to the line reference, fix the problem and try again. The code i provide here should be compilable, or at least compiles on my machine. I won't say bug-free because no code ever is!

I will try, if i can to give a run down of what the code does.  It is pretty well commented in my opinion so should be fairly self explanatory for seasoned coders. The code we'll be using is fairly complicated and deals with a lot of things that newbies want to know, dealing with input and output, interfacing with a display, using the ADC and Timer interrupts, sleep modes and more. It was a great primer for me at any rate =D

Ok, lets go onto your first program and try to upload it!
« Previous StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
15
Followers
1
Author:Whiternoise
I'm a third year physicist at Warwick University, dabbling in electronics and photography and currently seeing what interesting combinations you can make with the two :)