Optimize Your Arduino Code with Registers

Поделиться
HTML-код
  • Опубликовано: 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
  • НаукаНаука

Комментарии • 46

  • @gordmills1983
    @gordmills1983 4 месяца назад +8

    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 7 месяцев назад +4

    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

  • @shunnoysarkar6906
    @shunnoysarkar6906 Месяц назад

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

  • @dadik7466
    @dadik7466 3 года назад +6

    came from reddit, great video!

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

  • @0124akash
    @0124akash 5 месяцев назад +2

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

  • @dw6528
    @dw6528 5 месяцев назад

    Very nicely done!!!! Sincere thanks!

  • @omkarkhade4602
    @omkarkhade4602 3 года назад +1

    Great tutorial, Thanks.

  • @eat-myshorts
    @eat-myshorts 7 месяцев назад

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

  • @joshuavincent7884
    @joshuavincent7884 3 года назад +1

    Great video

  • @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 4 месяца назад

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

    • @gordmills1983
      @gordmills1983 4 месяца назад

      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.

  • @nobodyeverybody8437
    @nobodyeverybody8437 22 дня назад

    how to learn about the registers and keywords?

  • @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 4 месяца назад

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

  • @IamSJ
    @IamSJ 2 года назад +1

    they should make macros using the constexpr that would be clean

  • @jeevanshrestha8888
    @jeevanshrestha8888 Год назад +3

    wow nice tutorial...

  • @Johann75
    @Johann75 2 года назад +4

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

  • @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: ruclips.net/p/PLKL6KBeCnI3X7cb1sznYuyScUesOxS-kL

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

  • @samarmany6272
    @samarmany6272 Год назад +2

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

    • @BenFinio
      @BenFinio  Год назад +1

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

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

  • @osamarabee3927
    @osamarabee3927 Месяц назад

    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  Год назад +1

      ChatGPT makes things up sometimes! ruclips.net/video/3Xh9aNfOgfE/видео.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 7 месяцев назад

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

  • @edgarbonet1
    @edgarbonet1 19 дней назад +1

    Do not write `0b00100000`: it is hard to read and error-prone. Write `_BV(PB5)` or `(1

  • @tubical71
    @tubical71 16 дней назад

    Hihi... And nowadays people do this using something like rust or py.... 😉