No video

Optimize Your Arduino Code with Registers

  Рет қаралды 27,664

Ben Finio

Ben Finio

Күн бұрын

How do you shrink your Arduino code to make it smaller and faster? With registers! This video explains why you would ever want to write --. To learn how to program your Arduino with registers, check out my Tinkercad Circuits + Arduino playlist: • Tinkercad Circuits + A... . Also check out this great video by Shawn Hymel: • Level Up Your Arduino ...
For some interesting discussion and debate about the merits of doing this, see the following Reddit threads:
/ optimize_your_arduino_...
/ arduino_libraries_vs_d...
/ arduino_libraries_vs_d...
---------------------------------------------
/ benfinio

Пікірлер: 47
@gordmills1983
@gordmills1983 6 ай бұрын
Now write it in asm. For example, I wrote code that does exactly the same functions as multiple libraries, spi negotiation, i2c communication, in asm, and it compiles to just over 1.5kb…. The original code is over 24kb… there again I’m an old fart who started when memory was premium… you had to be efficient.
@NTG671
@NTG671 3 жыл бұрын
Didn't realize registers were a thing. You definitely inspired some new thought today. Good work!
@BenFinio
@BenFinio 3 жыл бұрын
Thanks!
@VeritasEtAequitas
@VeritasEtAequitas Жыл бұрын
Is recommend some basic computer hardware classes so you can see how gate logic makes up flipflops, registers, adders, buses, etc.
@esunisen3862
@esunisen3862 8 ай бұрын
Reminds me the good ol' time when i had to generate a 50 Hz sinewave signal with a 68705P3 and a DAC. I had to track every microsecond to have the right frequency. You kids have no idea how easier it is nowadays xD
@dadik7466
@dadik7466 3 жыл бұрын
came from reddit, great video!
@BenFinio
@BenFinio 3 жыл бұрын
Thanks!
@shunnoysarkar6906
@shunnoysarkar6906 3 ай бұрын
Hey thanks a tonne Ben! Also thank you for citing Spark Fun's video on registers!
@S0K0N0MI
@S0K0N0MI 3 жыл бұрын
I feel it shouldn't be necessary to bang on registers manually; That sort of thing should be resolved by the compiler.
@gordmills1983
@gordmills1983 6 ай бұрын
That is the problem, you are relying on a program to optimise a program. If the compiler is not efficient, now will the result be. No substitute for hand crafted assembly routines… smaller , faster, and if your nerdy and need to compute cycles, you can easily count the clock cycles via the cycles each op code needs to process. Great for tight timing when you want to run @8mhz and dump data to addressable leds….
@edgarbonet1
@edgarbonet1 2 ай бұрын
Do not write `0b00100000`: it is hard to read and error-prone. Write `_BV(PB5)` or `(1
@ezion67
@ezion67 Жыл бұрын
AVR is one of very few processor families specifically designed to be programed in assembly. Doing so is not much harder than using a higher level language. If speed and fine control over register use is what you after, assembly might be worth looking in to. For example: You may give a routine (function) exclusive use of some of the 32 general purpose registers instead of using variables in memory, reducing the need to push and pull from stack and heap. This can give significant performance gains. Frequently called interrupts are a good target for this technique.
@gordmills1983
@gordmills1983 6 ай бұрын
Don’t forget if you’re not using all the ports, you can use them to hold 8bit values too…
@gordmills1983
@gordmills1983 6 ай бұрын
Push and pop from the stack do use several clock cycles, so minimise if you need precise timing.. also the data sheets have a nice section on the opcodes , including clock cycles to execute.
@brucemangy
@brucemangy 3 жыл бұрын
I strongly believe that the compiler should do that for us :( i'm disappointed.
@fauxpasiii
@fauxpasiii 3 жыл бұрын
Came here to post basically this. Especially for a platform that will be a lot of people's introduction to this type of programming, training them to try to outsmart their compiler is going to cause problems further down the line. Not taking away anything from this very useful and informative video though! I found it illuminating.
@BenFinio
@BenFinio 3 жыл бұрын
@@fauxpasiii @brucemangy there is a lot of good discussion in the reddit thread that you may be interested in: www.reddit.com/r/arduino/comments/mnfxwl/optimize_your_arduino_code_with_registers/
@0124akash
@0124akash 7 ай бұрын
Knowledgeable information, sir how to convert delay code into millis function?
@BrainTrance
@BrainTrance 3 жыл бұрын
Two questions: 1.Is the faster/efficient code written in C? is that the reason why delay() converted into _delay_ms()? 2. Isn't it faster to bit operate the PORTB rather than reassign it a new value?
@BenFinio
@BenFinio 3 жыл бұрын
1. The code on the right is written entirely in C, but the Arduino IDE will let you mix and match, so you could write delay() instead. (disclaimer: I realize there is debate about what exactly the "Arduino language" is and I do not have a strong opinion on what to call it: hackaday.com/2015/07/28/embed-with-elliot-there-is-no-arduino-language/) 2. Yes, someone also just pointed this out on Reddit. I find that students are confused by masking and bitwise operators at first so that needs to be introduced as a separate topic. So we usually do this first, then do something like try to blink two LEDs out of sync, and realize that "whoops, we're overwriting all the pins because we're writing to the whole register" and then use that as motivation to introduce masking. It's about halfway through the list of Tinkercad tutorials linked in the description.
@Johann75
@Johann75 2 жыл бұрын
Why not create a translator that will translate into registers? Wandering why Arduino IDE does not do that translation…
@dw6528
@dw6528 7 ай бұрын
Very nicely done!!!! Sincere thanks!
@omkarkhade4602
@omkarkhade4602 3 жыл бұрын
Great tutorial, Thanks.
@samarmany6272
@samarmany6272 Жыл бұрын
Where is position of accumulator memory in arduino like as microcontroller AT89S51??? how to acces and save??
@BenFinio
@BenFinio Жыл бұрын
I'm not sure, you'd have to check the microcontroller's datasheet.
@IamSJ
@IamSJ 2 жыл бұрын
they should make macros using the constexpr that would be clean
@jeevanshrestha8888
@jeevanshrestha8888 Жыл бұрын
wow nice tutorial...
@joshuavincent7884
@joshuavincent7884 3 жыл бұрын
Great video
@pocopoco3468
@pocopoco3468 Жыл бұрын
Chat gpt said that we can store out libraries and parts of the program(Sketch) in SD card, I didn't find any video tutorial on this, how its done?
@BenFinio
@BenFinio Жыл бұрын
ChatGPT makes things up sometimes! kzbin.info/www/bejne/aYnLapSEm7SanKc
@vodkaboi4974
@vodkaboi4974 11 ай бұрын
you can store data inside an sd card but you'd need to learn how to make files and write in them in c++, thanfully it's not complicated and there are some good tutorial on youtube. Edit : this is limited to variables, i doubt you can store programme as it's not the same type of memory.
@esunisen3862
@esunisen3862 8 ай бұрын
@@vodkaboi4974 Well, technically if you can read/write directly the sectors without any filesystem, this is considered as virtual memory, sort of. The thing is it's MUCH slower. You could execute some code if it was first copied in RAM, that's how MBR worked in DOS.
@eat-myshorts
@eat-myshorts 9 ай бұрын
can I use millis function with this optimization using arduino registers?
@TericT
@TericT 3 жыл бұрын
Any tips on finding the documentation for such registers? I always find that to be incredibly difficult.
@BenFinio
@BenFinio 3 жыл бұрын
The ATMega328P datasheet has the full documentation. It's hundreds of pages long and a bit overwhelming at first. My Tinkercad Circuits tutorial series introduces things one step at a time, you may find that useful: kzbin.info/aero/PLKL6KBeCnI3X7cb1sznYuyScUesOxS-kL
@nobodyeverybody8437
@nobodyeverybody8437 2 ай бұрын
how to learn about the registers and keywords?
@sagnost
@sagnost Ай бұрын
Read datasheets or search avr tutorials
@osamarabee3927
@osamarabee3927 3 ай бұрын
some codes dosnot work with this methos i transfered a oled and dht sensor to this methos and it was impossible to work ???????????
@tubical71
@tubical71 2 ай бұрын
Hihi... And nowadays people do this using something like rust or py.... 😉
@imamnegeri8825
@imamnegeri8825 2 жыл бұрын
Wow,its very helpful with complex project algorithm on arduino. Could you please share your documents about this? Thanks
@BenFinio
@BenFinio 2 жыл бұрын
Hi - not sure what documents you're referring to?
@nonaak
@nonaak 2 жыл бұрын
and how to do IF THEN ELSE in register code. great fid you make. thanks
@BenFinio
@BenFinio 2 жыл бұрын
Hi - the syntax for if/then statements is the same in both C and the Arduino language, you can't replace it with registers (as far as I know).
@nonaak
@nonaak 2 жыл бұрын
@@BenFinio ok. Thanks for you anser 👍
@TheFauriGames
@TheFauriGames Жыл бұрын
Most of the time, registers on embedded systems are used for peripheral configurations (pins, timers, UART, ...), thats the things surrounding the CPU and both peripherals and CPU makes the µC. So depending on the bits stored on that register, the behavior of the peripheral will vary. On the other hand, instructions like if/then/else are directly involved in the ALU of the CPU. And most of the time standart libraries in C should handle the job.
@vodkaboi4974
@vodkaboi4974 11 ай бұрын
@@nonaak you can use logic gates but if else is already kinda a logic gate
PING vs HC-SR04 Ultrasonic Distance Sensors for Arduino
7:03
Advanced Arduino Code Optimisation with Pin Registers
15:16
Mario's Ideas
Рет қаралды 15 М.
Kind Waiter's Gesture to Homeless Boy #shorts
00:32
I migliori trucchetti di Fabiosa
Рет қаралды 15 МЛН
OMG what happened??😳 filaretiki family✨ #social
01:00
Filaretiki
Рет қаралды 13 МЛН
а ты любишь париться?
00:41
KATYA KLON LIFE
Рет қаралды 3,5 МЛН
Optimizing Arduino Code: no setup(), no loop() ⛔
9:27
Wokwi
Рет қаралды 201 М.
The World's Tallest Pythagoras Cup-Does It Still Drain?
10:05
The Action Lab
Рет қаралды 355 М.
Level Up Your Arduino Code: Registers
21:09
SparkFun Electronics
Рет қаралды 184 М.
Bare metal GPIO driver for ESP32 | Toggling an LED
12:43
The EV Engineer
Рет қаралды 7 М.
Port Register Control | Increase speed of Read/Write - Arduino101
15:22
Kind Waiter's Gesture to Homeless Boy #shorts
00:32
I migliori trucchetti di Fabiosa
Рет қаралды 15 МЛН