Optimize Your Arduino Code with Registers

  Рет қаралды 29,839

Ben Finio

Ben Finio

Күн бұрын

Пікірлер: 48
@NTG671
@NTG671 3 жыл бұрын
Didn't realize registers were a thing. You definitely inspired some new thought today. Good work!
@BenFinio
@BenFinio 3 жыл бұрын
Thanks!
@VndNvwYvvSvv
@VndNvwYvvSvv 2 жыл бұрын
Is recommend some basic computer hardware classes so you can see how gate logic makes up flipflops, registers, adders, buses, etc.
@dadik7466
@dadik7466 3 жыл бұрын
came from reddit, great video!
@BenFinio
@BenFinio 3 жыл бұрын
Thanks!
@shunnoysarkar6906
@shunnoysarkar6906 6 ай бұрын
Hey thanks a tonne Ben! Also thank you for citing Spark Fun's video on registers!
@gordmills1983
@gordmills1983 9 ай бұрын
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.
@esunisen3862
@esunisen3862 11 ай бұрын
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
@ezion67
@ezion67 2 жыл бұрын
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 9 ай бұрын
Don’t forget if you’re not using all the ports, you can use them to hold 8bit values too…
@gordmills1983
@gordmills1983 9 ай бұрын
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.
@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 9 ай бұрын
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….
@0124akash
@0124akash 10 ай бұрын
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.
@edgarbonet1
@edgarbonet1 5 ай бұрын
Do not write `0b00100000`: it is hard to read and error-prone. Write `_BV(PB5)` or `(1
@Johann75
@Johann75 2 жыл бұрын
Why not create a translator that will translate into registers? Wandering why Arduino IDE does not do that translation…
@dw6528
@dw6528 10 ай бұрын
Very nicely done!!!! Sincere 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.
@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/
@omkarkhade4602
@omkarkhade4602 3 жыл бұрын
Great tutorial, Thanks.
@IamSJ
@IamSJ 2 жыл бұрын
they should make macros using the constexpr that would be clean
@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
@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 Жыл бұрын
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 11 ай бұрын
@@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.
@jeevanshrestha8888
@jeevanshrestha8888 2 жыл бұрын
wow nice tutorial...
@joshuavincent7884
@joshuavincent7884 3 жыл бұрын
Great video
@nobodyeverybody8437
@nobodyeverybody8437 5 ай бұрын
how to learn about the registers and keywords?
@sagnost
@sagnost 4 ай бұрын
Read datasheets or search avr tutorials
@imamnegeri8825
@imamnegeri8825 3 жыл бұрын
Wow,its very helpful with complex project algorithm on arduino. Could you please share your documents about this? Thanks
@BenFinio
@BenFinio 3 жыл бұрын
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 Жыл бұрын
@@nonaak you can use logic gates but if else is already kinda a logic gate
@osamarabee3927
@osamarabee3927 6 ай бұрын
some codes dosnot work with this methos i transfered a oled and dht sensor to this methos and it was impossible to work ???????????
@yasirrakhurrafat1142
@yasirrakhurrafat1142 20 күн бұрын
How so? Maybe you have a library that doesn't recognise the low level code. You'd have to manually edit some of the code from the sketch.
@tubical71
@tubical71 5 ай бұрын
Hihi... And nowadays people do this using something like rust or py.... 😉
PING vs HC-SR04 Ultrasonic Distance Sensors for Arduino
7:03
Thank you Santa
00:13
Nadir Show
Рет қаралды 51 МЛН
Level Up Your Arduino Code: Registers
21:09
SparkFun Electronics
Рет қаралды 186 М.
Optimizing Arduino Code: no setup(), no loop() ⛔
9:27
Wokwi
Рет қаралды 210 М.
If you don’t learn sprintf(), your code will hate you later
8:34
Programming Electronics Academy
Рет қаралды 52 М.
EEPROM Memory - Store Anything - Arduino101
13:16
Electronoobs
Рет қаралды 110 М.
Advanced Arduino Code Optimisation with Pin Registers
15:16
Mario's Ideas
Рет қаралды 16 М.
How Fast Does Your Arduino Code Run? ⏱
19:18
Wokwi
Рет қаралды 26 М.
Level Up Your Arduino Code: External Interrupts
18:55
SparkFun Electronics
Рет қаралды 174 М.
PlatformIO: All you need to know in 10 Minutes!
10:56
J's e-shack
Рет қаралды 335 М.
How to Organize Code
14:37
Programming Electronics Academy
Рет қаралды 64 М.
How to Use Arduino Interrupts The Easy Way
33:28
Rachel De Barros
Рет қаралды 92 М.