Port Register Control | Increase speed of Read/Write - Arduino101

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is episode 2 of my arduino 101 series remember that this is not basic level anymore so we will learn some more in-depth programming for arduino in the previous video we have took a look at the data sheet of the mega 328 microcontroller and learn step by step how to configure the registers for the internal comparator and how to use that comparator today we will learn something even more important we will see how to use the port register control and why is that better than just using digital right or rate we will understand which pins correspond to each port how you can find these names on the datasheet how to configure the registers and control the inputs and the outputs with practical examples we'll see why this is a lot better so make sure that you subscribe and activate the notification bell and consider supporting my work on patreon so let's get started [Music] glc pcb is sponsoring this episode feel free to go to their website jlcpcb.com and find other services such as low-caste pcb manufacturer with 24 hours build time smt assembly of all your components or the snt stencil for soldering smd components using their platform is very easy to order pcbs just go and upload your gerber files and select the settings and for only two dollars you can order five pcbs of any color that you want receive them very fast and by the way even at this low price the finish quality is amazing what's up my friends welcome back this is the arduino uno that i will use for today examples this board is using the atmega 328p pu microcontroller this ic has 28 pins and it has a dip package but we also have basically the same microcontroller in an smd format and this is the atmega328p version this one has an smd package and 32 pins as memory and all the other stuff these ics are basically the same but the smd version has two more adc pins and one more ground and vcc pin so let's focus on the view version for now pins are labeled like this starting from the dot on the first pin to pin 14 and then to the other side to pin 28 but the connections between these pins and the pins of the arduino board are not using the same names i mean p9 from the ic for example is not connected to pin d9 from the board so let's see the real connection the atmega328 has three digital ports port b port c and d each of this port is controlled by a register and since the arduino uno is an 8 bit board each register could control 8 bits so 8 pins in this way digital pins from the arduino from d0 to d7 are controlled by the port b and the analog pins a0 to a5 are controlled by the port c now we will see in the interruption tutorial in the future that each of these pins has an interruption bit that is marked as pc int as for port change interruption then each of these pins could have extra functions such as accuracy communication pins spi communication wired communication pwm output the reset pin or the crystal oscillator input so this should be the final port map of the atmega 328 microcontroller where we have the port pins with one color and the real arduino pins of the board with a different color in order to make you understand better okay now let's see how to control these b c and d ports when i make for example this code here using digital write i set the digital pin d9 of the arduino to be high but internally what i really do is to place a one on the second bit of the port b register so why is that well because that bit as you remember represents the digital pin d9 of the arduino and by setting this to a 1 it means that it's set to high but digital write is a function if we open arduino libraries we can see this code for this function and as you can see these are a few lines it detects which port is selected what value is selected and then it says the port to that value so having more lines in the code it means that we have more delay time so let's make a test in an empty arduino code i set pin d9 to be output then in the void loop i put the pin to high and with no delay in between i put it to low and then i also place a delay of 5 milliseconds the void loop will repeat itself over and over the arduino uno works at the speed of 16 megahertz and because there is no delay between the low and the high state of the pin this code should create a very very short pause one second divided by 16 megahertz is 62 nanoseconds so i upload this code to the arduino and connect my oscilloscope to pin d9 so here we have the signal each 5 milliseconds we have a very short pause i make a zoom in and i measure the width of this pose and we can see it is 4 microseconds but why is this so long there is no delay between the high and the low state of the pin so why isn't this in the range of nanoseconds well that's because we are using digitalright which is a function of the arduino and this will take a while to execute we enter the digitalwrite function we get the values for the timer the port and the b to control then we decide if the port is for a pin or not we change the output value we stop interruptions for a moment and then we change the pin to high or too low so all these steps will take some time so that's why the pulse is longer than expected another solution instead of using digital right is to directly control the port register so let's see how to do that but first let's see the speed difference remember that we had a code where i put d9 to high and then to low with no delay in between the second code will do exactly the same but using port register control so i upload the second code once again when i zoom in i can see the pause which is a lot shorter now the pose is only 64 nanoseconds and that is 60 times faster than before the difference in time is amazing so if your code needs a lot of speed using register control is a lot faster i now use the port register control for all my projects and the results are way better ok so as you remember arduino has three ports pc and d today we'll center on the digital read and write so we won't use analog values in a normal code if you want to set a pin to high for example you first have to define that pin as an output right so for that instead we use the data direction register or ddr we have the ddrb ddrc and ddrd for each port placing a zero to the bits of these registers that means that the pin will be set as input and with the one we set up in as output let's say that you want to set pin d3 as output and the rest of the pins as inputs for that we do ddrd because pin d3 is on 4d and then we equal this to this binary value as you can see the fourth bit of this byte is equal to 1 so pin d3 will now be an output if you want for example pin d3 to d7 to be outputs we equal ddrd to this value and the same here for pd9 or for the pin a3 but using ports p and c okay so now we know how to set a pin to be input or output now let's see how to set it to high or too low for that we use the p or rt registers we have once again the port b the port c and the port d registers setting a bit to 1 means that the pin is set to high and with the bit set to 0 that means that the pin is set to low ok so before we have set ping d3 to be an output now let's set it to be high for that we equal port d to this binary value where we put a 1 to the 4th bit of the register which represents pin d3 as another example 4d equal to this value that means that the pin 0 1 2 4 and 6 are low and pins 3 5 and 7 are high so you can do the same for any other pin from any other port but this is not exactly a good way to configure registers why well because we affect all the bits of that register even if we want to change only one bit for one pin in the example before we wanted to set pin d3 to high so we equal 4d to this value so we set pd3 to high but we also set all the other pins to low so what if you don't want that imagine that for example that pins 5 and 7 are already set to high in a different part of the code and now with this line here you set those pins to low and you don't want that so how to set only pin d3 without affecting the rest for that we use boolean operators and and or we use the end operator to place zeros and we use the or operator to place once so for example for putting d3 to high instead of making poor d equal to the byte we do port d or equal and then the byte the or operator will work like this if we multiply zero with zero we get a zero if you multiply a one with a one you get a one and if you multiply a one with a zero or a zero with a one we still get a one so that's why we use this operator to put once because in any case there is a one the one will always win only when both values are zero the output will be zero so imagine that for the register already has the pin 5 and 7 set to high so the bit 6 and the 8 are set to 1. so now we do our register or our byte where bit 4 is a 1 because we want to set the 3 to high so 1 by 1 we make this operation 0 or 1 is still a 1. 0 or 0 is still a zero again zero or one is still one till we get to the fourth bit where one or zero is still one and we get this result so as you can see bit four is now a one but bit five and seven are still the same so it didn't affect the rest of the bits okay now let's say that you want to set to low a pin so we have to put a zero let's now put the tree to low without affecting the rest for that we make an end operator in this case one and one will give a one one then zero zero and one and zero and zero will all give the zero output only when both inputs are one the output will be one as well so that's why we use this to play zeros so in this case we do poor d equal to our byte but invert it so all the bits are ones but the fourth bit which has to be a zero so once again one by one we make the operation one and one is still a one one and zero is a zero one and one is a one and for the fourth bit zero and one is a zero so we put a zero to digital pin d3 without affecting the rest to invert the byte in arduino we can pass from all zeros to all ones and only change the bit that we want to use if that is confusing you can just use all zeroes but we add the exclamation symbol at the beginning with the exclamation signal in arduino we invert the values so this byte with an exclamation will be equal to this byte so that's how we set the pins to output or input and that's how to set them to low or high using the register control which is a way better mode but how can we read the value from a pin using registers instead of digital read for that we use the pin register this will store the input value of each pin for each port so let's say that for example you define pin d5 to be an input using this line in the code as we have just learned now in the code you want to read pdf value pin d5 input is represented by the 6 bit of the pind register so we have to check that if you want to check if this pin is high we have to make an end between the p-i-n-d register and this byte where the only one is on the sixth bit so when the input is low the value will be all zeros when the input d5 is high this value will be 1 0 0 0 0 and in digital that is a 32 so in case that you want only a 0 and a 1 value you have to shift the register 5 bits to the right for pin d5 four bits to the right for pin d4 and so on now the read variable will be a zero for low and the one for high the same as if you were using digital read if you want to see if the pin is low just add an exclamation signal before the parentheses to invert everything so in case that you want to make an if digital read you should make something like this and change the bit you put 1 depending on which pin you want to read also change pin b pin d or p and c depending on which port is that pin from so guys that's how you make digital read and write or set the pins to input or output with the register control make sure that you watch my previous arduino 101 video read the data sheet of the atmega328p that you can find below and more examples on ultranoobs.com make sure that you subscribe and activate the notification bell and if you consider supporting my work check my patreon page thanks again and see you later guys hey guys electrolubs here this is the end of the video and thank you very much for supporting my channel and watching my videos and maybe even subscribe to this channel and i would like to give a special thank you to all our patrons for supporting my work supporting my tutorials and if you also consider supporting me just check my patreon on patreon slash electronoop select any tire that you want and like that you'll be able to see my videos before the youtube release you can get in touch with me with comments or questions you can even receive a t-shirt like this one depending on the tire that you select and uh also maybe you have some problems with your project and will be able to help you so like that we can help each other so yeah once again thank you very much for all our supporters on patreon.com and by the way i'm also on facebook instagram twitter and and also our website electrons.io so if you make an account you'll be able to post your projects your tutorials teach others your work and also use the forum for our for the questions and all the doubts that you have thank you once again for supporting this channel for giving a like to this video and also by subscribing and supporting me on patreon.com keep up you guys
Info
Channel: Electronoobs
Views: 31,473
Rating: 4.9814911 out of 5
Keywords: arduino, tutorial, port register control, port, control, register, PORTD, PIN, datasheet, class, course, pin change, interruption, speed, increase, manipulation
Id: UhTRrjYXqPU
Channel Id: undefined
Length: 15min 22sec (922 seconds)
Published: Sun Nov 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.