Level Up Your Arduino Code: Registers

  Переглядів 180,872

SparkFun Electronics

SparkFun Electronics

6 років тому

Before watching this episode, you’ll want to be familiar with digital logic, which you can read about here: learn.sparkfun.com/tutorials/...
In this slightly-more-advanced edition of Adventures in Science, we dive into the inner workings of Arduino and microcontrollers. In order to understand how to set up interrupts on microcontrollers, we first need to cover the basics of interacting with registers. We take a simple example--turn an LED on whenever a button is pressed--and rewrite the program to manually read and write to registers on the ATmega328p.

КОМЕНТАРІ: 184
@sparkfun
@sparkfun 6 років тому
The next episode is up! Learn about external interrupts in Arduino: ukposts.info/have/v-deo/gmZik4aCsI2b2Yk.html
@jerryocrow1
@jerryocrow1 5 років тому
Your visual props and paper, clear voice and audio, pacing, sequencing, spiriling, and script are SO HELPFUL. You are a really good teacher. Whereever you work, you deserve a raise, promotion, and an assistant. Go get 'em tiger!
@S7udio1381
@S7udio1381 5 років тому
This is a great reference. Actually this is the first reference I found is comprehensive enough for me to understand the registers. Great job!
@99codemister
@99codemister 3 роки тому
OOooof. This description is good. If you want great instructions, then watch Ben Eater's channel. There he designed an eight bit computer from scratch. He introduces each concept slowly and sometimes will make an intentional "mistake" along the way.
@richardsalmon2046
@richardsalmon2046 6 років тому
Awesome video. That was the best explanation I've seen on the matter.
@ShawnHymel
@ShawnHymel 6 років тому
Thank you! Trying to figure out how to explain registers proved to be quite difficult. Glad to hear it helped!
@kennycho1682
@kennycho1682 2 роки тому
It's amazing to see that what I'm learning in school actually relates to something really fascinating and useful. Well done!
@sto2779
@sto2779 3 роки тому
It's nice how you saved almost half the flash memory space, that's one nice perk of programming in a lower level language. It's just ridiculous how you did not mention literally that it will run much "faster" than Arduino "overhead". All in all, this is an excellent tutorial with zero confusion and explanation about making use of microcontroller registers using the bitwise operators.
@jawadtahmeed9854
@jawadtahmeed9854 5 років тому
Long before Arduino came, we MCU programmers had always been doing this. Took quite a while to learn all those bit banging, but it was worth the time spent. Anyway, good tutorial. :)
@nderezic
@nderezic 3 роки тому
This perfectly demonstrates the benefits of more abstract programming languages: they are more readable and less prone to bugs. It also demonstrates benefits of avoiding abstract languages in microcontrollers. Damn! Now I need to rewrite all my code 😉
@AhmadAsmndr
@AhmadAsmndr 4 роки тому
one of the most advanced Arduino tut I have ever seen. thank you a lot.
@jesusesquibel4181
@jesusesquibel4181 6 років тому
brilliant explanation. I look forward to seeing the follow-up video(s).
@valerienihsimsion1870
@valerienihsimsion1870 4 роки тому
This is the best explanatory bit math lesson i ever followed. Thanks very much Sir.
@luwang404
@luwang404 4 роки тому
feels like I'm enlightened! expecting more of this kind of videos! really really thankful to you!
@TroyFletcherKeyboards
@TroyFletcherKeyboards 4 роки тому
The 660 page monstrosity is what enthusiasts read and understood before meta languages and corporations made money making things more high level, easier to understand, and also slower and less efficient. Now you make videos explaining what a register is. Edit: Alright, I was kind of salty. This is a pretty good video that goes into depth and is quite understandable. Have a like.
@gabriellgardin
@gabriellgardin 5 років тому
The most wonderful thing I've seen in a long time.
@mxcollin95
@mxcollin95 5 років тому
Great video!!! Learned a ton from this video! I’ll definitely be downloading and saving this lesson! Thanks for explaining this. 👍
@sathvikswaminathan7933
@sathvikswaminathan7933 4 роки тому
@10:40 it is a better practice to manipulate the DDR register value by using the OR logic. For example, DDRD = DDRD | B00100000; would be a better practice because digital pins 0 and 1 aren't supposed to be meddled with as they're used for serial communication.
@BorisDessimond
@BorisDessimond 4 роки тому
GOD. That was so clearly put together. THANKS.
@bascelik91
@bascelik91 3 роки тому
This is awesome! Please more tutorials on this topic. If you can,please add some references to books or so, so beginners can catch up on this topic. 👌👌
@sharingmylittleinsight
@sharingmylittleinsight 2 роки тому
all stuff get simpler with good analogy, thanks a lot sir
@rikkardo9359
@rikkardo9359 4 роки тому
I have never ever seen such a positive feedback command section before. It really is an iconic video!
@bkriegsman2890
@bkriegsman2890 6 років тому
Nicely done, great example and pretty quick pace.
@ColinRichardson
@ColinRichardson 6 років тому
Another thing I found amazing, was the SPEED increase from doing multiple digital writes to being able to write multiple pins directly at once (as long as they are on the same port letter). It wasn't just "faster" it was Monumentally faster. When dealing with multiplexing Multiple 7 Segment LEDs, the "off segments went from having a ghost of the previous figures to being very sharp.
@ShawnHymel
@ShawnHymel 6 років тому
This is true. Manually working with registers saves you clock cycles in addition to program space.
@favesongslist
@favesongslist 3 роки тому
So good to have machine level programming being taught so well, hopefully this will help understanding of how micros work in reality rather than point and click abstraction it is for most programmers. Do you know of a good optimising compiler for Arduino, that can produce this level of optimised coding?
@petercorrea
@petercorrea Рік тому
This is the exact video I’ve been searching for to begin my bare metal journey. I wanna know all the tiny details in every nook and cranny.
@AlexanderBelikov
@AlexanderBelikov 3 роки тому
If only I could hit Like every time I'm watching this video without dropping my previous one... Thank you!!
@martingentieu6850
@martingentieu6850 Рік тому
As an engineering student, this video was interesting and helpful ! Thank you.
@randomrandom420
@randomrandom420 6 років тому
That's funny. I'm writing down in my notebook as when I was at school. Thanks, professor!
@redboxup
@redboxup 4 роки тому
very well explained, thank you for putting so much effort!!!
@domedom2227
@domedom2227 6 років тому
Awesome Video! Very cool Idea with the Containers to show how to set the bits
@turnerkirk1920
@turnerkirk1920 4 роки тому
This is an UNBELIEVABLY AWESOME TUTORIAL!!! thank you thank you thank you thank you!!!
@ricsanders69
@ricsanders69 2 роки тому
Brilliant...nothing against adafruit...but here is the beginning of you not having to depend on another library maker for code! As an an exercise you can look at the low-level libraries that are out there and it would look more like the code in the video...great for learning! :-D
@calfee62
@calfee62 4 роки тому
Profoundly useful information presented clearly. Thank you!
@workethicrecords5901
@workethicrecords5901 6 років тому
Can't wait for part 2!
@buildersmark
@buildersmark 6 років тому
Really good tutorial. As I'm new to all of this, it takes a bit to wrap around my head...
@beamertech1
@beamertech1 4 роки тому
Builder's Mark newbie here too. I watched and now I’m so confused I’m thinking I need to go back to the basics... wait. Where are the basics?
@MultsElMesco
@MultsElMesco 5 років тому
I finally understood registers. Great video!
@RyanNeufeld
@RyanNeufeld 6 років тому
This was fantastic! Thank you. I learned a lot.
@1DR31N
@1DR31N 3 роки тому
Awesome. A real expert who masters Arduino, and much more for sure.
@Abdelhamid_Helal
@Abdelhamid_Helal 3 роки тому
Great explaination !. but could you explain the analogwrite and analogread funcutions ?
@akawmv
@akawmv 2 роки тому
This is awesome! Thank you for the amazing tutorial.
@rubikon7648
@rubikon7648 4 роки тому
Excellent explanation i had met so far, thank you,
@chimpspecialist
@chimpspecialist 4 роки тому
I can't possibly imagine a better tutorial. Okay, I probably could, but you came pretty darn close!
@OzerBurucu
@OzerBurucu 3 роки тому
Thanks for explenation. I have a question: how can we do bitwise operation if the button pin is (let say) pin 12 (PB5), and ledPin is pin 12 (PB4). I mean in digitalRead part. Do we still have to define ledPin as output ? (Because of DDRB =001000, all other pins exept 5, are inputs. )
@moinshaikh6684
@moinshaikh6684 3 роки тому
Excellent tutorial! Thank you sir!
@SandyNMS
@SandyNMS 4 роки тому
good at reducing memory usage thanks for the tutorial
@phoperdox_sore
@phoperdox_sore 6 років тому
Never thought about that, thanks for making this this video!!!
@chibuzordesmond3937
@chibuzordesmond3937 Рік тому
I watch for two minutes and I try to like only to see I’ve already liked.
@sovatnaphon9384
@sovatnaphon9384 5 років тому
Thank you very much for your episode, but I am still having another trouble while trying to connect two sensor in one raspberry pi board. May you please help, especially changing the address!
@CAXRAMEDIA
@CAXRAMEDIA 3 роки тому
Useful video. We love it. Thanks SparkFun 🙏
@inferno6012
@inferno6012 4 роки тому
this is good like really really good. I NEED MORE
@JeremyHongelectronics
@JeremyHongelectronics 6 років тому
Excellent video Shawn! Definitely was a good refresher for me. :)
@ShawnHymel
@ShawnHymel 6 років тому
Most welcome, and glad it helped!
@embeddedcastle
@embeddedcastle 4 роки тому
Very nice! You don't need to shift to right when reading digital input because any value different to zero is considered as TRUE.
@sashamuller9743
@sashamuller9743 4 роки тому
really good job with all your videos they a treat to watch.
@jude8876
@jude8876 3 роки тому
I LOVE THIS, THANK YOU SO MUCH!
@noealves8400
@noealves8400 6 років тому
Awesome video. great work guys.
@lycankarmah1976
@lycankarmah1976 4 роки тому
Very great explanation. Thank you
@sebastiannicolasgiles3659
@sebastiannicolasgiles3659 6 років тому
title should be: "level down your arduino code"
@SantuS021
@SantuS021 6 років тому
With subtitle: "Speed up your code to whole new level"
@ShawnHymel
@ShawnHymel 6 років тому
LOL that's fantastic. Love it!
@sebastiannicolasgiles3659
@sebastiannicolasgiles3659 6 років тому
you mean "old level" ahaha
@galihtanu
@galihtanu 3 роки тому
Level up i think ! Because much motlre dificult to trace and understanding.
@tybot6224
@tybot6224 3 роки тому
Haha you say old code but the Arduino IDE uses this method for programing the atmega32p board. PinMode() is one of the functions from the arduino IDE. This type of "old code" makes you arduino more efficient and accurate.
@user-px2nq6rk9i
@user-px2nq6rk9i 6 років тому
Thank you !! Thats what i am looking for :)
@milindmohapatra2745
@milindmohapatra2745 5 років тому
Really helpful video. But I have a doubt. By manipulating registers, how can we generate pwm signals. Basically I am asking for the replacement of the "analogWrite()" function. Thank you for the help.
@alexdavidsantiago3955
@alexdavidsantiago3955 3 роки тому
The Arduino.h file has the bitRead, bitSet, bitClear, bitToggle, and bitWrite definitions that do all the bit shifting, we just enter the register and the bit
@Electrologia
@Electrologia Рік тому
really great video, thanks a lot!
@lucianomaia9460
@lucianomaia9460 Місяць тому
amazing content, very well explained
@martijnvangorp
@martijnvangorp 4 роки тому
Very good explanation. Goodjob
@bigbamtv8112
@bigbamtv8112 Рік тому
Clear teaching
@AbdulRauf-le1el
@AbdulRauf-le1el 4 роки тому
excellent lesson.
@taranagnew436
@taranagnew436 6 років тому
where do you get the arduino base in 8:16?
@hirahaisadakeliye
@hirahaisadakeliye 4 роки тому
Nice video, I had experienced ur course on arduino helped me learn a lot if possible please come with another course on register programming on Udemy thanks for this video
@taranagnew436
@taranagnew436 6 років тому
where can i find the pdf for it and can you put the mega register onto a arduino uno?
@nedvedsimao1253
@nedvedsimao1253 10 місяців тому
very nice video. great work thank you sir
@guardiangamer2695
@guardiangamer2695 Рік тому
It was very nicely done
@user-hd2iv8jr6n
@user-hd2iv8jr6n 2 роки тому
Great video and useful ...
@karllaun2427
@karllaun2427 6 років тому
Excellent!
@galihtanu
@galihtanu 3 роки тому
Bitwise is amazing. 👍
@estebanacevedorivillas1799
@estebanacevedorivillas1799 5 років тому
I use int sw = PIND & ( 1 > boton; for read a digital port, but if I need Read an analog port witch registers what need to do?
@TheExvitermini
@TheExvitermini Місяць тому
Hello Sir, would it be possible to write a variable (integer for example) to one of the Arduino registers and then reading the register from a PLC using modbus (through RS485 shield)? Or, is it possible to write a variable from Arduino directly into a PLC register using modbus?
@nurbekhalikulov8867
@nurbekhalikulov8867 5 років тому
AWESOME! Brilliant!
@galihtanu
@galihtanu 3 роки тому
Please upload more about data register.
@Ghost572
@Ghost572 3 роки тому
Doing it by registers also makes accessing those input pins a lot faster, fairly sure the number I saw was ~56uS for DigitalWrite and using that register method brings it down to 5uS. I will find the name of the video and put it on this comment.
@VaclavNemec70
@VaclavNemec70 4 роки тому
Can I write for example 0b10101010 instead of B10101010?
@gacherumburu9958
@gacherumburu9958 3 роки тому
Thanks for the info..👍
@rafaelmarroquin4429
@rafaelmarroquin4429 6 років тому
Really helpfull thnks for share.. best regards
@ShawnHymel
@ShawnHymel 6 років тому
Most welcome. Glad it helped!
@kifahzaidan4505
@kifahzaidan4505 Рік тому
Hi, Thanks a lot, its really greate effort and great knowledge. i need to do a project for a preset position linear actuator with a DC motor abd a stepper motor, with Arduino Mega or UNO but i am not able to write the code and the scheme, can you please help. Regards
@robertstrong172
@robertstrong172 6 років тому
Reminds me of the peek and poke in my old Vic 20
@jordanmusleh6305
@jordanmusleh6305 3 роки тому
I have a question how can I a 2 robot arm project and a motor vehicle project in one arduino uno is it possible to make it happen..?? I have been working on it for months and no use for it I don’t know if my codes is right or it won’t work ...please help me..!!
@testgearaddicts4607
@testgearaddicts4607 3 роки тому
YT channel 'Joop Brokking' posted this project in 2016. Arduino makes code "human readable", in that it makes sense to beginners. The Arduino IDE is great to get acquainted with embedded, I'm 100% stoked its out there, but to make it to the 'real world project level' you have to manipulate registers without the crazy 'Playground' overhead. If you want to be freed from GitHub, you will have to learn to read the data sheets yourself, and not expect others to do the work for you. Bottom line, an ATMega328 costs less than a buck ($1) and you can get 'Blinky' working for less than 1% of the available space using a better IDE.
@incxxxx
@incxxxx 4 роки тому
What for you wrote >>btn_pin in the first line of the loop at the end of the line: int btn= (PIND & (1 btn_pin; when we have: const int btn_pin=2 and int btn is already assigned: int btn= (PIND & (1
@shifter65
@shifter65 5 років тому
What are the little bins usually used for? Or were they made specifically for the purpose of illustrating bits? Can't think of anything that small needing a mini bin.
@meepk633
@meepk633 5 років тому
Pills.
@massimookissed1023
@massimookissed1023 4 роки тому
They are often sold as containers for SMD components, although not-removing the components from their tape reel in the first place is a better way of storing them.
@wajdibrahem7617
@wajdibrahem7617 3 роки тому
how can I write in RFM69HCW registers ?
@arthasronald2186
@arthasronald2186 2 роки тому
Why the register codes completely doesn't work on my Arduino mega 2560? Is that because of pin issues?
@shafiqalom8408
@shafiqalom8408 4 роки тому
Awesome...make video on PWM control and Serial communication in Arduino by using Registers(port manipulation)...Advance Thanks...
@Korystuwatch42
@Korystuwatch42 2 роки тому
brilliant
@elangblitar369
@elangblitar369 4 роки тому
are the code register worked in esp8266?
@asishreddy7729
@asishreddy7729 3 роки тому
I know this was a wonderful explanation. But I understood nothing. What am I missing and where do I learn it? I don't know how to interpret matter on data sheets and the terminology for starters.
@sumitthapa001
@sumitthapa001 4 роки тому
Can you share the data sheet
@jeffvoight6586
@jeffvoight6586 5 років тому
If you're trying to save space and execution time, just use PORTD = B0010000 & PORTD; Saves two instructions and 3 or 4 clock ticks.
@chrisw1462
@chrisw1462 5 років тому
@17:00, you spend a bit of time determining the value of a bit. The AND isolates the bit, but that's as far as you need to go. Since it also ensures every other bit is '0', all you need to do is test for non-zero (usually the second fastest test in a microprocessor). i.e. If The shift you use to test for equal to '1' is unnecessary. "HIGH" and "LOW" are typically macros for non-zero and zero, respectively. If that's how the Arduino library defines them, you woudn't even have to change your If statement.
@mmulisha1022
@mmulisha1022 Рік тому
What can be slightly confusing to new users, are when you are using IO pins that are not "inline" with the port Pins. Example IO pins 8 thru 13 are PINB 0 thru 5. So instead of defining the Variable as the IO pin you would want to define them as the PINB pins... So for a button hooked to IO pin 8 you would want variable as such const int bnt_pin = 0; Not const int bnt_pin = 8;
@TutunSadhukhan
@TutunSadhukhan 3 роки тому
fantastic
@RoyvanLierop
@RoyvanLierop 6 років тому
4m47s There are three possible states for a GPIO-pin, not two as told. A pin can be digital in, digital out or high impedance.
@ShawnHymel
@ShawnHymel 6 років тому
You are correct in that I did not mention high impedance mode, so thanks catching that. In AVR, high-Z is accomplished by setting the corresponding bits for DDRx and PORTx to 0, which is the same as setting the pin to input with no pull-up. So, while tri-state does exist for AVR, it's also considered an input mode. See section 1.3 in this excerpt from an AVR Primer book: bit.ly/2r2zZa1
@eee_bangla
@eee_bangla 2 роки тому
can you give me the souce code of avr .
@leggomypotato1484
@leggomypotato1484 3 роки тому
Can someone explain why he is bit "shifting"? He says it's to retain changes made by other things while still changing the bit you want, but I can't comprehend how bit shifting accomplishes this for any case other than this specific function, which is better accomplished by just writing the bit... It sounds like a very good way to accidentally put 1's and 0's where they don't belong and disrupt other functions. WHY? You can just 'OR' it with B00000010
Adventures in Science: Plastic Ant Combat Bot Build
5:43
SparkFun Electronics
Переглядів 22 тис.
Level Up Your Arduino Code: Timer Interrupts
17:22
SparkFun Electronics
Переглядів 225 тис.
когда одна дома // EVA mash
00:51
EVA mash
Переглядів 5 млн
Optimizing Arduino Code: no setup(), no loop() ⛔
9:27
Wokwi
Переглядів 188 тис.
EEPROM Memory - Store Anything - Arduino101
13:16
Electronoobs
Переглядів 95 тис.
Can you solve this Oxford admissions question?
8:18
MindYourDecisions
Переглядів 25 тис.
Adventures in Science: LIDAR
6:59
SparkFun Electronics
Переглядів 133 тис.
Big Tech AI Is A Lie
16:56
Tina Huang
Переглядів 47 тис.
✔ Multitask Arduino with State Machines (& Switch Debouncing)
30:42
Normal Universe - Chris Guichet
Переглядів 67 тис.
Bare-Metal MCU #1 - Intro to registers
13:10
Mitch Davis
Переглядів 64 тис.
Running a Buffer Overflow Attack - Computerphile
17:30
Computerphile
Переглядів 2 млн
Adventures in Science: How to Use Rotary Encoders
12:05
SparkFun Electronics
Переглядів 220 тис.
I KNEW I’d Hate this (I Was Wrong)
12:19
Linus Tech Tips
Переглядів 617 тис.
Samsung or iPhone
0:19
rishton_vines😇
Переглядів 153 тис.
I wish I knew this When Istarted Programming #school #software  #codingtips
0:34
Claude Ams - Programming Guru 💻
Переглядів 7 млн
Broken Flex Repair #technology #mobilerepair
0:55
ideal institute aligarh
Переглядів 15 млн
Компьютер подписчику
0:40
Miracle
Переглядів 193 тис.