This is an old revision of the document!
AVR a un bus interne de données de 8 bits, par lequel on traite les données entre l'unité arithmétique de logique (ALU), le statut du registre (SREG), le compteur du programme (le PC), la mémoire vive (SRAM) et les périphériques. Le programme, une liste de commandes, qui sont exécutées dans l'ALU viennent d'une adresse dans la mémoire flash, indiquée par le compteur du programme. L'ALU a 32 registres de 8 bits, qui sont utilisés comme des opérandes en effectuant des instructions.
L'AVR a deux étapes d'instructions. Lorsqu'une instruction est executée, la suivante est rapportée de la mémoire du programme. C'est pourquoi réaliser des instructions de saut prend 2 cycles sur l'accomplissement de la condition de saut. Puisque la nouvelle instruction est toujours allée chercher à l'adresse de mémoire suivante, il est nécessaire de renoncer à l'instruction précédente pour permettre d'aller chercher une nouvelle en sautant à une autre adresse, parce qu'il a été allé chercher une ancienne, à un faux emplacement.
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;
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
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 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.