0% found this document useful (0 votes)
6 views

ALL ABOUT ARDUINO ASSEMBLY PROGRAMMING

The document provides an overview of Arduino assembly programming, specifically for the ATMega328P microcontroller, detailing its features, internal registers, and instruction set. It includes a step-by-step guide on how to create a simple program to blink an LED using both C/C++ and assembly code, highlighting the necessary file structure and code snippets. Additionally, it explains the logic behind the assembly code used to control the LED and how it integrates with the Arduino IDE.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

ALL ABOUT ARDUINO ASSEMBLY PROGRAMMING

The document provides an overview of Arduino assembly programming, specifically for the ATMega328P microcontroller, detailing its features, internal registers, and instruction set. It includes a step-by-step guide on how to create a simple program to blink an LED using both C/C++ and assembly code, highlighting the necessary file structure and code snippets. Additionally, it explains the logic behind the assembly code used to control the LED and how it integrates with the Arduino IDE.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Page |1

LESSON 1
ALL ABOUT ARDUINO ASSEMBLY PROGRAMMING
ATMega328P Features

First, it has 6 Analog input pins, and


its memory can go up to 32kb. Also,
it has 2kb of SRAM and up to 1kb
of EEPROM.

Then, its clock speed stands at 16


Megahertz, and it has a total of 14
I/O pins.

The chip has 3 ports, B,C, & D. Port


D gives us access to all 8 bits while
B & C provide 6 bits each.

Internal Registers
Page |2

Instruction Set
LOGIC ADD SUB AND OR MUL
BRANCH JMP RCALL RET CPI BREQ
DATA TRANSFER MOV IN OUT LDI STS
BIT & BIT TEST ROL CBI CLC SWAP POR
CONTROL BREAK NOP SLEEP WDR SBIC

Memory Map
• There are 2 bytes for general purpose registers from R0-R31.
• There are 64 I/O R0-R31
• R0-R15 are split by function
• R16-R31 are able to accept direct bit storage
• R0-R25 are 8-bit
• R26-R30 can store 16bits with the H & L bytes stored in each pair as shown.
Page |3
Page |4

Arduino IDE & Assembly Programming Model. Blink On-Board LED.

To allow an assembly program to compile in the IDE we need two files.


They must be named the same and reside in the project folder.

1. Create a new sketch and name it. Let’s say LED.ino.


2. In the top right corner of the IDE click the down arrow and
choose New Tab.
3. Name the new file LED.S (capital S). This will contain our
assembly code. The file MUST be named .S so the code is pre-
compiled, and the correct codes will be linked together
properly. If you used a .s (lower case) the code will not pre-
compile. Pre-compiling means all the sections of the app are
identified and located in memory so they can be assembled and linked with any necessary libraries.
4. In the .INO file, add this code. We are using a small sketch to manage our app.

1//-------------------------
2 // C Code for Blinking LED
3 //-------------------------
4 extern "C" //the following will be located externally to this ino file
5{
6 void start(); //define our function prototypes here.
7 void led(byte);
8}
9 //----------------------------------------------------
10 void setup()
11 {
12 start();
13 }
14 //----------------------------------------------------
15 void loop()
16 {
17 led(1);
18 delay(200);
19 led(0);
20 delay(200);
21 }

Line 4: This tells the IDE compiler we are using code outside/externally to this C code sketch. It defines the two assembly
functions we will be using: namely start() and led(byte).

Lines 6 & 7: These are the prototypes of our functions and tells the compiler what the signature of the functions will be.
In other words, besides its name, it defines the parameters and data types the functions will use. We must define our
functions in C/C++ before using them.

Line 10: The void setup() manages the app just like a regular sketch. The difference is, we have encapsulated our
functions into another file which happens to be assembly code. We could do the same thing with C++ code to separate
our code into logical sections.

Setup() calls the start() function inside the .S file to initialize our app. Notice how we are simply mixing C/C++ code we
are accustomed to with a block of assembly instructions.
Page |5

Line 15: As usual, the loop() function will make our program run in a loop. But in this case, the led() function is in the .S
file. The LED function accepts an integer as a parameter. This value will be used to turn the on-board LED ON and OFF.

Line 17: This calls the LED(1) an passes 0x01 (0000 0001) as an argument to turn LED ON.

Line 19: This calls the LED function again but passes a 0 as the argument to turn the LED OFF.

Lines 18 & 20: Here we are using the standard delay() function we are accustomed to in our sketches. This is not the
most efficient way to create a delay, however as we will see soon. But it demonstrates the mixing of the two languages
and keeps things simple for the moment.

Now, open the .S tab so we can write our assembly code. Notice comments start with a “;” not “//”.

1. /*
2. ;________________________________
3. ; Assembly Code Used to toggle the
4. On-Board LED on Pin 13
5. ;________________________________
6. NOTES:
7. Use actual pin# to toggle LED. In this case PB5. Don't use 6 even though it is the 6th bit!
8.
9. */
10. ;tells where to start storing code in IO registers
11. #define __SFR_OFFSET 0x00
12. #include "avr/io.h" ;this is used to upload code to the UNO using AVRDUDE which comes with the IDE
13.
14. .global start ;declare global variables so it can be accessed fro INO code
15. .global led
16.
17. ;----------------------------------------------------------------------------
18. start:
19. SBI DDRB, 5 ;SBI(Set Bit in IO Register) sets PB5 (D13) to output mode so we can write.
20. RET ;return to setup() function
21. ;---------------------------------------------------------------------------
22. led: ;the argument sent to led()in the INO code is stored in R24.
23. CPI R24, 0x00 ;CPI(Compare I/O bit) to 0. IF R24 == 1, THEN Jump to line 18 to turn it ON, ELSE jump to
line 17.
24. BREQ ledOFF ;BREQ(Branch If Equal to 0). If R24 is equal to 0 jump to subroutine ledOFF
25. SBI PORTB, 5 ;SBI (Set Bit in IO Register) D13 to high
26. RET ;return to loop() function
27. ;---------------------------------------------------------------------------
28. ledOFF:
29. CBI PORTB, 5 ;set D13 to low
30. RET ;return to loop() function
31.
32. /*
33. * Let’s look at IF-ELSE logic in Lines 22-26.
34. * 1. We send a 1 (0x01) the first time through the loop in the INO code to turn the LED ON. This value is stored
in R24.
35. * 2. Line 23 compares this value to 0. If R24 != 0, which it isn't, the program jumps to line 25 where it
Page |6

36. * sets PB5 HIGH and turns the led ON.


37. 3. Now we call LED again but pass a 0 (0x00) to turn it off. Line 23 compares what we sent (0 in this case)
38. * and once again compares it to 0 (0x00). Now line 23 evaluates to true so it executes line 24.
39. * 4. When line 24 branches to ledOFF(), line 29 clears bit PB5 (D13) and makes it 0 (0x00) which turns the LED
off.

Equivalent C++ code:

if(R24 == 1) //we sent a 1 to the led() function to turn it on

SBI PORTB,5; //turn it on

else

ledOFF; //turn it off because we sent a 0 to turn it off

Keep in mind that these codes are being called/controlled by the loop function inside the INO file. The return (RET)
statements direct the execution instructions to jump/go back to where these functions were called just like standard
logic in high-level lanaguages!

You might also like