2.6. Atmel AVR Microcontrollers

What is a microcontroller?

A microcontroller (µC, uC or MCU) is an integrated circuit which contains a processing unit, memory and input / output peripherals. They are small, low-power and low-cost equivalents of computers used in embedded systems. Moreover, their features vary according to the different tasks they are designed to fulfill. For example, you don't expect the same things from your microwave oven and your TV remote control.

Typical microcontroller features include:

  • Central processing unit
  • Memory
    • volatile (it doesn't keep the information stored once the power is turned off): RAM
    • non-volatile: Flash, EEPROM
  • Input/Output ports
    • On your PC, the inputs received are from the keyboard and mouse, whereas the monitor, printer and speakers are output devices.
  • Serial interfaces: RS232, SPI, I2C, CAN, RS485
  • Timers
  • ADC (Analog to Digital Convertor) units

Programming with microcontrollers. Importance of datasheets.

Your desktop computer is a “general purpose computer” - it can run multiple programs at the same time. Microcontrollers are specialized computers: they do one thing well; they are dedicated to only one task. The program is stored in their non-volatile memory and runs at power on. The volatile memory is faster than the non-volatile; this is the reason why all the program data is stored in the volatile memory once the program starts executing.

Usually, you want the microcontroller to interact with the exterior: receive information from sensors, light up LEDs, command motors etc. This is done through the I/O pins, which can be input pins, output pins or bi-directional. If a bit can be used either as input or output, we need to “tell” the microcontroller how we intend to use it. In this respect, we need to write in the appropriate bit of a registry a 1 or a 0, according to the microcontroller's datasheet.

Most external signals are continuous. This means that they can take any value in an interval; in mathematical terms, this means that the signal is equivalent to a line. For instance: the temperature in a room can vary from -20 to +40 degrees and can take any value between these two; it may vary smoothly, but there is no guarantee to that. However, microcontrollers only understand binary numbers, 1s and 0s. As such, any signal received from sensors has to be transformed into data that can be processed by the microcontroller. The device that accomplishes this task is the Analog-to-Digital Converter (ADC).

Serial interfaces are used for communication with other microcontrollers or devices. Different communication protocols can provide enhanced security, higher throughput (faster transmissions), half/full duplex communication etc. The communication protocol should be chosen according to the user and application needs.

Because microcontrollers are intended to carry out real time tasks, they need a way to measure intervals of time. Essentially, timers are counters. They increment a counter at a set frequency until a threshold is reached or until it overflows. Most microcontrollers incorporate multiple 8-bit and 16-bit timers to provide the user with more flexibility.

All these internal devices are commanded through internal registries. In this respect, registries can be used for configuring devices, reading the device-specific states and/or storing important data. Every microcontroller has a datasheet that groups registries according to the device to which they belong. Also, this document contains the meaning of each bit and how setting or clearing these bits affect the respective device and your program.

Typically, all devices first have to be configured, then enabled. After this, you can use the data provided by the device. When you don't want to use a device any more, you have to disable it. Configurations shouldn't be changed while a device is enabled because it leads to unexpected behaviour.

How can we use the datasheet?

As we have stated before, every microcontroller has a datasheet (.pdf). They are typically very large, over 500 pages. However, you only need to read parts of the datasheet in order to be able to program your microcontroller. Moreover, different families of microcontrollers are very similar and code can be ported easily, with minor differences, from one microcontroller to another.

Datasheets are free to download and you can find them on the official websites of the microcontroller producers. They usually provide the datasheet and also a summary of the datasheet (under 50 pages) that only includes basic information about the microcontroller.

The first thing to do after opening a datasheet is to take a look at its table of contents (ToC). In some versions of Adobe Reader the ToC is not visible from the beginning and you can enable it from View → Show/Hide → Navigation Panes → Bookmarks.

The first pages of the datasheet always show the features the microcontroller has. When you want to buy a microcontroller for a certain application this is where you will look to see if the program specifications are met and to compare different microcontrollers.

After the list of features, the datasheet shows you different pin configurations for distinct microcontroller capsules. After buying the right microcontroller, you need to make your own circuit design (e.g. in a software program like Eagle). At this stage it is important you know which pins connect where. For instance: you can see in Figure 2. that pins number 15 and 16 are used as MISO and MOSI (needed in the serial communication through SPI). If you know that in your program you want to use SPI to communicate with an other device, you will use them as such. If you do not need this type of communication, you can use these two pins for other type of I/O operations (e.g. you can connect them to a LED or a sensor).

Most chapters refer to the internal devices the microcontroller incorporates (e.g. timers, ADC, USART) - for which you can also view the block diagram to see how the devices are connected internally. Other chapters help the programmer by providing configuration information for achieving different functionalities (e.g. interrupts, USART in SPI mode). In both cases you are given the features and an overview of the respective modules, configuration information (how to configure your module for different outcomes), as well as their respective register description.

The register description is the most important part of the datasheet, which we will be using intensively. If you are working with an 8-bit microcontroller, then most of its registries will have 8 bits (a byte). Some registries might have 16 bits, but they will be split into two bytes (high and low). In Figure 1 you can see how a register is defined in the datasheet. Each register has a name (in this case TCCR1A), which you can use as is in the code (it is defined in the AVR library). Moreover, each usable bit has a name (e.g. bit number 5 is COM1B1). Some bits are unused and are marked by a ”-”. Bits can be read-only, write-only or read-write; in Figure 1 it is clear which bit is of what type. Moreover, you can also see what is the initial value of a bit. After each register, every bit or group of bits is described in terms of what functionalities described previously they influence when they change their value.

Figure 1. 8-bit microcontroller register description.

At this point you know what you want to use - e.g. you need a timer and you need to program it. You just have to look in the ToC, choose a type of timer (8 or 16 bit), read about how to configure it and modify its registries as your application requires.

There are usually 5-10 registers per device and you don't usually use them all at the same time. This makes scrolling through the register description of the devices really easy. In fact, this is the fastest way to find a bit or a register you are looking for. If you would try to use Ctrl-F (the find function in Adobe Reader), you will see that the occurrence of the string typed is very high in the 500+ page datasheet. Thus, finding what you are looking for becomes a cumbersome, dull task.

What is a pin?

A microcontroller uses its pins to interact with the exterior. A pin can be digital (it handles binary values) or analogue (it can handle a wider range of values); the differences between analogue and digital will be detailed in Chapter 2.8.

Pins can be classified as input, output or bidirectional. Input pins receive data from the exterior. Output pins command devices or send information. Bidirectional pins can be used as either input or output pins.

What is a port?

The 3PI microcontroller (ATmega328P) has 4 ports: A, B, C and D. Each port has assigned 8 pins. In Figure 2 you can see which pin belongs to what port. E.g. pin number 30 is PD0 meaning that it is pin number 0 from Port D. If a pin belongs to Port B its name will start with “PB”, followed by a number from 0 to 7.

Each port has three 8-bit registers: a data register (PORTx), a data direction register (DDRx) and a port input pin (PINx) - with x representing the port letter (A, B, C or D).

  • The DDR register has 8 bits, each one corresponding to a physical pin. It selects if the pin will be used as input (bit written as 0) or output (bit set as 1).
  • The PORT register is used for writing the output data or for activating pull-up resitors (depending on the direction of the data set in the DDR register).
  • The PIN register is used for reading input data.

The I/O ports are more thoroughly described in the datasheet (section 14).

Atmel AVR family of microcontrollers

AVR is a family of microcontrollers produced by the Atmel Corporation. Generally, AVRs can be classified into six groups:

  • tinyAVR — the ATtiny series
    • 0.5–16 kB program memory
    • 6–32-pin package
    • Limited peripheral set
  • megaAVR — the ATmega series
    • 4–256 kB program memory
    • 28–100-pin package
    • Extended instruction set (Multiply instructions and instructions for handling larger program memories)
    • Extensive peripheral set
  • XMEGA — the ATxmega series
    • 16–384 kB program memory
    • 44–64–100-pin package
    • Extended performance features, such as DMA, “Event System”, and cryptography support.
    • Extensive peripheral set with DACs
  • Application-specific AVR
    • megaAVRs with special features not found on the other members of the AVR family, such as LCD controller, USB controller, advanced PWM, CAN etc.
  • FPSLIC (AVR with FPGA)
    • FPGA 5K to 40K gates
    • SRAM for the AVR program code, unlike all other AVRs
    • AVR core can run at up to 50 MHz
  • 32-bit AVRs
    • more advanced, supports Linux-based operating systems

The size of the program memory is indicated by the name of the component. For example, ATmega328 has a 328kb of Flash memory.

AVR microcontrollers have a two-level execution pipeline, allowing the next instruction to be brought from memory(Fetch) while the current one is in execution(Exec).


The high-performance Atmel picoPower 8-bit AVR RISC-based microcontroller combines 32KB ISP flash memory with read-while-write capabilities, 1024B EEPROM, 2KB SRAM, 23 general purpose I/O lines, 32 general purpose working registers, three flexible timer/counters with compare modes, internal and external interrupts, serial programmable USART, a byte-oriented 2-wire serial interface, SPI serial port, a 6-channel 10-bit A/D converter (8-channels in TQFP and QFN/MLF packages), programmable watchdog timer with internal oscillator, and five software selectable power saving modes. The device operates between 1.8-5.5 volts. By executing powerful instructions in a single clock cycle, the device achieves throughputs approaching 1 MIPS per MHz, balancing power consumption and processing speed[1].

The main features of the ATmega328P are:

  • Operating Voltage - 1.8 - 5.5V
  • Advanced RISC architecture
  • Clock Speed: 16MHz
  • Memory: 32KB Flash program memory, 1KB EEPROM, 2KB Internal SRAM
  • Two 8-bit Timer/Counters
  • One 16-bit Timer/Counter
  • Six PWM Channels
  • 8-channel 10-bit ADC
  • Programmable Serial USART
  • Master/Slave SPI Serial Interface

The same microcontroller can be packaged in different capsules, according to the user's needs. The 3PI has a capsule of type TQFP with 32 pins, as you can see in Figure 2.

Figure 2. ATmega328P pinout for the TQFP capsule.


roboticsisfun/chapter2/ch2_6_avr_microcontrollers.txt · Last modified: 2012/10/28 19:59 by liana.marinescu