The ATmega is a microcontroller series by Atmel. It is an 8-bit architecture, with chip variants depending on the needed memory, peripherals and package size. Below are code examples for the ATmega range of microcontrollers.

A photo of the Atmel AVR ATmega168 microcontroller. Image from
A photo of the Atmel AVR ATmega168 microcontroller. Image from

Child Pages

ATmega General I/O
ContentsConfiguring PortsWriting To PortsGeneral I/O are pins that are used for non-specific purposes. Non-specific purposes are those not requiring any of the on-board peripherals that come with the ATmega micro-controllers (such as USART, timers). They are essentially pins which can be set either to digital high or digital low by the software. Configuring Ports x … Continue reading ATmega General I/O
ATmega Header Inclusions
Include files that contains functions that are used by your code. Use double quotation marks (“) around files that are in the same directory structure as your code, and use greater than, less than symbols for files that are in the default win-avr directory. e.g. Included file is in the same directory structure as your … Continue reading ATmega Header Inclusions
ATmega Interrupts
ContentsOverviewAVR-LibCWriting Interrupt Handler FunctionsEnabling/Disabling InterruptsOverview Interrupts can be triggered from many sources in an AVR architecture. Typical examples include timer compares, logic level changes on external pins, watchdog timer overflows and usart buffer empty or byte received interrupts. The individual interrupts are enabled by setting a bit in the configuration registers for each peripheral, and … Continue reading ATmega Interrupts
ATmega Power Management And Sleep Modes
ContentsOverviewAVR-LibC LibrarySleep ModesPutting The Micro To SleepThings To Watch Out ForOverview The ATmega architecture provides power saving modes of operation to reduce to total power consumption of the microcontroller. The number and type of power saving modes depends on the exact chip in the ATmega family. Power saving modes are very useful when designing an … Continue reading ATmega Power Management And Sleep Modes
ContentsSpecsDownloadI wrote some code to deal with communication through USART between two micro-controllers. The comms code has message identification, variable data length capability and does not need to use any interrupts. It is designed to be used with the USART module on the ATmega8 but it could be changed to use any communication protocol on … Continue reading ATMEL ATmega – UART
ISP vs. JTAG, PonyProg, AVRDUDE, AVR MkII, wiring diagrams, pinouts, the reset line and more info about programming ATmega microcontrollers.

Code Examples

One annoyance with the ATmega series is that Atmel wrote all the code to compile with IAR Compiler, a paid for IDE, and not their own proprietary and free AVR Studio. This means that you have to port the code to work with the Win-AVR compiler if you want to use ATMEL’s code examples. The main differences between the two compilers are precompiler directives (e.g. #pragma), delay functions, and the handling of variables that are stored in flash.

Code Compatibility

ATMEL has done a good job at keeping code as similar as possible between microcontrollers in the ATmega family. One point to note is that at some point, they decided to add a ‘0’ to all peripheral register descriptions, even when there was only one of these peripherals present in the chip (e.g. USART0 instead of USART). This generally applies to all the registers that are associated with the peripheral (e.g. RX0 and TX0 instead of RX and TX). You may have to add these before code will compile when using older source code with the newer avr libraries.