Hello everyone, I'm trying my first contact with the Arduino IDE now, it's just the first few weeks. In the past I've done some things in assembly and I'm more familiar with that language.
I believe that other colleagues also on the forum have also taken this path.
I would like to know how I can join the Arduino IDE language with the Assembly and some very simple things.
As an example I am familiar with terminal PB5 which is number 13 on the Arduino. I wish that instead of writing
You'll see in 5 the Serial monitor , so digitalRead(PB5); would actually read pin D5 (PD5)
When you write a program, depending on the existence of PB5 would make the code less portable to other architectures, so the Arduino team created a mapping between the real pins and some pin names like A0 for the first analog input port
Based on which Arduino you use, the mapping will be different. When you select a board in the IDE, it selects the associated variant for the pins mapping.
if you want to use the Arduino IDE, best is to adopt the conventions they set rather than trying to bring along the other way of doing things. That will make your life easier if you move to an ESP32 later on for example
This is a Read-Modify-Write operation that takes more than one machine cycle on the AVR processor. If a interrupt occurs immediately following the Read operation and the ISR writes to PORTB, then the subsequent Write operation will undo what happened in the ISR.
that's true before the optimizer does its job and notices the const part and the fact that it fits on one byte as expected by pinMode for example and so will be replaced directly in assembly
with #define β 924 bytes in flash, 9 bytes for global variables
with const uint8_t β 924 bytes in flash, 9 bytes for global variables
==> exactly the same
note that a define for a number will be treated as an int (16 or 32 bits) whereas a const uint8_t will occupy 1 byte. It can make a difference in some maths
The type depends on the usage, int by default. No variable is allocated, no memory reserved.
With variable declarations the compiler is free to allocate less or no memory under circumstances. I already observed no difference in memory usage with a variable declared as int or byte.
From this, we might jump to the conclusion you're using a Nano/Uno, but we could be very wrong. You'll find on this forum life gets easier if you specify up front, in your first post, which processor, exactly, you want to work with/ask questions about; it can considerably reduce the resultant thread-thrash of irrelevant-if-only-we-knew postings.
PB5 is NOT pin 13 on an Arduino. It's just "5." Your code has to know "elsewhere" that it is part of PORTB, and that it's a bit number rather than some other meaning of "5"... That happens automatically if you do assembly language instructions like sbi PORTB, PB5, and the compiler will do it for you if you say PORTB |= 1<<PB5; (don't get me started on giving "5" multiple names that just mean 5!)
Some "board" packages define PIN_PB5 and etc for using typical ASM names with the Arduino functions. "minicore" does this for the ATmega328p used on Uno and Nano.
Note that using this makes your code very chip-dependent. Not ALL arduinos haave PB5 on pin 13. In general, the Arduino pin numbering is more portable if you're dealing with board-level products, and the asm approach for chip-level products.
digitalWrite(PIN_PB5, 1); is MUCH slower than PORTB |= 1<<PB5;
Note that code does not handle PORTB atomically. Thus it is not interrupt-safe.
On most AVRs (and on the ATmega328p in particular), PORTB |= 1<<5 compiles to a single atomic instruction. Not PORTB |= (1<<5)+(1<<4), though. Nor PORTL |= 1<<5; (on ATmega2560)! heh heh.