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 [2025/05/31 09:53] – marcin | en:multiasm:piot:chapter_4_9 [2026/01/20 12:59] (current) – [Examples] ktokarz | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| Programming AVR microcontrollers can be divided into three levels: C++, libraries, and assembler. Each of these levels offers distinct benefits and is utilized according to the project' | Programming AVR microcontrollers can be divided into three levels: C++, libraries, and assembler. Each of these levels offers distinct benefits and is utilized according to the project' | ||
| - | * **C++** is a high-level language that allows programmers to write code in a more abstract and understandable way. Using C++ for AVR enables | + | * **C++** is a high-level language that allows programmers to write code in a more abstract and understandable way. Using C++ for AVR enables advanced features such as object-oriented programming, |
| - | * **Libraries** are sets of predefined functions and procedures that facilitate programming AVR microcontrollers. An example is the AVR Libc library, which offers | + | * **Libraries** are sets of predefined functions and procedures that facilitate programming AVR microcontrollers. An example is the AVR Libc library, which provides |
| - | * **Assembler** is a low-level language that allows direct programming of the AVR microcontroller. Writing code in assembler gives full control over the hardware and allows for performance optimization. However, programming in assembler requires a deep understanding of the microcontroller' | + | * **Assembler** is a low-level language that allows direct programming of the AVR microcontroller. Writing code in assembler gives full control over the hardware and allows for performance optimization. However, programming in assembler requires a deep understanding of the microcontroller' |
| - | 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 | ||
| + | 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); | ||
| + | } | ||
| + | </ | ||