AVR Microcontroller Coding

The question we are often asked is should I start with C or assembly language to program my AVR microcontroller? Well, there are pros and cons to both approaches.

C has many advantages, including much faster code writing and increased portability. It is also much easier to understand and modify the code later. By using library files, code can be reused easily or pre-written functions can just be added by including a library.

So, why not just use C then? Well, the main downside is the much larger code produced by C compilers. C code can often be many times bigger than the equivalent code written in assembler, especially with free C compilers like WinAVR. Commercial C compilers such as IAR are much more efficient and you are paying lots of money for optimising C code back to a size closer to the assembler equivalent. AVR microcontrollers have a much better architecture for C compilers than other microcontroller families like PIC as they were designed with C in mind (multiple accumulators, lots of SRAM and 16-bit address (pointer) registers) but C code will always be bigger.

AVR microcontrollers with larger memory are not that much more expensive now, so why not just write in C and get a larger memory? No problem, unless your project is price critical. The main problem with larger code is speed. If you want your AVR microcontroller to operate in real time, for example to read an UART, process the data and output it on SPI bus while also monitoring switches or other functions, you can easily run out of time to do all the processes needed.

In this situation, using assembly language for time critical sections makes sense, and most C compilers will let you include assembly language files or assembled code. How can you do this if you don’t know assembly language?

More importantly, C compilers hide the workings of the microcontroller, as they do all the memory management, control interrupts and peripherals so you never get an understanding about the structure of the AVR microcontroller and how it operates. It is comparable to those tools that let you design a website in 5 minutes. Yes, you can, but you will never understand how the process works and they produce huge amounts of code.

In our opinion, you should start with assembly language as this lets you understand the microcontroller. Once you have written programs in assembler, you can then move on to C programming with much more idea of what is actually going on.

All our AVR training kits take this approach, and include books that cover all the basics before moving on to C language. Click here for an overview of our AVR starter kits.

2 thoughts on “AVR Microcontroller Coding”

Leave a Reply to kandadmin Cancel reply