This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:multiasm:exercisesbook:arduinouno [2026/03/26 23:42] – [Reading analogue values] pczekalski | en:multiasm:exercisesbook:arduinouno [2026/03/26 23:42] (current) – [Reading analogue values] 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> | ||
| + | |||
| + | <figure arduinouno> | ||
| + | {{: | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | ===== 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 " | ||
| + | |||
| + | On the programming level, GPIO ports are grouped into 3 " | ||
| + | * 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> | ||
| + | {{: | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ===== 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> | ||
| + | < | ||
| + | |||
| + | <table assemblergpioinstructions> | ||
| + | < | ||
| + | ^ Instruction | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | </ | ||
| + | |||
| + | A common scenario is to first set either the GPIO is input or output (using the correct DDRx register), then either set ('' | ||
| + | <note tip>'' | ||
| + | |||
| + | ==== Examples ==== | ||
| + | Below are sections representing common usage scenarios: | ||
| + | |||
| + | ==== 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), | ||
| + | The low-level ADC register-based operations use the following formula to obtain an ADC value (figure {{ref> | ||
| + | |||
| + | <figure avreq1> | ||
| + | {{: | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | Analogue reading uses a complex setup of ADC-related registers as presented in table {{ref> | ||
| + | |||
| + | <table tabadcregisters> | ||
| + | < | ||
| + | ^ Register | ||
| + | | '' | ||
| + | | | | | ||
| + | | | | | ||
| + | | | | | ||
| + | </ | ||