Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:multiasm:exercisesbook:arduinouno [2026/03/26 23:12] – [Instructions] pczekalskien:multiasm:exercisesbook:arduinouno [2026/03/29 17:24] (current) – [Examples] pczekalski
Line 1: Line 1:
 +====== Introduction to the Arduino Uno programming in Assembler ======
 +
 +The following chapter assumes that you are familiar with basic assembler operations for AVR microcontrollers. Below, we explain the most important construction elements and assembler instructions for manipulating the Arduino Uno's (figure {{ref>arduinouno}}) GPIOs, based on the ATmega328P microcontroller.
 +
 +<figure arduinouno>
 +{{:en:iot-open:getting_familiar_with_your_hardware_rtu_itmo_sut:arduino_and_arduino_101_intel_curie:programming_fundamentals_rtu:arduino_uno_top_c.jpg?400|}}
 +<caption>Arduino Uno development board</caption>
 +</figure>
 +
 +===== GPIO and Ports =====
 +
 +The Arduino Uno exposes a number of GPIOs that can serve as binary inputs and outputs, analogue inputs, and many of them provide advanced, hardware-accelerated functions, such as UART, SPI, I2C, PWM, and ADC. In fact, not all of the pins on the development board are such "general-purpose": some of them provide specific features, while others do not: there is no internal multiplexer, so functions such as UART, I2C, SPI, PWM and ADC are bound to particular GPIOs and cannot be changed.
 +
 +On the programming level, GPIO ports are grouped into 3 "ports" (figure {{ref>arduinoports}}), and it is how you can access them:
 +  * PortB, with GPIOs from D8 to D13,
 +  * PortC, with GPIOs from port A0 to A5,
 +  * PortD, with GPIOs from D0 to D7.
 +
 +A bit in the port corresponds to a single GPIO pin, e.g. bit 5 (6th, zero-ordered) of PortB corresponds to GPIO D13 and is connected to the built-in LED.
 +
 +<figure arduinoports>
 +{{:en:multiasm:exercisesbook:assembler_arduino_uni.drawio.png?600|}}
 +<caption>Arduino ports</caption>
 +</figure>
 +
 +
 +
 +===== IO Registers =====
 +Each Port has assigned three 8-bit registers:
 +  * DDRx (Data Direction Register): there are 3 of those registers, one per Port (B, C, D): DDRB, DDRC and DDRD. This registers configures GPIO as Input (0) or Output (1). Configuration is done "per bit", so it is equivalent to controlling each GPIO individually.
 +  * PORTx (Port Data Register): there are also 3 of those registers: PORTB, PORTC and PORTD. The operation depends on the value of the specific bit in the corresponding DDR register; either pin is configured as input or output:
 +    * If a specific GPIO pin (represented as a bit in the related DDRx register) is set as output, then PORTx bit directly affects the GPIO output: 1 is HIGH (+5V), while 0 is LOW (0V).
 +    * If a specific GPIO pin is set to input, PORTx value controls the internal pull-up resistor: 1 enables pull-up, 0 disables it.
 +  * PINx (Pin Value Register) represents the current input state of the GPIO.
 +
 +==== Instructions ====
 +There is a set of assembler instructions that operate on Ports (I/O registers), as shown in table {{ref>assemblergpioinstructions}}. 
 +<note>Assembler-level operations using ports are much faster than ''DigitalRead'', ''DigitalWrite'', and other instructions in C++, roughly 50 times faster.</note>
 +
 +<table assemblergpioinstructions>
 +<caption>Common GPIO-related, I/O instructions</caption>
 +^ Instruction  ^ Description                                                      ^
 +| ''SBI''      | Set bit in register                                              |
 +| ''CBI''      | Clear bit in register                                            |
 +| ''SBIS''     | Skif if bit in register is set (1)                               |
 +| ''SBIC''     | Skip if bit in register is clear (0)                             |
 +| ''IN''       | Read hardware register to the general-purpose register (R0-R31)  |
 +| ''OUT''      | Write the general-purpose register to the hardware register.     |
 +| ''ANDI''     | Masks a bit                                                      |
 +| ''ORI''       | Sets a bit                                                       |
 +</table>
 +
 +A common scenario is to first set either the GPIO is input or output (using the correct DDRx register), then either set (''SBI''), reset (''CBI''), check (''SBIS'', ''SBIC''), read the whole register (''IN'') or write the whole register (''OUT'').
 +<note tip>''IN'' and ''OUT'' instructions operate on whole, 8-bit registers rather than on single bits. Those are general-purpose instructions, covering the whole range of IO registers (0-63), beyond aforementioned DDRx, PORTx and PINx registers.</note>
 +
 +==== Examples ====
 +** Template for the assembler code **\\
 +
 +Using plain assembler (not C++ + assembler) requires a specific construction of the application where the program is located (loaded) into memory exactly at 0x0000.
 +
 +<code asm>
 +
 +    .org 0x0000
 +    rjmp start
 +
 +start:
 +...
 +</code>
 +
 +It is common practice to use ``rjmp`` (relative jump), which makes is easier to place data before the start of the code. And it is a good "embedded" practice to keep it even, if it does not really jump, as in this example. Forgetting to put it may impact your programming experience later, when you decide to declare some data.
 +
 +** Core I/O registers and their IDs **\\
 +To operate on I/O registers, the developer must either include a library with definitions or (when programming in pure assembler) declare them on their own.\\
 +Below there is a table {{ref>ioregisterids}} with a list of I/O registers used to control GPIO (Ports B, C and D) and their addresses:
 +<table ioregisterids>
 +<caption>I/O registers and their addresses (IDs)</caption>
 +^ Name  ^ Address (I/O) ^ Description ^
 +| PINB  | 0x03 | Input pins register (Port B) |
 +| DDRB  | 0x04 | Data direction register (Port B) |
 +| PORTB | 0x05 | Output register/pull-up enable (Port B) |
 +| PINC  | 0x06 | Input pins register (Port C) |
 +| DDRC  | 0x07 | Data direction register (Port C) |
 +| PORTC | 0x08 | Output register/pull-up enable (Port C) |
 +| PIND  | 0x09 | Input pins register (Port D) |
 +| DDRD  | 0x0A | Data direction register (Port D) |
 +| PORTD | 0x0B | Output register/pull-up enable (Port D) |
 +</table>
 +The easiest is to declare constants (converted to values at compile time) and insert them before the code starts (note that they do not exist in memory, so do not disturb code placement and proper execution):
 +<code asm>
 +; I/O registers
 +.equ PINB,  0x03
 +.equ DDRB,  0x04
 +.equ PORTB, 0x05
 +.equ PINC,  0x06
 +.equ DDRC,  0x07
 +.equ PORTC, 0x08
 +.equ PIND,  0x09
 +.equ DDRD,  0x0A
 +.equ PORTD, 0x0B
 +
 +; your code starts here
 +    .org 0x0000
 +    rjmp start
 +
 +start:
 +...
 +</code>
 +
 +<note>''.equ'' is converted into a value and substituted in the code during compile: thus it does not exist in the final, compiled binary code.</note>
 +<note>Depending on the compiler you use, there are two standards of syntax. You can find the correct ''__.equ PINB,   0x03__'' or ''__.equ PINB = 0x03__''</note>
 +
 +Below are sections representing common usage scenarios for GPIO management:
 +
 +**USE GPIO as output**\\
 +In this scenario, we use GPIO as an output. The simplest is to use the built-in LED to get instantly observable results.\\
 +The built-in LED is connected to GPIO13 (D13) and is controlled via PortB (5th bit, zero-based indexing; see figure {{ref>arduinoports}}). The built-in LED is enabled in the LOW (0) state and off in the HIGH (1) state on GPIO13.
 +It is also convenient to declare a bit number representing the built-in LED position in PortB, so instead of using a number, we can use an identifier:
 +
 +<code asm>
 +.equ 
 +
 +
 +</code>
 +
 +
 +**Use GPIO as input**\\
 +
 +**Use GPIO as input with pull-up**\\
 +
 +
 +==== Reading analogue values ====
 +Reading of the analogue values is not so straightforward as in the case of binary ones. 
 +Built-in ADC converter uses 10-bit resolution, has 6 channels (A0-A5, respectively). It also uses a reference voltage (configurable), typically 5V.\\
 +The low-level ADC register-based operations use the following formula to obtain an ADC value (figure {{ref>avreq1}}, based on the input value ''Vgpio'' and the reference value ''Vref'').
 +
 +<figure avreq1>
 +{{:en:multiasm:exercisesbook:screenshot_from_2026-03-26_22-41-59.png?200|}}
 +<caption>ADC value calculation based on the input voltage and reference voltage</caption>
 +</figure>
 +
 +Analogue reading uses a complex setup of ADC-related registers as presented in table {{ref>tabadcregisters}}:
 +
 +<table tabadcregisters>
 +<caption>ADC-related registers used for reading the analogue values of GPIOs</caption>
 +^ Register   ^ Description                    ^
 +| ''ADMUX''  | Selects voltage reference and  |
 +|            |                                |
 +|            |                                |
 +|            |                                |
 +</table>
  
CC Attribution-Share Alike 4.0 International
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