This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:multiasm:piot:chapter_4_9 [2026/02/06 16:33] – raivo.sell | en:multiasm:piot:chapter_4_9 [2026/02/27 01:13] (current) – [Examples] jtokarz | ||
|---|---|---|---|
| Line 8: | Line 8: | ||
| The choice of programming level depends on the project' | The choice of programming level depends on the project' | ||
| + | |||
| + | |||
| + | Some AVR assembly examples for the Arduino Uno (ATmega328P), | ||
| + | |||
| + | ===== Examples ===== | ||
| + | |||
| + | **Blink inbuilt LED on pin 13 (port PB5) using assembly** | ||
| + | <code c> | ||
| + | void setup() { | ||
| + | // Set PB5 (pin 13) as output | ||
| + | // if DDRB (Data Direction Register B) = 1, PORTB is output | ||
| + | asm volatile(" | ||
| + | } | ||
| + | void loop() { | ||
| + | asm volatile(" | ||
| + | delay(500); | ||
| + | asm volatile(" | ||
| + | delay(500); | ||
| + | } | ||
| + | </ | ||
| + | For Arduino Uno (ATmega328P): | ||
| + | * DDRB has address 0x04 | ||
| + | * PORTB has address 0x05 | ||
| + | |||
| + | ---- | ||
| + | |||
| + | |||
| + | **Add two numbers using AVR registers** | ||
| + | <code c> | ||
| + | void setup() { | ||
| + | Serial.begin(9600); | ||
| + | uint8_t a = 7, b = 9, result; | ||
| + | asm volatile( | ||
| + | "mov r24, %1\n" | ||
| + | "mov r25, %2\n" | ||
| + | "add r24, r25\n" | ||
| + | "mov %0, r24\n" | ||
| + | : " | ||
| + | : " | ||
| + | : " | ||
| + | ); | ||
| + | Serial.println(result); | ||
| + | } | ||
| + | void loop() {} | ||
| + | </ | ||
| + | ---- | ||
| + | |||
| + | **Read digital input (PD2, bit 2 on PORTD)** | ||
| + | <code c> | ||
| + | void setup() { | ||
| + | Serial.begin(9600); | ||
| + | asm volatile(" | ||
| + | } | ||
| + | void loop() { | ||
| + | uint8_t value; | ||
| + | asm volatile( | ||
| + | "in %0, 0x09" | ||
| + | : " | ||
| + | ); | ||
| + | Serial.println(value & (1<< | ||
| + | delay(200); | ||
| + | } | ||
| + | </ | ||