Optimize Your Arduino Code with Registers

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 เม.ย. 2021
  • 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
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 44

  • @gordmills1983
    @gordmills1983 3 หลายเดือนก่อน +7

    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.

  • @shunnoysarkar6906
    @shunnoysarkar6906 27 วันที่ผ่านมา

    Hey thanks a tonne Ben! Also thank you for citing Spark Fun's video on registers!

  • @NTG671
    @NTG671 3 ปีที่แล้ว +2

    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.

  • @dadik7466
    @dadik7466 3 ปีที่แล้ว +6

    came from reddit, great video!

  • @esunisen3862
    @esunisen3862 6 หลายเดือนก่อน +3

    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

  • @dw6528
    @dw6528 5 หลายเดือนก่อน

    Very nicely done!!!! Sincere thanks!

  • @omkarkhade4602
    @omkarkhade4602 3 ปีที่แล้ว +1

    Great tutorial, Thanks.

  • @0124akash
    @0124akash 5 หลายเดือนก่อน +2

    Knowledgeable information, sir how to convert delay code into millis function?

  • @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 3 หลายเดือนก่อน

      Don’t forget if you’re not using all the ports, you can use them to hold 8bit values too…

    • @gordmills1983
      @gordmills1983 3 หลายเดือนก่อน

      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.

  • @joshuavincent7884
    @joshuavincent7884 3 ปีที่แล้ว +1

    Great video

  • @eat-myshorts
    @eat-myshorts 6 หลายเดือนก่อน

    can I use millis function with this optimization using arduino registers?

  • @Johann75
    @Johann75 2 ปีที่แล้ว +4

    Why not create a translator that will translate into registers? Wandering why Arduino IDE does not do that translation…

  • @nobodyeverybody8437
    @nobodyeverybody8437 วันที่ผ่านมา

    how to learn about the registers and keywords?

  • @IamSJ
    @IamSJ 2 ปีที่แล้ว +1

    they should make macros using the constexpr that would be clean

  • @jeevanshrestha8888
    @jeevanshrestha8888 ปีที่แล้ว +3

    wow nice tutorial...

  • @BrainTrance
    @BrainTrance 3 ปีที่แล้ว +2

    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 ปีที่แล้ว +4

      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.

  • @brucemangy
    @brucemangy 3 ปีที่แล้ว +11

    I strongly believe that the compiler should do that for us :( i'm disappointed.

    • @fauxpasiii
      @fauxpasiii 3 ปีที่แล้ว +7

      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 ปีที่แล้ว +1

      @@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/

  • @samarmany6272
    @samarmany6272 ปีที่แล้ว +2

    Where is position of accumulator memory in arduino like as microcontroller AT89S51??? how to acces and save??

    • @BenFinio
      @BenFinio  11 หลายเดือนก่อน +1

      I'm not sure, you'd have to check the microcontroller's datasheet.

  • @S0K0N0MI
    @S0K0N0MI 3 ปีที่แล้ว +14

    I feel it shouldn't be necessary to bang on registers manually; That sort of thing should be resolved by the compiler.

    • @gordmills1983
      @gordmills1983 3 หลายเดือนก่อน

      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….

  • @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: th-cam.com/play/PLKL6KBeCnI3X7cb1sznYuyScUesOxS-kL.html

  • @osamarabee3927
    @osamarabee3927 27 วันที่ผ่านมา

    some codes dosnot work with this methos i transfered a oled and dht sensor to this methos and it was impossible to work ???????????

  • @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?

  • @pocopoco3468
    @pocopoco3468 ปีที่แล้ว +1

    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  11 หลายเดือนก่อน +1

      ChatGPT makes things up sometimes! th-cam.com/video/3Xh9aNfOgfE/w-d-xo.html

    • @vodkaboi4974
      @vodkaboi4974 9 หลายเดือนก่อน

      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 6 หลายเดือนก่อน

      @@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.

  • @nonaak
    @nonaak 2 ปีที่แล้ว

    and how to do IF THEN ELSE in register code. great fid you make. thanks

    • @BenFinio
      @BenFinio  2 ปีที่แล้ว +3

      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 ปีที่แล้ว +1

      @@BenFinio ok. Thanks for you anser 👍

    • @TheFauriGames
      @TheFauriGames ปีที่แล้ว +2

      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 9 หลายเดือนก่อน +1

      @@nonaak you can use logic gates but if else is already kinda a logic gate