BarCamp Fall 2010 - AVR Microcontroller Programming


AtmelAVR is a line of 8-bit microcontrollers.

What is a microcontroller? A single chip containing a CPU, program and data memory, usually an onboard clock oscillator, and some I/O peripherals.

Peripherals include digital GPIO, analog-to-digital converters, timers, PWM generators, and serial communications.

Hardware Overview

All AVR devices use a similar CPU core, which has an 8-bit data path and 2-stage RISC pipeline. Support for instructions like multiplication varies by device. The AVR CPU is a modified Harvard Architecture, which means that program and data memory are in separate address spaces. This makes some programming tasks, especially in C, difficult or impossible.

All devices include a number of GPIO pins, most also have more complex I/O facilities as well.

The CPU clock can run from either an onboard RC oscillator or an external crystal. Crystals are preferred in high-performance applications because they can be much faster and more accurate than the internal oscillator, but they require external circuitry. 8-pin tinyAVR devices are usually run from the internal oscillator.

Device Lineup

General purpose devices: tinyAVR and megaAVR.

Special purpose devices: USB, CAN, automotive, lighting, LCD


Peripherals included on most devices:

Timers/counters: An 8- or 16-bit register that can count up or down from a clock signal and can trigger interrupts or change output pins on overflow or reaching certain values. Timers can be used for simple time measurement, creating time delays, or generating PWM waveforms.

Analog-to-digital converter (ADC): Reads analog signals from an input pin and converts them to digital values, usually with 10-bit precision. Useful for reading sensors, measuring voltages, etc. These ADCs are very versatile: they usually have many different possible input channels that can be used in single-ended or differential mode, and can use an external power supply voltage or internal bandgap for their reference voltage.

Serial communications: Most AVR devices have hardware support for several serial communication interfaces, including SPI, I²C and CMOS-level USART. SPI and I²C are convenient for talking to other microcontrollers. The USART hardware is compatible with RS-232 serial ports on PCs when used with an external RS-232 transceiver (level shifter).


"Programming" refers to two different concepts in microcontrollers: designing software and loading this software into the memory on the chip.


Several assemblers and compilers are available for AVR. A popular open source C/C++ compiler is avr-gcc, a port of the GNU Compiler Collection targeting the AVR architecture.


There are 3 main ways to program AVR devices: in-system programming (ISP), parallel programming, and high-voltage serial programming (HVSP).

ISP (also called in-circuit serial programming or ICSP) is the most popular option. It is based on the serial SPI bus, uses only 4 of the chip's pins. The ISP connector can be integrated into the circuit design, allowing software to be installed in-system without transferring the MCU to a separate programming board. Parallel programming uses more pins and is only available on larger devices but can be faster. HVSP is a variant of ISP that can be used when the reset pin has been programmed to act as an I/O pin.

In all cases, a programmer device is used to connect the AVR to a computer. For ISP, the simplest option is Atmel's AVR-ISP mkII, which is USB-based and costs about $35. Other products are available such as the USBtinyISP kit or homemade cables attaching directly to a serial or parallel port.

Special Function Registers

Special hardware components are accessed by reading and writing bits in SFRs. In avr-gcc, symbols for each SFR are defined in header files and can be used as if they were ordinary variables.


Interrupts are an important tool in microcontroller programming. They allow the CPU's normal program flow to be suspended and transferred to a special location when a hardware event occurs.


Pin change: level changes on digital input pins can trigger interrupts. This is a more efficient and reliable way to respond to input signals than simply polling the pin registers.

Timers: timers can generate several interrupts, overflow, output compare, and input capture on some devices. These can trigger software routines at regular intervals more accurately than by using delay loops.

ADC, serial input/output: These functions run "in the background," and can be configured to trigger interrupts when they complete. The main program can continue with other tasks until it data is ready to be read of the device is ready to take more data to transmit.

-- StephenCavilia - 2010-10-22
Topic revision: r3 - 26 Apr 2011, UnknownUser
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding CodenamePending? Send feedback