Bare-Metal MCU #1 - Intro to registers

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 พ.ค. 2024
  • This is the first video in a journey from Arduino to STM8. The goal is to start with Arduino, which is a popular starting point. I'll then break it down into a fundamental level, and then apply those fundamentals to other microcontrollers, such as the STM8.
    This video focuses on how to program an ATMEGA328 while avoiding the Arduino helper functions.
    ATMEGA328P Datasheet: ww1.microchip.com/downloads/en...
    ATMEGA328 Datasheet:
    ww1.microchip.com/downloads/en...

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

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

    Keep making videos bro. This is one of those rare videos on youtube where a dude explains everything in a "grounds-up". This manner of explanation leaves no holes in understanding and encouraged me to review your video over and over. Keep it up!!

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

    I like programming as close to the metal as possible. GOOD JOB showing this and illustrating why this is superior.

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

    I’ve been using a personal project as an opportunity to learn about Arduinos and the whole design process. Thought I had it all figured out until I tried to transition to the ATTiny85 and was quickly humbled.
    Your videos are providing the bridge to microcontrollers that I had no idea I needed and I cannot say enough how much I appreciate all of your work. It goes without saying that this step up in understanding is likely what stops most of us from going further with our projects so I can’t imagine how many doors you’ve opened for people. Thank you.

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

    This was really neat! Been looking for videos that explain how to do this kinda thing for a while now and this was by far the best one, can't wait to watch the rest!

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

    This is basically a full explanation that’s bridging the gap in my knowledge between circuits and computer programs. I want to let you know how valuable this is to me and, I’m sure, to other people. Thank you for making this.

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

    We loved this tutorial so much we featured it in this week's Electromaker show!

  • @prasadsalunke23
    @prasadsalunke23 9 หลายเดือนก่อน +2

    Criminally underrated channel this is ☝️🙂Keep up the good work ❤

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

    Saved the playlist. Your explanations are super crystal clear. Thanks

  • @rpr42
    @rpr42 3 ปีที่แล้ว

    I have just started watching this series and am enjoying it. I have been playing with the ESP32 using micropython mainly and have always been curious about digging in deeper. Great Job!

  • @raif10
    @raif10 11 หลายเดือนก่อน

    I've watched a ton of git manipulation/register videos lately and this is by FAR the best I have seen. Watch it and save it.

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

    Great presentation and in depth analysis of the necessary elements to understand bare metal programming. Been looking for something to jumpstart my understanding with avr bare metal programming. Even if you applied it for STM32, it's so concise it could be applied to any MCU with the proper study of the datasheet. Well done.

  • @AlexSKelly-up7lf
    @AlexSKelly-up7lf 3 ปีที่แล้ว +3

    Man, you're killing it.

  • @cthoadmin7458
    @cthoadmin7458 ปีที่แล้ว

    This is high quality stuff Mitch! Thanks for bringing it to us... (rushing away to try the blink example myself)

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

    this guy knows alot, thank you for this amazing information and please continue sharing it

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

    Thx Mitch! I was looking for some Ben Eater style videos about understanding microcontrollers. I think I've found it!

  • @AdilKhan-jp7hn
    @AdilKhan-jp7hn 3 ปีที่แล้ว

    Thank you for such detailed information on these avr microcontroller. Excellent content and video quality

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

    Great job, I am really enjoying this series

  • @Landline3772
    @Landline3772 2 หลายเดือนก่อน

    The best explanation I've ever seen , you are best!

  • @meemuboi
    @meemuboi 3 ปีที่แล้ว

    This was really easy to understand, well done and thank you for making these videos!

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

    I dont understand how this series has such low views... Those Videos taught me so much!
    Thanks for that dude!

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

      i think its coz no one will understand what the title of the video even means, i came here after watching his recent "beyond arduino" video. And thank god did i watch that video haha, and thank god that i came here :D

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

      The title is a bit obscure. Also SparkFun has a couple of videos on Atmega328 registers/ports that are quite good. Still he does a good job of explaining and chunking it and deserves all the best for his efforts.

  • @lokipuk
    @lokipuk 10 หลายเดือนก่อน

    You are SO the best of the best. Perfect explanations

  • @nyakoi
    @nyakoi ปีที่แล้ว

    Thank you, exactly what I needed. Very well explained!

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

    Great videos!!!
    Thanx for your time and effort!!!

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

    Thank you very much, Mitch, awesome, very informative videos!

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

    nice work bro. hats off to you.

  • @n-o-i-d
    @n-o-i-d 3 ปีที่แล้ว

    Thank you for this amazing content!

  • @brunosuperman
    @brunosuperman ปีที่แล้ว

    The best video about this on the internet sofar! You are really didactical! Thanks you!

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

    Hi Mitch, i just want to thank you for helping me out with this video. Well done! I am now able to write the right code for my project :-)

  • @phamngocthinh5913
    @phamngocthinh5913 ปีที่แล้ว

    i love this video, it's help me can understand a lot of the information behind the function already have in Arduino. Thanks for this video

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

    1:04 nice

  • @Fubar12341
    @Fubar12341 ปีที่แล้ว

    Brilliant video well explained and it works !

  • @sermuns
    @sermuns ปีที่แล้ว

    this is amazing! thank you!

  • @jameshall7539
    @jameshall7539 ปีที่แล้ว

    thanks for this tutorial it teaches people how to do it in the right way

  • @edinetgrunhed6000
    @edinetgrunhed6000 3 ปีที่แล้ว

    i like this kind of video its low level programming we can understand how mcu working under the hood and our ticket for understand mcu third partly library works

  • @aregjan
    @aregjan 4 หลายเดือนก่อน

    Wow, 18x faster with direct access ?? That's an enormous improvement, I shall do that going forward. These videos are really great .

  • @slowjocrow6451
    @slowjocrow6451 11 หลายเดือนก่อน

    Thanks for the video 👍 Trying to learn bare metal with no previous microcontroller experience

  • @oz3230
    @oz3230 3 ปีที่แล้ว

    Thanks a lot man! was looking for this :D

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

    Amazing video, thank you very much!!

  • @arduinomaquinas
    @arduinomaquinas ปีที่แล้ว

    Big show, good job, subscribed and like 1.6K full 😉👍

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

    Excellent content ie., Oscilloscope waves (high level vs low level port assessing ) 18x faster than high level
    Thank you

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

    12:37
    He wanted to make it a a BIT longer; however, could not because he wanted to keep it in a BYTE size format.
    So so so good, how to do you keep a straight face with golden tech puns like that............subscribed (not just for the pun, but it was icing on the cake)

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

      Pbbbttttt. That’s because I had no idea I did that until you just pointed it out

  • @Dygear
    @Dygear 6 หลายเดือนก่อน +1

    For the Port B segment, might be worth showing where Port B is defined. I can't seem to find it in the headers. I'm told from a Google Search that this is in the avr tool-chain. I would imagine that it's defined with the volatile keyword to tell the compiler that the reads or writes to this value will have side effects not seen by the C code directly and thus can not be optimized out.
    For the Delay segment, might be worth mentioning that the clock speed is the driver of how fast the system runs. A single instruction takes at least one CPU cycle to complete and is your overall system budget on how many instructions you can do in any given second. With that in mind, a write to a variable takes 1 instruction, a read to a variable that is in the CPU register takes 1 instruction. With the STM8 running at 8MHz it can process 8,000,000 instructions in a given second. The for loop itself is setup (long i = 0) at the setup of the loop and for the purposes of this discussion is meaningless. (1 / 8,000,000 of a second). The execution of the code takes 1 instruction (PORTB = 0), the check takes one instruction (i < 1_000_000), and the increment takes one instruction (i++). One iteration of the loop cycle actually takes 3 instructions, and so each for loop is costing 3,000,001 cycles. The delay it's causing is actually around 3/8th of a second or 375 milliseconds and that is why it's blinking faster than the previous example.

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

    This is great! I already learned this stuff in a course at the university a few years ago, but needed a refresher. I always felt that the arduino coding is making things dumber rather than easier. And every time i start working with arduino i feel like im relearning bad habits lol. The easy accessability and price of arduinos is great but why cant we all just learn to code like this?

  • @ajmalashraf7921
    @ajmalashraf7921 3 ปีที่แล้ว

    Thank you its so interesting ...

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

    This is realy realy good example, love it. Maybe I'll be able to do it now, 6 us static output loop. Thank you :-3

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

    awesome!

  • @mostafagaberahmed6657
    @mostafagaberahmed6657 3 ปีที่แล้ว

    thank you . this helps

  • @Swordhero111
    @Swordhero111 ปีที่แล้ว

    Perhaps the port version is only faster because you are setting it equal, but in digit write it is probably doing the |= operator to set and &= to unset the bit, thus requiring more instructions.

  • @zephsmith3499
    @zephsmith3499 3 ปีที่แล้ว

    I understand the niche of starting at the bottom level in understanding the ATMEGA chips and the Arduino environment - looking under the hood to explain registers, machine code, assembly, and C/C+. And learning to bypass the Arduino libraries at times. Kudos. There is value in that.
    But what is the reason for aiming to eventually transitioning to the STM8?
    I could see why someone aiming for large scale manufacturing might want to use the cheapest uC which can handle their task, and the ATMEGA line often isn't that. But for a hobbiest, what advantage is there to (eventually) transitioning to another low level uC with similar capabilities, using a different toolchain and ecosystem to learn?
    Consider by contrast, say, planning transitioning to the STM32 line, which has some advantages in handling some tasks which the ATMEGA cannot. One option in this case would be using the Arduino ecosystem retargeted for the STM32, so reducing the new learning curve once one is familiar with the ATMEGA (eg: using the Blue Pill devices).
    Anyway, good presentation. If you wish to share the reasoning behind the intended direction of this series, that would be cool.

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

      @@MitchDavis2 OK, good luck!
      It's good to see a different approach, starting from the low level and working up. Of course, the other approaches have their value too, but people differ in how they learn best, and in what they are trying to accomplish.
      I'm pretty familiar with the basics you are covering (my first microcomputer was an IMSAI 8800 and you could use front panel toggle switches to manually enter one byte at a time of machine code, for the boot code!). But I can well imagine that it would be very useful for some beginners.
      I'll try to check back later and see how this effort matures and what you do next.
      Kudos!

  • @zetaconvex1987
    @zetaconvex1987 3 ปีที่แล้ว

    Very nice. Looks like it will soon be time to ditch the IDE entirely and use something like avr-glibc.

  • @GingerNingerGames
    @GingerNingerGames ปีที่แล้ว

    I realise this is 2 years old now, but a good way to make a "Do nothing" loop is to run the following.
    __asm__ __volatile__ ("nop
    \t");
    That is an assembly line for no operation (nop) I can't remember what the
    \t does, but the bit where it says "volatile" means the compiler won't optimise it out. This is what I use for either fault states to entirely halt operation, or for stuff like this where you need it to do nothing while simultaneously keeping the compiler happy that you're actually doing something.
    This is also the only thing I remembered from my coding course in Uni, and I didn't even remember it, I just copy pasted it from one of my projects for uni.

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

    Your explanation and effort is very nice! Also the way you say "delay" is making me feel like you must be playing electric guitar.

  • @testme2026
    @testme2026 3 ปีที่แล้ว

    Great one thank you, one question why did you leave PORTB=32 and = 0 before the For loop ?

  • @isaacyuki1
    @isaacyuki1 7 หลายเดือนก่อน

    How does the microcontroller know, that DDRB as a variable is actually the register DDRB? Is this information somewhere stored?

  • @freddiesnijman
    @freddiesnijman 3 ปีที่แล้ว

    Thank you

  • @emma7694
    @emma7694 ปีที่แล้ว

    great!

  • @faizurrahman2399
    @faizurrahman2399 ปีที่แล้ว

    Correction:
    The hardware registers used by the Arduino Uno are located on-chip, separate from both RAM and ROM. These registers are used to control the behavior of the microcontroller, including setting pin directions, configuring timers and interrupts, and controlling various peripherals such as the analog-to-digital converter and serial communication ports.

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

    Do you have any examples for the new Arduino Uno R4? ;-)

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

    I always wondered what magic was going on in that digitalWrite function. turns out it's not so scary after all

  • @gustavocortico1681
    @gustavocortico1681 ปีที่แล้ว

    I feel like messing with bare metal is kind of an uncanny valley of physicality. Gives me a bit of a thrill.

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

    Also, any chance you could link the datasheet and other references in the description?

  • @2ftpmarco
    @2ftpmarco 6 หลายเดือนก่อน

    thanks !

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

    also a program that uses pinMode(), digitalWrite() and delay() takes 1536 bytes of flash and 9 bytes of RAM on my Mega2560, whereas a simple bitwise op function and _delay_ms() from AVR API takes only 304 bytes of flash and 0 bytes of RAM - meaning 5x times memory optimization. So yeah Arduino SDK functions are fine for prototyping, but optimizing for specific MCU pays off by a lot.

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

    Thanks for a very understandable video. Is there a way to enter the port value in binary? IE portb=00100000 instead of portb=32? It would be easier to program multi led blink patterns this way.

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

      In Arduino IDE, I’m pretty sure you can write binary if it starts with a capital B. Such as “int i = B11001010”

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

    Great video! However, the timing calculation in 6:20 is not correct since on left there is assignment-only, and on right it is read-and-assignment operation. These do different things. To make comparison correct, the |= and &= operators should be used on left side instead of the =.

  • @etleo3811
    @etleo3811 10 หลายเดือนก่อน

    ❤🙌🏾 🎉

  • @mustardthefirst1583
    @mustardthefirst1583 10 หลายเดือนก่อน

    thanks

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

    bro, u r smarter then my lecturer

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

    Great video. Thanks. Actually the sound of your loud typing made me realise how bad mine is! Must learn to type!

  • @bob-ny6kn
    @bob-ny6kn 2 ปีที่แล้ว

    Now, to read those one thousand pages of datasheet and see if I can find anything as useful as this...

  • @subhashkendole3005
    @subhashkendole3005 10 หลายเดือนก่อน

    In Attiny402, I want to put a delay of 40 seconds.
    Please suggest using assembly

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

    You got another subsicriber ☺️

  • @eduarddez4416
    @eduarddez4416 ปีที่แล้ว

    At uni their forcing us to program the Arduino board in C rather than in the Arduino language and I can understand why. Thanks alot for the tutorial

  • @zDoubleE23
    @zDoubleE23 2 หลายเดือนก่อน

    For part around 10:40, you might be able to fool the compiler by including semicolon “;” in your for loop.

  • @Fnta_discovery
    @Fnta_discovery ปีที่แล้ว

    Hello. often program the microcontrollers. I see bit shift right or left using symbol ">>". why we use it. I am waiting for your response thank you.

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

    good up to the point of delay function. NOP loops are not the way to go, missed opportunity to talk about timers and interrupts

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

      The hardest parts of these videos are deciding what NOT to talk about. Timers and interrupts are more of an intermediate topic that I didn’t want to spend time on quite yet. I ended up doing a video on timers (for STM32) eventually, and it actually ended up taking quite a bit longer than I was expecting

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

      @@MitchDavis2 valid points 👍🏻 im enjoying your videos so far, hope you'll come up with more!

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

    Would you consider making a similar bare metal series for ESP32, maybe the esp32-c3 RISC-V uC?

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

      I haven’t gotten a chance to use any ESP32s yet for projects, so I don’t know much about them. I have a few in a box, but I haven’t done more than simple blink examples with them yet

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

      @@MitchDavis2 I'd love to see the blink done from register manipulation, the most bare metal way the better. Maybe even assemble from assembly code, flash the binary to the uC. No C, no IDE...

  • @Flyonwaterslow
    @Flyonwaterslow 2 หลายเดือนก่อน

    🎉

  • @VeritasEtAequitas
    @VeritasEtAequitas ปีที่แล้ว

    Should have covered or referred to a video on but-masking the registers, but good video.

  • @saduniwathsala
    @saduniwathsala ปีที่แล้ว

    👍

  • @subhashkendole3005
    @subhashkendole3005 ปีที่แล้ว

    In Attiny402, this gives port not declared message. Which files to include ??

  • @trarredbandit9734
    @trarredbandit9734 ปีที่แล้ว

    Can you do ESP32 GPIO port manipulation?

  • @thomashvnmusic
    @thomashvnmusic ปีที่แล้ว

    To be honest watching this video made evrything click. For some reason it made me understand microcontroller programming better.

  • @medicallyunexplainedsymptoms
    @medicallyunexplainedsymptoms 2 หลายเดือนก่อน

    It's worth pointing out that writing to the whole port to change one bit is bad programming. The code below is a better approach, albeit with magic numbers. I left them in so it matched the program in the video, but defining bit 5 with a sensible name such as LED_Pin and using 1

  • @IvanEng747
    @IvanEng747 ปีที่แล้ว

    How to adapt this code for esp8266?
    To win in speed.

  • @brianxia
    @brianxia 3 ปีที่แล้ว

    Will continuously re-writing register wear down the memory? I heard they usually have limited number of read/write.

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

      Flash memory (where your program lives) and EEPROM memory (non-volatile memory) have finite write cycles. I believe they can OFFICIALLY be written roughly 100,000 times. Some people have done tests and discovered they work reliably for many more cycles.
      Registers live in RAM, and RAM is designed to be writen/read infinitely.
      Also, registers are what make the microcontroller work. There is no way to make your microcontroller do anything without writing to these registers. This all happens behind the scenes if you use something like "digitalWrite()"

  • @GrandNecro
    @GrandNecro 2 หลายเดือนก่อน

    i think if you indicate that the variable is volatile, the compiler won't optimize out the loop.
    for(volatile unsigned i = 0; i

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

    Ultimately the code will be compiled to machine code while uploading. Then why the "bare metal" codes are faster?

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

    where are those special variables defined?

  • @offensivebias3965
    @offensivebias3965 ปีที่แล้ว

    Question please correct me if i am wrong , by setting DDRB = 32; wouldn't that turn all PORTB# to be as an output ?

    • @zDoubleE23
      @zDoubleE23 2 หลายเดือนก่อน

      No. 32 is just decimal form of binary 00100000.

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

    Can you not use hex values instead of decimal values?

    • @MitchDavis2
      @MitchDavis2  3 ปีที่แล้ว

      You can use whatever you want. When it goes to the compiler, it's just a number. More commonly, you write something like (1>>5), which means "the 5th bit is a 1". I cover bit shifting in a later video.

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

    Why don't you make i volatile? That should be enough to trick the compiler

  • @fixfaxerify
    @fixfaxerify ปีที่แล้ว

    Another solution might be to declare the loop variable volatile.

  • @nm7c00
    @nm7c00 10 หลายเดือนก่อน

    I think he could've changed the compiler options to -o0 to avoid optimization

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

    I wish u see how the delay function is working , IN ASSEMBLY , that's scary .

  • @ay-pj8co
    @ay-pj8co 3 ปีที่แล้ว

    Dont you have to give data type when assigning a variable? So shouldnt it be uint32_t PORTB = 32 instead of just PORTB = 32?

    • @ay-pj8co
      @ay-pj8co 3 ปีที่แล้ว +1

      @@MitchDavis2 i really appreciate the quick reply that clarified that issue for me. Just one more question; why do we upload our programs to an mcu in hexadecimal format? Shouldnt it be in binary format? Thanks!

    • @ay-pj8co
      @ay-pj8co 3 ปีที่แล้ว +1

      @@MitchDavis2 thanks again for clearing that up. I'll be sure to watch the videos you referenced and i hope you keep making these videos since good embedded systems content is really lacking on youtube in my experience. Thanks!

  • @CallousCoder
    @CallousCoder 3 ปีที่แล้ว

    I can make it harder, though.
    This is level 2.
    In c I can push this with some nice bit wise operations like
    PORTB = PORTB | (1

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

      Bit manipulation comes in a bit later. Don’t worry, there’s 9 whole videos that go into making a complicated blink example :)

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

    so why do we put "32" there I don't get it :/

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

    hi, why don't you use millis?

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

      Part of this series is breaking down code and avoiding anything that uses the Arduino framework. This is entirely for educational purposes to show what’s going on under the hood. Millis() is Arduino code to get the number of milliseconds from the start of the program

  • @QWin-ir6yq
    @QWin-ir6yq 2 หลายเดือนก่อน

    Where are definitions? Will this code even compile?