Architecture

AVR has an internal 8-bit data bus, through which the data is moved between the arithmetic logic unit (ALU), status register (SREG), program counter (PC), random access memory (SRAM) and peripherals. The program, an array of commands that is executed in the ALU, comes from an address in the flash memory, specified by the program counter. The ALU has 32 8-bit general purpose registers, which are used as operands when carrying out instructions.

Block diagram of ATmega128

Instruction Pipeline

AVR has a two-stage instruction pipeline. While one instruction is executed, the next is fetched from the program memory. This is why carrying out jump instructions takes 2 cycles on fulfillment of the jump condition. Since the new instruction is always fetched from the next memory address, it is necessary to discard the previously fetched instruction and fetch a new one when jumping to another address, because it was taken from an old, wrong location.

General Purpose Registers

General purpose registers R0-R31 are like buffers for storing and operating with memory and peripheral data. They simplify the architecture of the processor, because they are quickly accessible by the ALU, and the use of the data bus to read operands from the memory is not necessary for every operation. General purpose registers are used for performing all data-related arithmetical and logical operations.

While programming in assembler, it is possible to store the urgent data in general purpose registers. While programming in C and a need to store a variable in a general purpose register arises, the variable is additionally defined as “register”. For example:

register char x;

Instruction Set

The instruction set of most AVRs consists of 90-133 different instructions. ATmega128 has 133 instructions. Instructions have either one, two or no operands. Most instructions take only one cycle to complete, but the more complex ones can use up to 5 cycles. For XMega, the successor of AVR, several instructions have been modified to use less cycles. Most instructions in AVR are used for jumps, moving and comparing data and executing arithmetic calculations. A status register is used for performing calculations and comparisons. It stores the output status of the ALU - whether the result was negative, positive, zero, exceeded the maximum allowed value (8 bits), needed to transfer a bit to the next operation etc (there are a few more complex cases).

Example

This is a piece of code written in Assembler and consists of pure instructions, which adds 5 to the byte at random access memory address $100 (decimal 256). These instructions exist in all AVRs.

ldi  r1, 5       ; Load the constant 5 to general purpose register r1
lds  r2, $100    ; Load the byte from the memory to register r2
add  r2, r1      ; Add the value of r1 to r2
sts  $100, r2    ; Write the value of r2 back to the memory

Program Stack

Stack is a data structure, where the last data written to the memory is read out first. AVR's stack can be used while operating with subroutines, interrupts and temporary data. Before executing a subroutine or interrupt, the address in the program counter where the program was interrupted is stored in the stack. When the subroutine or interrupt has finished its execution, this address is read back from the stack and the program continues from the address it left off from before. Storing temporary data in the stack is usually used when dealing with shorter chunks of code, which do not require reserved memory throughout the execution of the program. Simpler assembler programs are usually written so that it is not necessary to use the stack, but if the program contains a lot of variables and functions, the compilers automatically make use of it.

The stack of MegaAVR series microcontrollers is physically located in the random access memory. Some tinyAVR series devices do not have a random access memory at all and the stack is realized as a separate, quite limited memory unit. Typically there are no compilers for devices with no random access memory.

To program in a high level language (Pascal, C, C++), it is not necessary to be familiar with the inner workings of the microcontroller, because the compiler is capable of selecting general purpose registers and instructions by itself, but knowing what goes on in the controller is certainly beneficial. It is necessary to know the instructions of the microcontroller when developing time-critical applications, where the operations have to be completed in a limited amount of cycles.

en/avr/architecture.txt · Last modified: 2013/03/26 23:15 (external edit)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0