AVR Microcontroller C Code

The AVR microcontroller is excellent with C code because it was designed with C in mind. Atmel have also made it easy to use C in AVR Studio, as WinAVR compiler can run from within AVRStudio just like the assembler.

Only a few years ago, using C compilers involved complicated commandline instructions and make files but WinAVR more or less takes care of this for you. WinAVR is based on the free GNU-GCC compiler but is much friendlier, especially for beginners.

When you create a project in AVRStudio, you just choose C project and write your code in the editor. Then you just click Make and WinAVR does the rest. There are a few settings to be aware of in the compiler that you should set.

Go to Project – Configuration Options menu, and on first screen check these points:

  • Check Device matches your device. This is a different device setting from  the AVRStudio project device setting
  • Set Optimization to -0s at first otherwise lots of example code won’t run properly
  • The default output file is [project-name].elf. This is the debug file format, used by simulator and emulators like JTAG ICE and AVR Dragon. For output for programmers as well, check Create Hex File as this is the file for programmers.
  • Output files including the Hex file are created in a sub-folder called Default

That’s it.  Now you can paste in C code (or write it) and compile it. Emulators can step through C code in AVR Studio just like assembler code, which makes debugging much easier.

We have some C code example projects on our support pages.

  1. AVR C UART Code
  2. AVR C LCD Code
  3. AVR C Keypad Code

Apart from WinAVR C compiler, there are professional compilers that produce  smaller and faster code but they tend to be expensive. IAR do a free version of their compiler called Kick Start, which is limited to 4KB of output code. This is enough for most simple projects. Kanda Starter Kits include a book on getting going with embedded C programming.

 

One thought on “AVR Microcontroller C Code”

Leave a Reply

Your email address will not be published. Required fields are marked *