Home / Micro800 / Micro800 Programming
Description
Demonstration of how to build a digital logic full-adder using a User Defined Function Block (UDFB) in the Allen-Bradley Micro800 PLC using Connected Components Workbench (CCW). The full-adder UDFB is then reused to build a 4-bit ripple-carry binary adder.
Transcript
(00:01) so in this video I’d like to go about showing how we use the userdefined function Block in the micro 800 and in the CCW or connected components workprint software so a userdefined function block is basically a piece of code that I can create once and reuse it multiple times and it’s kind of a parameterized um function block or Stine where I can basically send in the inputs it’ll solve some logic and then I can pass the outputs out and I can basically reuse that code over and over again uh to do this I’ve got a project already
(00:38) created I’m going to use the micro850 simulator um because it’s going to be a little bit easier to show what I want to do here so I’m going to go ahead and open up the simulator I come to tools and I say micro 800 simulator so U right now the IP address is defaulted to the loop back interface I’m going to leave it at that I’m going to go ahead and start the simul Ator just to get it going um note that I am using the Developer Edition which basically the simulator will run for one day uh if you’re using the free Standard
(01:11) Version you can still use the simulator it only runs for I think maybe 10 minutes at a time which is sufficient amount of time to uh to do some testing so I’m going to go ahead and uh so I have it running we can see that it’s powered and running and uh the green is on there too so I’m going to go ahead and minimize that for a few moments so what’s where I kind of came up with this concept is um recently have was kind of going back and looking at some of the basics of microprocessors and digital logic and um was kind of looking at you
(01:52) know half adders and full adders and basically B you know adding binary numbers together and then doing it with logic gates so this actually makes for a really interesting example just to show the capability of what a userdefined function block can do now um would you actually need to do this in the micro a00 uh I don’t think so but it’s just a good exercise so the concept here is that if you were going to build a you know a half adder or a full adder out of either logic gates or even out of relays themselves electromechanical relays um
(02:30) you know you would create this this half adder and full adder circuit um so if we’re going to add two binary numbers together like we have input one input two or A and B I’m going to keep it simple with a four-bit number um just just to make it a little bit easier when I go and build the code but if we’re going to add two binary numbers together we know if you know input a is a one and input B is a one well 1+ one in binary actually becomes a zero and we carry the one over into the next um into the next uh position so you know this is a you
(03:09) know decimal value of one and this is a decimal value of one well 1 plus 1 of course is two but in binary uh two is basically a one in the second position here okay so to do that and uh kind of gone down a little bit of uh you know the YouTube and internet a rabbit hole and was researching uh half adders and full adders a little bit and to do it in logic gates you know basically to make a full adder we need to basically combine two half adders okay so um again not really trying to uh teach digital logic here but just kind of showing you what
(03:49) I’m going to set up so for a half adder basically our inputs A and B will go into an exclusive or gate and they will also go into an and gate and then then we get the sum out of the exclusive or and then we get the carry out of the and gate so that carry is basically what happens here right we’re carrying the one over to the next position and then to make a full adder we need to basically combine two half adders and then these carry outputs go into an or gate to determine if we if we have a carry out to the next
(04:25) position and then next and the reason why this is a good example for a userdefined function block is we can basically string these full adders together to make what’s called a ripple carry binary Adder so basically each one of these full adders kind of you know basically the output of the First full adder kind of gets sent to the second full adder to the third and the fourth and the fifth and so on and so on so on so to make it simple again I said I’m going to do a 4-bit number so that would basically be four
(05:00) full adders kind of cascaded together or uh in this again terminology Ripple carry um we can keep doing this you know 8 bit 16bit whatever we want right so we’re going to build this uh basically build this into a a userdefined function block and then I can reuse that in the code so let’s come back to our uh our logic I don’t have any programs here I’m going to go ahead and create a userdefined function block first and when I say I can right click on it say add we can create either a structure text ladder diagram or a function block
(05:40) diagram um block I’m going to go ahead with function block diagram I’m going to go ahead and rename it to [Music] adder and I’m going to double click and that opens up our function block space so I’m going to to go ahead and add um the actual instructions themselves so uh rightclick uh show instructions and if you remember from a moment ago our very first gate is an is a exclusive orgate so it just so happens that we have logical functions in the micro 800 so again that’s partly why I just thought this was an interesting exercise so
(06:26) there’s our exclusive wargate inputs one two and the output I’m going to go ahead and add a uh second instruction block and this will be an and gate and I’m going to go ahead and add a couple of variables here all right we need to have our input one and input two and we all seem to have what’s going to be called our carry in so that’s kind of our input put at the moment and I’m going to go ahead and copy and paste this to make it a little bit easier and then if you remember back from the uh from the drawing here we had
(07:16) uh you know exclusive or and an and and then a second exclusive or and and that’s the second half adder and then I also had to have an or gate down here for the carry out so if I come back I’m going to go ahead and add one more instruction down here I’m going to uh call you know make it an or gate all right and then uh of course we’re going to need two more variables at this point we’re going to need uh an output and we’re going to need this carry out all right so the carry out actually connected there so that’s fine
(08:00) U the sum basically the output of the second exclusive orgate will go to the to the output or the sum um so the input will go to the first exclusive or and then we will also combine the input to the and gate all right so going back to our diagram here uh a goes into the exclusive or and it gets uh paralleled into the and gate and then the output from this first half add goes into the second half add and then the uh the carry in goes into the second so basically the output goes into a and this carry in goes into
(08:43) B of the second half adder so I come back to my diagram this will go this will connect to there and this will connect to there and then I will also connect the output to this and gate and to there and the lines kind of get written over here a little bit we can always clean this up later but uh for points of this and then basically they only got two more connections to make here and that is the outputs of each and gate will go into this or gate to determine if there’s a carry out all right so that’s basically half
(09:26) adder one half adder two and then the gate so I can now basically this is now my full adder block that I can reuse multiple times now the only other thing I do next is Define the variables so these will be local variables all right so we have Global variables which are of course uh accessible by all programs local variables are only accessible to this local program uh what we’re calling Adder so I’m going to double click on local variables eventually it opens up so I’m going to need to create several variables here so again I have an input
(10:15) a input B I have a carry input I have the sum output and the carry output essentially so uh so the key is that when we create these variables we’ll call it input a and it’s needs to be a Boolean and the direction is we want to make this a variable an input all right so this will basically be an input into this function block and we’re going to create a second one called input B is also a Boolean it’s also a variable input I have one more carry uh one more input I’m going to call this one carry in and again this is an
(11:06) input then we’re going to have two outputs from this function block one is the sum so we’re going to Define this as a variable output and then the other one is carry out also an output now note that there’s also just a VAR or variable so if I needed to I could create variables that are only kind of used internal to this block they’re not going to be something that gets passed in or not a variable that gets passed out just gets used internal to the logic in this situation I actually don’t have any internal variables to reference so I’m not
(11:46) creating any of that type so I come back to my Adder now now it’s a matter of populating those variables here into the right spots so for my uh um for this one be input a so I’m going to make sure I’m picking from the adder there’s input a we’re going to choose input B for this one and we’re going to choose the carry in for that variable for my two outputs I’m choosing the sum and then for this orgate it’s the carry out that’s it I’m GNA save it that’s all I got to do so now when I come back to my main program I need to
(12:40) First create one so I’m going to go ahead and add a program and I’m going to go ahead and make this a function block diagram as well for the main program because it’s going to be a lot easier to to draw and show that Ripple carry here so I’m going to go ahead and uh double click on program one and here again is program one so again this is a userdefined function block this is the main program this is what’s actually going to execute this does not automatically execute when the when the controller Goes On The Run mode this
(13:14) program one is what executes so to basically build the Ripple carry I’m going to basically do this I’m going to take four of these full adders that I just created and I’m going to basic basically give it an input and then tie the carry out to the carry in of each full add so to do this I’m going to come back to the toolbx I’m going to say instruction block now what’s kind of nice about having created this userdefined function block is now when I say show instruction block selector I type in adder and there is my
(13:58) userdefined block and there’s my input a my input B my carry in my sum and my carry out right so now I can uh copy and paste paste it again and paste it one more time so basically 1 2 3 4 that is basically mimicking one two three four so in this case we’re going to carry you know again the the carry out will feed the carry into the next block so we can string these along um just like this and then each of these adders will have a separate input for each A and B and a separate sum out so that brings us back to our variables
(15:03) again going to have two variables and we’re going to have one variable to come out now uh to make life a little bit easier I’m just going to change the color of a couple of these so we can we can kind of change the color and help them kind of stand out a bit better so this will be our input one or a this will be our input B and what we do here is being that I uh am using the simulator and I said I’m going to use a 4bit Adder I’m going to use these first four inputs as my input one so binary uh you know that’s going to be my
(16:03) uh my two to the zero position two to the One 2 the 3 and 2 the 4 and then I’ll use 7 six 5 and four for my um for the input B and I’m going to use uh 3 2 1 and zero for my output so basically we can represent the binary number and then see the binaries get added on the output St down here so I’m going to go ahead and use uh three for input for the first Adder two for the second Adder one and then zero just to kind of make it look like a binary number here um basically so what we’ll do is just to kind of help make this go a little
(16:51) quicker I’m going to uh Define this first one and then do copy paste so we’re going to do uh again di3 for the first input and we’ll use di7 for the input B I’m going to go ahead and uh do a copy paste and we’ll drop them on the next [Music] Adder paste again on the next adder paste again on the next Adder so now being that I’ve done this it should make it go a bit quicker I’m going to make that two make this six make this one make this five and make this one
(17:57) zero and make this one four of course we have to draw our lines and connect to each input for each Adder we do the same thing here with the output uh in this case we’re going to tie it to the actual output so that’ be d o and I’m going to use uh again 0 1 2 and three but I’m going to go with three here first copy paste paste and paste so we will change this one to two this one to one this one to zero and last thing is connect the dots all right so I basically again just
(19:04) built my ripple carrier Adder Ripple carry Adder um based on making a full adder function user defined function block so I could could reuse this code over and over and over again and basically we’re just going to pass in different inputs into each of these adders and then get a different output for each Adder so basically that’s it so uh once again I have my simulator powered the IP address is tied to the loot back so when I come back to CCW if I come to my micro850 I’m going to say connect I’m going to come to the ab
(19:47) E-1 we see right here that the 1271 is the microwave 50 simulator I hit okay it says do you want to download so right now it is actually connecting to my emulator controller it’s downloading the program you notice how it just went into the program mode right there and it says okay download complete do you want to change it back to remote run and we say yes and there it went back in the Run mode so the point of all this again in was to basically build a uh you know this little logic that will add these two binary numbers
(20:37) so again we have input a input B so when I put in a one and a one I’m going to get a 0 0 one0 as the sum so we’re going to see that right now with these lights so just to show the basics if I don’t even have the input B turned on if I turn on uh input a okay so uh that turned on the first output second output third bit fourth bit right so remember think of this as a binary number uh where this is the least significant this is the most significant now if I do the same thing for the second okay turned on the first
(21:23) one turns on the second one oops sorry about that turns on the third one and turns on the fourth one so we know that you know basically that would have been 000000 0 plus 00001 and that basically turned that light on now if I turn on uh basically 0 Z 0 1 and then 0 0 0 1 look what happened it actually added it so it it took the uh it it it now so this is again this is 2 to the 0o this is 2 to the one so this would have been uh the one’s Place essentially this is now two so again decimal 1 plus decimal 1 equals decimal 2 binary 0000 0 1 plus
(22:15) binary 0 1 equal 0 0 1 0 so the adder worked and if we come back here into the logic we see that you know true true and then um we can see that this output is false but we have a carry is true and the carry got passed into the next adder and we see that even though these two inputs are false since the carry comes into the second Adder then the output is true so again why would you need to build a ripple carry binary adder in the micro 800 I don’t think there would ever be a reason to do so if anybody knows please
(23:01) please feel free to comment but uh the the exercise was really to show you how I was able to build you know one piece of logic and reuse it over and over again and even Cascade them together um just to do that so U really again the uh the key is uh the idea of being able to use you know reusable code with user userdefined function block and again we can create a a user find function block that is either based with structure text ladder diagram or function block um so we have the the ability to create user defined function box in any of the
(23:37) languages that we choose and then we could re and then we can use that function Block in any of the languages that we choose up here in the main program I just happened to use function block and this was function block but I could have easily have made this into ladder or I could have made this in the ladder and use up here in function block so a lot of Versatility and the ability to reuse your code
