Arduino Workshop - Chapter 4 - Bit Math

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] and now we arrive at bitmap many of the variables that we've used so far has been simple 1 or 0 variables boolean logic used with an integer variable and this is fine but we're only using 1 bits worth of data in a 16-bit number which is wasting a lot of space this is but one example out of many where it is incredibly valuable to be able to manipulate specific bits within data and control data at a lower level doing this is called bitmap or bit manipulation and it involves a set of operators called bitwise operators we'll be looking at different logical operators and processes such as and or XOR not and big shifting operators which describe how different binary numbers can be manipulated to understand this a little better we'll look at an example of its own and then combine them to perform powerful data functions if they're found to be confusing don't worry once you understand the concept of dealing with bits and bytes it'll start to make a lot more sense so let's take a look at some examples of these bitwise operators so here we have our different bitwise operators and or XOR knot and ship or bit chips and each of these perform the function to give an output based on binary numbers you can also use hex numbers or decimal numbers but it's a lot easier when it's used from the perspective of binary the cooked because if the forms specific bit operation so let's take a 4-bit number as an example we'll use 0 0 1 1 as our example now n is designed to take two values and output the third value and it will copy a bit to the output only if that fit exists in both operators so to understand that we'll take this number here so we'll be performing it with 0 0 1 1 and we'll use another 4-bit number perhaps 1 0 0 1 now the N function we'll copy a bit to the result only figures exist in this one and that one so it would be a zero because there is not a bit in both sections there another zero both places have a zero another zero and this would be a one because if it exists in both operands step which is cool so that is and which brings us to all so all says that there will be a bit in the result if it exists in either one operand or the other or both as well but it cannot be blank so in this first bit here there is a zero here and a one there which means there is a one in one of them which is all we need so that would be a one now there is neither in either of these so that would be a zero a bit except in one of those so that would be one and finally we have two ones which still holds true we would have one and that is the or operator so X or what's that about well it is called exclusive or shortened to X or what that does is it works it in the exact same way as or but only if it exists in one of them but not both which is pretty much you can equate to if the values are different so let's take a look well we have one in this operand and the zero in that part of the operand so we would get a one there are zeros in both of these so we get a zero there is 1 and a 0 in this place here now these are photo climbs and we or that would result in a bit in our results but with XOR it's exclusive so there has to be different it currently has a bit in one of the operands so that would be a zero there which brings us to not now not in shift which we'll get to now are a little bit different in the sense that they don't perform a result based on two different operands instead they modify a single operand so let's take our base number 0 0 1 1 in binary which if you'll know if you know your binary would be it's three so let's take a look well not simply invert whatever number you're using in it goes each bit and invert it saying if it is a zero then it is not a zero which is a one and if it's a one and it becomes not the one which is a zero so our output for naught if we performed it on this number would equal one one zero zero it inverts the bits and flips them pretty self-explanatory it's really cool now shift involves shifting all of the bits a certain amount of places so it shifts we have ratchets and lecture and these used to standard ariel greater than or less than symbols but two of them so that is shitless now on there is chips right they go in the direction that the arrows are pointing so let's take our number here we'll take 0 0 1 1 and what this says is that if we shift it left to each bit moves along two places which means our new number would look like if we shifted at one place instead it would be and so on and so forth and this year's right operator works in exactly the same way so if this is our least significant bit then shoots right operator we complete leave the result as directed here where we've got 0 0 1 1 shifted right to and give our result as 0 0 0 0 or 0 it was shifted 1 and we would get 0 0 0 1 or 1 which is really cool and that is a basic example some truth table examples of how you can use the different bitwise operators now that you've got an understanding of how the difference bit wise functions work let's put that knowledge to use with an example using shift registers shift registers are a nice bit of hardware which allows you to extend the number of output or M put pins on your microcontroller using only a couple of pins to control you extends those tins think of a shift register perhaps as a power board to your i/o pins a shift register requires three pins to control the clock pin the data pin and an update pin the clock is generated from your Arduino and allows data to be sent out one bit at a time along the data line in series these bits are received by the shift register and then sent up to each output channel in parallel so the shift registers that we're going to be working with has eight channels so we need to send it one byte of data you can get parallel input / serial output registers which you use for it adding additional inputs and serial input such parallel output registers which are used for adding additional outputs they can't work as both and today we're going to be working with the output type today we'll be looking at the parallel types to control some LEDs however the concept is the same only in Reverse to be using the input type a handy feature of shift registers is that they contain a serial output pin which allows data to be received at the input to be passed on to the next chip in the chain allowing for multiple chips to be connected up in series we'll be using a single chip for our example with eight outputs and using the bitwise operators that we've just looked at to control the data being sent to some LEDs so let's take a look here on the breadboard you can see that I've got eight LEDs that are all connected up and they've got a resistor running to ground on the ground rail with my breadboard and the wiring diagram is up on the textarea of the workshop to take a look at that and then I've got eight wires going to the outputs of the shift register and I'll have a pin map diagram of this particular chip it's a seven for HC five nine five and the shift register and then we've got those resistor on the ground our power rails tower connected to the chip set up in a pretty pretty standard example and you can easily change multiple chips as we said before then I've got power running from my Arduino here and then I've got the three pins data clock and the latch pin or the update pin so let's take a look at the code that we're going to use to run this so we'll go right from the very top and if it appears a bit complicated at first bare with it because there's only a simple number of different function to combine to give a few different results so we've got three constants here I died up in our clock pin and our latch pin and the shipped out functionality that we're going to use is embedded within the Arduino it's not external libraries that we have to add and because of that we need to declare each of these pins as an output because there's no library to initialize that for us here we've got to two different global variables one is a byte variable which we haven't looked at yet and by is specific to I do we know it's not part of the standard C make up but what it does is it create a single byte variable rather than an integer which takes up two bytes so handy for saving space and this is going to be LED mapping it's going to be our base example in which we perform operations on so much like in the white board example where we had a sample number that we use for the different operations that's what led Matthews emit has to be an 8-bit number or a single byte and you can go through and change this to self I've just made it to be 1 1 1 1 0 0 0 0 or that's 0 X s 0 if you're using a hexadecimal so that is our binary and 0 B just denotes that it is a binary number if it was 0 X that would be a hex number then delay time is you are just using some timing control so setting up in Moses output initializing the serial monitor and I'm using a function that where we've got written down below called shift right and this is just going to control the shift register and all I'm doing here is setting all of the LEDs as 0 for stars so we're using some hex here at 0 X 0 0 which is simply 8 zeros in binary or 0 in decimal so that's all of the outputs as off then we're going to serial print some instructions for ourselves because we're going to be using the serial monitor is the input through data to control the LEDs then our loop has a few different elements here we're saying if serial is available which we've looked at before which says yeah if there's some data waiting in the serial input that we want to use then we're going to pass that as an integer to a variable or local variable that we've called input our input value for short so then we're saying if input values greater than 255 because we only need a single byte of data some instructions say to answer a number between 0 and 255 but what if your friend who's testing out your demo is a little sick and enters something greater than that well we've all counted for that ins given some instructions saying Auto try again again enter a number between 0 and 255 and return to the top of our loop so let's assume that they've entered the correct number between 0 and 255 very good now what we can do here is we're saying serial prints decimal and then we're printing the input value as shown and that it outputs it or prints it to the serum about is a simple decimal number as you would input it into the zero minus because the 0 wanted up past the string values which were simply going to use a decimal numbers then it prints out the binary format so that we have an example to compare against easily as we've seen our whiteboard demonstration it's much easier to just use binary values for these for these different bit operations rather than decimal hex is still fairly easy but binary is by far and away the easiest because you can you can modify the bias to see what's happening directly now we're printing is the value of our number in binary so serial print our variable and then we put a comma and we're telling the format that we wish to print that variable in bin stands for binary and then the spare line for them for money now what we're doing here is we've got serial print and this follows we've got three different chunks of code for and or and XOR functions we've embedded two not in shape functions because they're fairly self-explanatory and they perform only on a single number that these perform on two different operands so serial print and results giving us some formatting there and then on the same line we're performing LED maps and input value as a binary number so we're saying alright we're going to take LED map which if we scroll up is 1 1 1 1 0 0 0 0 or 0 X s 0 or just s 0 in hexadecimal and we're performing the end function on it with that input value so we input a value and it ends with LED map and then print the output result then it goes to the function shift right and passes it one piece of data which is the result of LED map and we've added with input values we'll scroll down here and in shift right you see it gets top it takes one parameter just plus one argument here and digital rights this is how we use the shift register it's really simple we simply pull the latch pin load say hey we're sending some data be ready to update we use shift out and we pass it the pin that our data pin is connected to the connect clock pin is connected to whether we want the most significant bit or the least significant bit going first and you can just use that to toggle the direction that your data is going in and then when we print out the value which in this case is the parameter of value that we're being parked then we on take latch pin and pull it back up again seing the transfer is complete for only tasking a single byte of data and then it will be all sorted and update that value when we set up high again which is really cool that's how the shift right function works we go it and pass it the types of led map been ended with input valve and we wait three seconds wait for to have a look at it and then we do the same thing but with or and XOR and we use the pipe or vertical bar and the up arrow symbol there to perform those different functions and passed on shift right and at the end we rinse and repeat that loop so let's take a look I'm gonna connect my Arduino up make sure you guys can see that pretty well cool alright now let's go to our serial monitor and see if that runs first we can upload sketch wait for it to compile and upload and then we can go ahead and open up our serial monitor alright fantastic so given us some instruction to you enter a number between 0 and 255 let's say so we'll here if we enter 256 it will recognize that greater than are the limit that we've set and say oh no try again let's put let's put in 188 so it says 188 in decimal is 1 0 1 1 1 1 0 0 in binary and then we've actually just missed the shift register but if you're quick and you were watching each of those output values would be mirrored on our shift register so we'll run that again one a K and you can see the end result is there followed by the all results followed by the XOR result and you can see that the XOR results is one bit shorter than the other two and that's because it simply has a zero as the first bit or the number of bits before it receives a one because in binary that's the same thing that would read exactly the same as that was shifted over one with the zero put forth so run that one more time and I'll point you in the direction of the LEDs if that's the end result the or results and the XOR result in binary that with our LEDs lining up to indicate a bit you set really really cool use of an LED all right so let's take a look let's try the number ten now ten ended week a number at your zero or with it gives a fairly large number and XOR gives us the exact same number as it works out so you can go ahead and you can change LED map here to be whatever you want so if it was if it was 255 let's try uploading that and see how it goes so in this case because our base number is different if we enter ten instead of n being zero we're going to get a different number again because binary Kenyan binary is one zero one zero am i comparing that against four are three eight once we can see the difference between the XOR and the or result so that's pretty nifty it's a really cool use of shift registers and it's really simple bit logic you can see there's not a lot going on we're just performing a few different functions to get the different results use the shift register to visually display them and print them to the serial monitor which is really cool and whilst it might not be apparent what the power of using bitwise operators is they're incredibly valuable because most of the logic behind the built-in Arduino functions uses these these bitwise operators to function whereas if you were writing to bare metal if you were using an AVR microcontroller with a straightforward GCC compiler then you would need to each output each pour each input by itself when your going to need to use a whole bunch of different bitwise operators in a single line to perform the expulsion so it's really useful to know and as you start getting into some more advanced projects as we'll look in the future chapters you're going to use them heaps tip so check out the next section on using the EEPROM
Info
Channel: Core Electronics
Views: 44,114
Rating: 4.9332323 out of 5
Keywords: arduino workshop, arduino math, how to use math on arduino, arduino math tutorial
Id: McgvehwAako
Channel Id: undefined
Length: 17min 55sec (1075 seconds)
Published: Sat Mar 11 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.