So Kevin, I've watched several of your videos now over a period of time of course. I started with the Shift Registers and somehow always stumble across your videos when trying to learn about whatever it is I'm working on at the moment. So I think it's more than appropriate to like and subscribe. Now this video shed some light from the power series I watched prior. This more or less pulled the blinds up to let the light in. So now here goes that really dumb question at lightspeed. I'm currently working on some LoRa Feathers I bought from Adafruit. The idea with this project is sense PIR statuses and send that data via radio packets to another radio. Now in order for this to be a feasible project I am going to manage power much as you described in the Power series. But I also want to use interrupts and the like. So in other words these radios will be just chilling around the backyard, the solar panels will be feeding the batteries until a motion is sensed. At which point the Feather will wake up do the radio TX/RX and go back to sleep. Long story short, Can I use these methods on just about any Atmel even though it's on an Arduino Compatible of various sorts? I don't need any special IDE settings? Or a library to use code bits like portd?
@dp08137 жыл бұрын
I don't quite understand everything here but that byte reduction is INCREDIBLE! Can't wait to try this out!
@SalisburyKing5 жыл бұрын
I have watched a few videos on the subject but managed to grasp the concept after watching this one. Thank you.
@robinwilliams53482 жыл бұрын
Apologies if this is a dumb question... Instead of shifting 1, four times to the left to get the correct bit value / mask, could you just use "16" or "0xF" are they not 00010000 without the bitwise shift operations? (I saw a reply below suggesting using 0b00010000 if that would work?) Thanks for the video - it explained it really clearly :D
@VFMIAB8 жыл бұрын
Thanks so much for this video ! It made many things clearer to me (including what bit shifting is, and how it's done ;) ). Do you have any idea of what "digitalWrite" is doing that it is so much slower?
@montpierce4248 жыл бұрын
Great video, thanks Kevin. It's interesting that some of the comments demonstrate exactly why functions like digitalWrite () exists. Many arduino programmers don't understand the avr microcontroller and/or the math needed to twiddle bits... It's great though to see everyone having fun with arduino. Some may even get interested enough toin look at Atmel's atmega328p datasheets and some AVR microcontroller programming books. If they do they won't be sorry... It will be like taking off the training wheels.
@malgailany9 жыл бұрын
Nice technique. Its interesting if we can see what maximum frequency you can toggle the port using the two methods. Thanks.
@kennethbentley95269 жыл бұрын
what happens if you remove the sleep, then measure the frequency it switches the led on your scope? I bet with the direct port manipulation you'd notice you could switch at a higher frequency. unless your arduino's clocks are synced, I don't think you can measure delay like that. not sure though
@jean-christophesicotte-bri13156 жыл бұрын
Hi Kevin, I'm not sure why wouldnt it be simpler to just use the function bitset() to turn on a single bit in a byte, instead of using and or equals, shift to the right and all that good stuff? is because bitSet() cant be used with ports and registers? if so, I guess we could use a byte variable and bitSet whatever in it in order to pass its value to the port we want to change? Would that work?thanks!
@SusanAmberBruce4 жыл бұрын
Is it possible to do a parallel input using a Port command and how is this done?
@philbx19 жыл бұрын
Thanks again Kevin. Very well explained as usual! This reminds me of the Uni guy David on the EEVblog channel rewriting C++ math functions to speed up his 3D printer. Sure, higher level code is good, but at a major price in overhead.
@derkeen21386 жыл бұрын
This is the consequence for the easy entry in programming with the Arduino IDE. You will learn a lot about processors if you go into registers and clock cycles. All those arduino functions are compiled with a lot of jumps, stack pushing, RAM operations and finaly register operations. Every 16 clock cycles used for those operations more, slows down your execution by 1us. Bit-operations is something every programmer should learn.
@Mumme6665 жыл бұрын
I´ve learned so much from your videos Kevin! I went back over them a few times in a couple of years, and now direct port manipulation seem the shizzle... ;) Thanks!
@mahdishaban8 жыл бұрын
Why not use PORTD &= (0
@ColinRichardson8 жыл бұрын
+Mahdi Shaban You cannot use (0
@mahdishaban8 жыл бұрын
+Colin Richardson ahh I see thanks for the reply
@rickmorenojr7 жыл бұрын
So, I guess you could also use PORTD &= 10000
@rudysneppe36837 жыл бұрын
Hey, Kevin Do you happen to have a hardcover book with this weird stuff explaned with lots of examples. Direct port manipulation, registers & timers for Arduino Uno Nano (Atmega 328)
@m4rt1006 жыл бұрын
hi. could you explain how i should connect the osciloscope probe to the pin of the Mcu
@MrMoonlightMan8 жыл бұрын
Kevin, thanks for this and all the other vids you have been doing so far. All very clear and inspiring. I may be off topic here, but I'd like to know your opinion about particle Photon. thanks.
@MithatKonar8 жыл бұрын
It would also be instructive to see how long it takes from the interrupt line going low to the LEDs turning on.
@kushalhn5154 жыл бұрын
Liked and subscribed. I have to build a circuit for short duration pulse. I am shifting my home tomorrow and decided i should finish this and many more works before i leave my township that i stayed for 23 yeaRs . Thank u so much ! I dont know if u will read this .. but thank u
@jeffreylebowski49275 жыл бұрын
Would it be faster to just use B00010000 or H10 instead of (1
@AliG.G7 жыл бұрын
So whats the conclusion? There is no difference between digitalWrite or Direct port manipulation? Also I am lost where it says ~4ms.
@93Hotshot7 жыл бұрын
Ali G in a few words: your arduino acts faster with port manipulation, it change the pin states 4 microsecondes faster than the digital write function
@DavidKirtley9 жыл бұрын
Since the processor and ports are not bit addressable, the libraries will be doing the same operations for the bit twiddling. Either their implementation is extra super crappy (unlikely), or you are waiting out a port write (or a sequence of port writes) setting each pin with each digitalWrite(), depending on its implementation. The delay being a factor of the clock speed seems to indicate something along this line. The more interesting problem is setting multiple pins in sequence. With just the digital pins, you are setting up to 14 pins, that means a delay of somewhere between 4 us and 56 us (14 * 4) calling digitialWrite() for individual pins. The time depending on how many pins you are setting. Direct port manipulation is just however long it takes for the bit twiddling in the registers (fast) plus either one or two port writes take (depending on the number of pins set) for the same operation.
@ColinRichardson8 жыл бұрын
+David Kirtley I recently changed my code from digitalWrite to PORTB and PORTD, and it drastically increased my speed.. I was multiplexing Nine 7 Segment displays at 60Hz with a binary to 7Segment IC and a Binary to Decimal IC. And you could see the haze of the previous/next value about to be wrote on the segments since I couldn't change the number and digit fast enough with 8 digitalWrites.. But after the changed to PORTB and PORTD writes, it is crystal clear.
@PerchEagle4 жыл бұрын
How about using assembly code ?
@pavelp808 жыл бұрын
Hmm, might be nice to turn 3 phase bldc motor faster, have to try sometimes when I find dead hard drive. Your tutorials are excellent and useful, eg. low power designs, I can learn from them a lot.
@gc9n7 жыл бұрын
Hey Kevin . I have a pro mini and what i want is turn State of A1 and A0 simultaneously OLD WAY if Something digitalWrite(A0, LOW); digitalWrite(A1, HIGH); else digitalWrite(A1, LOW); digitalWrite(A0, HIGH); end New way if Something PORTC = (PORTC & B11111101) | B00000001; else PORTC = (PORTC & B11111110) | B00000010; end Is this Correct? if considering the used platform(arduino pro mini.) Thanks in advance
@Kevindarrah7 жыл бұрын
I'd have to check the datasheet for the PORT assignment, but you if you want to tangle both, you could just shift over something like a B11, instead of (1
@rajeshsharma-ng3cb6 жыл бұрын
Can you make a tutorial about timer interrupt in arduino I'll love to see it
@SyedRizvii9 жыл бұрын
Nice one Kevin, would appreciate if you could do one on interrupts with some examples likes of the old school PIC timers etc i.e. tmr0 and tmr1 stuff and how we can implement them in arduino efficiently
@Kevindarrah9 жыл бұрын
+Syed Mazahir Rizvi yea, was thinking about that. The AVR has some pretty powerful timers/counters. The input capture counter is cool too - did a line frequency monitor project with that recently.
@aravindma22097 жыл бұрын
So, when you do this 'quicky' thing, you're simultaneously changing the state of other pins too I assume; correct me if I'm wrong.....thanks :)
@priyabratasaha53857 жыл бұрын
Is there some way to digitalRead() with direct port manipulation?
Priyabrata Saha sure read pins register and mask off the unneeded bits and you can test for a 0 or greater than 0 to see if it was hi.
@rich10514146 жыл бұрын
portState = (PORTD & (1 > portNumber; To explain, "(1
@PerchEagle6 жыл бұрын
I learned that the shifting might take some cycles, how about writing to the port like this: PORTB = 0x01 or PORTB = 1 Isn't it faster than writing a shifted value like: PORTB |= (1
@fredlodden15385 жыл бұрын
Yes, simply writing a byte value to a port is faster as it avoids all the shifting of a bit and the ANDing and ORing and NOTing. But it also wipes out the settings for all the other bits in the port. These other bits are digital inputs and outputs too, so altering 8 pins at a time when you only want to change 1 could really make your peripherals go nuts. Hence the need to only affect the target port bit when setting or clearing one pin - and that introduces the requirement for bit shifting and logic operations.
@ismzaxxon9 жыл бұрын
i am assuming arduino does not support things like portd.4 & 00001000B.
@AugustoWeber6 жыл бұрын
I was thinking why make another instruction operation of shift when you can use OR and the binary number wanted. As the same to reset. With the AND instruction.
@pakcheesy28 жыл бұрын
What is his USB to serial chip?
@rasmushaun17735 жыл бұрын
Nice video (: You could also just do: PORTD = B00010000; delayMicroseconds(5000); PORTD = B00000000; Or is it wrong? (:
@lamjota5 жыл бұрын
i think you could make PORTD = PORTD | B00010000; //Puts bit 4 High without the risk of changing any other bit delay(2500); PORTD = PORTD & B11101111; //Puts bit 4 Low without the risk of changing any other bit. delay(2500); i tested this in atmel studio 7
@Avionics24 жыл бұрын
At last!!! Someone could explain this. Thank you very much !
@PaulHikes228 жыл бұрын
Exactly what I've been needing. Perfect. Thank you!
@rich10514146 жыл бұрын
I don't quite understand how it could be quicker unless it is a api inefficiency. You should request it to be implemented into the api, unless there is a very specific reason the api version is inefficient.
@alexcho85578 жыл бұрын
Can't you just do PORTD = (0
@jeffreylebowski49275 жыл бұрын
no, because you want all the other bits in the register to remain unchanged - with your method, you would set them all to 0.
@zebratangozebra6 жыл бұрын
Great explanation Kevin.
@garybooting_co_uk58667 жыл бұрын
OK so direct port manipulation is faster but is not assembly language even faster?
@JasonMasters7 жыл бұрын
It depends on the programmer. ;) But yes, assembly code is generally faster to run than any compiled language. The trade-off is that a compiled language will take care of many "fiddly bits" for you and is easier to write and usually easier to understand, while assembly code requires the programmer to think of everything (pretty much literally everything) and is a bit more difficult to understand.
@realvideosrv18796 жыл бұрын
@jasonMasters I thing it is a Byte more difficult to understand
@shep74847 жыл бұрын
Those circuits you show in your circuit don't look like Arduinos. Are they Microcontrollers?
@TechnoAutomation7 жыл бұрын
Thanks for this buddy. Nice work.
@dufniall899 жыл бұрын
i really enjoy your videos thank you for your time
@SheltonDCruz6 жыл бұрын
brilliant - thanks Kevin.
@TomMinnick8 жыл бұрын
Hey Kevin as always love your videos. I did a similar test a few years ago here...damage-designs.com/blog/2012/8/17/arduino-speed-test.html Question for you, does the shift operation itself (1
@Kevindarrah8 жыл бұрын
+damage.cc good question - I've been wanting to write my own code to drive the WS2812 LEDs now for a while, so I'll need the writes to execute as fast possible. Might keep on trying new things to get them even faster.
@richardcasey41468 жыл бұрын
+damage.cc Compiler must convert it to a static value. Using the shift operation vs using a binary constant produces identical code. i.e. (1
@ColinRichardson8 жыл бұрын
+Richard Casey wonder if it's compiler specific. Also, wonder if B00010000 and 0b00010000 turn out the same too.. So many ways to represent the same number.
@rajeshsharma-ng3cb6 жыл бұрын
Thanxx loved the way you explained
@MilanKarakas8 жыл бұрын
Great example. Thanks!
@satavtarsingh33318 жыл бұрын
Nice work. Thank you.
@jondoe66088 жыл бұрын
keep the vids going!
@zanityplays3 жыл бұрын
Indeed
@henkoegema63902 жыл бұрын
Clear explanation. 👋
@idvfpv3 жыл бұрын
thx, it helped me a lot!
@SKElectronics7 жыл бұрын
Good explanation understand very well.
@mixtermuxter86029 жыл бұрын
Awesome man!
@krystian25216 жыл бұрын
You shouldn't use delay in interrupt
@AtomkeySinclair5 жыл бұрын
Excellent information sir! Thanks of the time (no pun intended).
@ro2nie4 жыл бұрын
Quinn from Homeland?
@CutiePie43257 жыл бұрын
I heard you liked breadboards, so I got you some breadboards for your breadboards so you can bread while you board.
@iceberg7899 жыл бұрын
thanks u're the best !
@starfirrxvx66548 жыл бұрын
you can do it in another way just simply do PORTB=40;
@szafran15438 жыл бұрын
Dude, you looks like Leonardo Dicaprio :D:D:D
@bluebear255197 жыл бұрын
if leonardo dicaprio and johny deep have a child
@srijal9 жыл бұрын
Instead of PORTD &= ~(1
@Kevindarrah9 жыл бұрын
+Srijal Poojari nice, yea, that should work too
@srijal9 жыл бұрын
Kevin Darrah cool, thanks!
@oreganodealerlsog86309 жыл бұрын
this will toggle the bit
@srijal9 жыл бұрын
Oreganodealer LS OG What do you mean?
@oreganodealerlsog86309 жыл бұрын
PORTD ^= (1
@doctorvox6 жыл бұрын
Wow, this is hard enough to understand without all the mistakes.... can you do it again please?!
@ekiskaliburnirvana90474 жыл бұрын
You know fastwrite but dont know to increase font size.
@mattibboss8 жыл бұрын
if you showing any code....zoom in...it annyos the hell out of me if i have to go to my PC and look your videos there...
@mattibboss8 жыл бұрын
if this is better than why is there DigitalWrite at all?
8 жыл бұрын
+mattibboss humanized command.
@montpierce4248 жыл бұрын
digitalWrite () uses the arduino mapped pins. digitalWrite () calls library functions to find the mapped AVR port and port bit #. Using digitalWrite () simplifies coding at the expense of performance. Also, it only sets or clears one bit at a time, which creates a ton of unnecessary overhead if you need to set/clear several bits in the same port. Run your arduino code from debugger and you will see all the function calls needed to find the port/bit to set/clear. digitalWrite() simpifies arduino programming so you won't need to reference the atmel atmega328p datasheets, which a lot of people (especially beginners) find a bit confusing.
@JasonMasters7 жыл бұрын
... or you could just learn to program in your Arduino's native language via assembly code. ;)
@Acky00785 жыл бұрын
Why would anyone use those stupid arduino libraries on such a simple mcu?!