Avr Atmega328p Arduino Sample Explain Internal Function - Assembler Code

by BoboacaC in Circuits > Arduino

136 Views, 0 Favorites, 0 Comments

Avr Atmega328p Arduino Sample Explain Internal Function - Assembler Code

Arduino-PC-connection-s-new.png
1.jpg

In this tutorial will try to explain how code really work internal arduino with atmega328p microcontroller. Keep in mind that assembler for atmega328p is not same as assembler for esp8266 !!! The code in ARDUINO IDE can be same ... but assembler code result is tottaly different because is two microcontroller architecture more different. In this tutorial will explain architecture of atmega328p and the most good way is with assembler code. Also you can try to compile code and send to microcontroller Arduino with atmega328 from this page https://www.costycnc.it/avr1

Supplies

arduino-nano.jpg

You need an arduino with atmega328p microcontroller can be arduino uno or nano

A usb cable to connect arduino to computer.

A computer where need to install ch341.zip driver if your arduino have ch341 module onboard

An open https://www.costycnc.it/avr1 for test

Let's Get to Know Arduino Avr Registers

boeing 747 .png

The picture in the top is

The pilot's seat and cockpit of a Boeing 747 passenger jumbo jet ... has 980 buttons and dials. Here's a pic... it's like a endless sea of gauges.

Source : https://www.reddit.com/r/pics/comments/5vv8qt/the_pilots_seat_and_cockpit_of_a_boeing_747/

Arduino have 255 registers and any register have 8 bits ... so in total , like Boeing 747 , arduino have 255x8= 2040 switches !!!

But dont worry ... many of them is unitilised and another is register for data ... so no switches!!!

But set any bit of some register is same as how pilot prepare Boeing 747 before to fly setting these switch on or off ...

For example at arduino to write a port to high or low need to write one to bit of register.

For example if want to make PORTB0 to 1 (PORTB0 is equivalent to D8) need make 1 the bit 0 of register 5

So if a bit 0 of register 5 is set to 1 and all remainder is set to 0 ... in this case PORTB0 that coresponding at D8 will be on. The onboard led is D13 that corespond on PORTB5.

So again...

If want make LED ONBOARD ON !!! need to set to 1 bit 5 on register 5

In assemble code this mean SBI 5,5 ... so first 5 is register and second 5 is a bit

Also if declare:

.equ PORTB = 0x05

You can write:

SBI PORTB,5 or SBI 5,5 ... is same !!!!

It seems complicate but is not if you can try to understand these base principles of programming mode.

Is essential to understand good these base principles to understand better and easy the arduino and internal registers.


Downloads

Let's Get to Know Arduino Avr Opcodes

mnemonic.png

Now, I'll try to explain how assembly code actually works! Understanding these basic concepts is crucial because it helps you see how Arduino works behind the scenes.

Sure, you might not be able to build every project in assembly language, but knowing assembly and the Arduino architecture will empower you to create advanced projects in high-level languages like C++. Additionally, if you ever encounter a situation where there's no readily available library, you can write a few lines of assembly code to create a custom solution.

Back to our tutorial!

What Does SBI 5, 5 Mean?

I presume you understand the meaning of 5, 5 in the SBI 5, 5 instruction. But what is SBI?

SBI: The Opcode

SBI is an opcode, which is a human-readable word representing a specific numerical value. It's a way for us to understand the instruction more easily.

Referring to the AVR Instruction Set Manual:

If you refer to the Atmel AVR Instruction Set Manual (page 99) https://ww1.microchip.com/downloads/en/devicedoc/atmel-0856-avr-instruction-set-manual.pdf, you'll find the definition of SBI:

SBI – Set Bit in I/O Register

  • Description: Sets a specific bit within an I/O register.
  • Operation: Sets bit b in I/O register A.
  • Operands:
  • A: I/O register address (0 to 31)
  • b: Bit position within the register (0 to 7)

Understanding Memory Representation is Beneficial

While it might not be essential for basic programming, understanding how code is stored in memory can significantly enhance your grasp of low-level AVR programming for the ATmega328. This concept is generally similar across most microcontrollers, with some specific variations.

From Assembly to Machine Code

When you write SBI 5, 5 in assembly, an assembler translates it into machine code that the ATmega328 understands. While we humans interpret SBI as "Set Bit in I/O register," the ATmega328 doesn't understand this. It only recognizes the actual machine code.

Why Assembly Uses Symbols

Assembly languages use symbols (like SBI) because humans can't work effectively with just numbers. These symbols make code much easier to understand and write. Ideally, we could work directly with numbers, but since that's not practical, we rely on assemblers to translate code for us and for the microcontroller.

SBI 5, 5 Breakdown:

Let's break down how SBI 5, 5 is translated for the ATmega328:

SBI A, b        > Machine Code >  1001 1010 AAAA Abbb
  • First Byte (Opcode):
  • Binary: 0b10011010
  • Decimal: 154
  • Hexadecimal: 0x9A
  • Second Byte (Register and Bit):
  • A (register): 5 in binary is 0b101
  • b (bit): 5 in binary is 0b101
  • Combined: 00101 (register) + 0101 (bit) = 00101101
  • Binary: 0b00101101
  • Decimal: 21
  • Hexadecimal: 0x2D

Therefore, SBI 5, 5 translates to 0x9A2D in machine code.

Understanding Binary and Hexadecimal

I understand that binary code might seem complex at first. However, with practice and examples, you'll find it's not as difficult. Once you grasp it, you'll be able to understand and configure registers more efficiently.




Understand the Compiler and Verify Your Code

finally-hex.png

So, when you compile the instruction SBI 5, 5, you'll see the hex code 2D9A as the result. This is the code that the ATmega328 understands!

Now, you might be wondering why it's 2D9A and not 9A2D. The reason is because the ATmega328's memory is organized in little-endian order. This means that the least significant byte (LSB) of a 16-bit value is stored in lower memory address, followed by the most significant byte (MSB).

Final

So, focus on understanding these core concepts for now. In the next tutorial, we'll write some applications in assembler code that involve interrupts, registers, opcodes, binary, and hex values. These are the fundamentals (the ABCs) of programming that any beginner in AVR assembler for the ATmega328 needs to know.

It's not helpful to know advanced C++ libraries or algorithms for AVR assembler on the ATmega328 if you can't yet use basic interrupts, registers, and opcodes. Understanding these core concepts will give you a solid foundation for low-level programming and a deeper understanding of the ATmega328.

Here's why these concepts matter:

  • They're the building blocks. Mastering them gives you precise control over the microcontroller's hardware.
  • Optimization and Efficiency. You can write code that performs better for specific tasks.
  • Direct Hardware Interaction. You can work with external devices and sensors at a lower level.
  • Custom Functionalities. You can create hardware features that aren't readily available in higher-level languages.
  • Deeper Understanding. You'll gain a better understanding of how hardware and software interact.

Learning Curve:

You're right; advanced C++ libraries and algorithms might not be directly applicable here. But a strong foundation in these core concepts will enable you to build more efficient and hardware-aware programs in AVR assembler. Once you grasp these fundamentals, you can build upon that knowledge and explore libraries and algorithms specifically designed for AVR assembler.

Additional Tips:

  • ATmega328 Datasheet: This document provides detailed information about registers, interrupts, opcodes, and memory organization.
  • Online Resources: Many websites and courses offer AVR assembler programming instruction.
  • Practice Makes Perfect: Start with simple programs like blinking an LED or reading a button press. Gradually increase the complexity as you gain confidence.

I hope this clarifies the importance of these core concepts for AVR assembler programming on the ATmega328! Feel free to ask if you have any questions.