AVR Microcontroller Fundamental speed run

RIXED LABS
RIXED_LABS
Published in
5 min readMay 21, 2021

0x0: Introduction

Hello there, hope you are doing good, I just started with RISC or Reduced Instruction Set Architecture last week which is quite different from the existing CISC or the Intel architecture we mostly deal with, so this small blog will be an overview of AVR 8 microcontroller, about its registers, the instruction set nomenclature, and a few more stuffs which might be new to know for people who never crossed path with this microcontroller & a speed run fundamental revision for people who have already been working on projects using the 8- bit microcontroller.

0x1: As always, with the registers & operands!

So, during most of the tutorials related to CISC architecture, I probably gave a start with describing the number of register and a small info about them, here in the RISC architecture also I will be doing the same i.e. describing about the registers, but wait! Did I tell you that there’s a crucial difference I am about to skip that here AVR is based on Harvard Architecture, and all those previous tutorials were based on Von Neumann Architecture, aight ! Let’s not focus on these not so complicated seeming terms and focus on this fact that on this AVR thing separate physical memory address is used for instructions and data whereas on all those Intel based blogs or those involving CISC architecture uses same physical memory address for instructions and data, now let’s focus on the register & operands without any further delay.

Registers in AVR are no different in use from other microcontrollers or microprocessors they serve the same purpose of being special storage, just with a different size capacity which is 8 bits here, some advantages of register compared to other storage sites are like they are connected directly to the CPU , can be used directly in assembler instructions, mainly as targets or as read registers for calculation, there are 32 registers in an AVR and originally denoted from R0 to R31 as per directive. In AVR some requisites of using a register can be like if we need pointer access we must reserve R26 to R31 for that purpose, most registers for calculation purposes are best from R1 to R15 and lot more directives which might help us to write effective Assembly program in AVR. Let’s understand use of any preferred register of our choice:

CLR R12 ;This clears our preferred register and sets it to zero

There are a lot of instructions, we will go through the instructions in a while 😄.

Let’s check out an interesting subtopic under register that is pointer registers, the register pairs R27:R26, R29:R28 & R31:R32 are quite special, and have short names X, Y, Z and of course they are understood by the assembler, these pairs are actually 16 bit pointer registers, and are capable of pointing to addresses with maximum 16 bit of length.

LD R1,X ; denotes that reading from some address X in the memory LD R1,X+ ;denotes that reading from some address X and increment the    ;pointer after wards by oneLD R1, -X ;Denotes that decrement the pointer by one and read from the new address afterwards

0x2: Ports

Ports in the AVR are like roadways from the CPU to the external hardware components, one of the most used ports is the flag register, where flags from previous operations based on instructions are written to and branching conditions are read from, the accessibility of the ports depend on storage space and other factors, ports in AVR have a fixed address, and mostly while programming AVR we do not need to remember those port addresses as mostly they are included in the header files.

0x3 : The Instruction Set Summary

The instruction set summary in AVR can be categorized into 5 types of which are Arithmetic & Logical instructions mostly used for performing calculations storing value after calculations and perform logical operations like ADC, ADD, NEG, CBR and lot more. The MCU Control instructions are like the Sleep, Break and much more which perform these operations, the data transfer instructions are mostly used for doing data transfer from/between registers/immediate addresses using instructions like LD, LDI, MOVW and lot more, the Bit & bit test instructions perform operations like logical left shift , rotate left through carry and lot more using instructions like ROL, ROR, LSR etc. And the last set of instructions according to the mind map above is the branch instruction which performs branching using various instructions like RJMP for relative jump & IJMP for Indirect Jump.

0x4: Writing the first program & understanding the program

;helloworld.asm.include "./m328Pdef.inc"ldi r15, 0x20
out DDRB, r15
out PortC, r16
Start:
rjmp Start

Now let us understand the code, the ;helloworld.asm is the comment we have added into our code just for ease of understanding here it stands for the name of the file , the include “.m328Pdef.inc” is an include file, you can include then header file depending on the AVR, this header file used above belongs to ATmega328P, had it been some different board we have to use different header file, and these header files make sure we can access the ports and register of that specific AVR board. Then the instruction “LDI” we used stands for Load immediate and says directs the assembler to take the register (R15) and load up the value 0x20 which is an hexademical, now the program uses a data transfer instruction out, which directs to copy the contents of the register r15 to the DDRB register, which stands for Data Direction Register B and sets up the pins on PortC, finally the rmjp Start actually is a branching instruction which denotes that relative jump to the label start, in simpler words the computer is placed into an infinite loop which keeps falling back to the Start.

0x5: References

0x6: Conclusion

That was a very quick overview of the AVR assembly and some small bits regarding it, and has been written by someone who has two week learning experience into RISC architecture, if you find any issues into this blog feel free to reach me out, I am always open to learn from my mistakes. Till then happy learning ! 😃

Blog by Nerd of AX1AL, join us on our small discord server.

--

--