en:multiasm:piot:chapter_4_9
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:multiasm:piot:chapter_4_9 [2024/09/27 23:22] – created pczekalski | en:multiasm:piot:chapter_4_9 [2026/01/20 12:59] (current) – [Examples] ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ======3 | + | ====== 3 Levels |
| + | 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 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 provides functions for I/O, memory management, and mathematical operations. Using libraries enables rapid application development without writing code from scratch. Libraries are particularly useful in projects that require frequent use of standard functions. | ||
| + | * **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' | ||
| + | |||
| + | |||
| + | |||
| + | 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); | ||
| + | } | ||
| + | </ | ||
en/multiasm/piot/chapter_4_9.1727468564.txt.gz · Last modified: by pczekalski
