plc5 division remainder

What data-types do you want this to work with, the ControlLogix will work with xINTs or REALs.

If the PLC5 doesn't have the MOD function, then it is just a case of continuously subtracting until you can't anymore.

You can do this using a loop, i.e. a JMP/LBL combination, but be careful if the two values would cause excessive scan times ( eg. 10 mod 0 would be infinite!)
 
Which PLC/5 are you working with? Its a little easier to roll your own with an enhanced processor, but its doable with either.
 
Not to distract you from this mathematical diversion, but the PLC-5 Enhanced processors do have an Integer Modulo function. You can use it in a structured text routine or as an expression in a calculation block like CPT.

An example:

CPT
Destination N7:2
Expression N7:0 MOD N7:1

Where
N7:0 = 10
N7:1 = 3
results in N7:2 = 1, where 1 is the remainder of 10 divided by 3.

The MOD operator does not work correctly with Floating Point operators in ordinary PLC-5 controllers but does work correctly in the SoftLogix 5, according to the online Help file.
 
r = n - (d * Int(n / d))

10 / 3 = 3
N7:0/N7:1->N:2(int)

3 * 3 = 9
N7:1*N7:2->N7:4

10 - 9 = 1
N7:1-N7:4->1
 
That works for 10/3. It does not work for 11/3. The PLC/5 will round 11/3 and give you 4 as the answer.

Still waiting from frw312 to see which processor he is working with, as well as find out which data type he is working with.
 
hats off to Ken ...

personally I'm EXTREMELY impressed with the "MOD" function from Ken Roach ...

what would we do without the guy? ...

I tried it on a PLC-5/20 and it works fine ... even so, I haven't been able to find it mentioned in any of the PLC-5 documentation (yet?) ...

so ... where does Ken get this stuff? ... and what else does HE know that we DON'T? ...
 
Originally posted by Ron Beaufort:

so ... where does Ken get this stuff?

I found this several years ago also. I used it once in a structured text routine and thought I would take a chance that it would work in a CPT and it did. It is also an inline ladder instruction if you want to use it that way.

I am currently running Logix5 V6.2. If you search for MOD in the help it does come up. But it isn't one of the listed instructions under instruction help and isn't in the list of valid instructions for a CPT in Help.

Keith
 
Ron, its not listed in the instruction help of logix5 but it you do help->Contents->Index and type MOD you will find the help page for the mod instruction for the enhanced series of processors.

Interestingly, you can even enter the MOD instruction as a block instruction on a rung, but it won't verify. You will get an error message that it can only be used with CPT, FAL, FSC, and CMP instructions and ST files.

A081710-A.jpg
 
thanks ... got it now ...

thanks, Keith and Alaric ...

I did a search of the online PLC-5 Instruction Set Reference Manual at the AB Literature Library and came up empty ... it appears that the RSLogix5 Online Help is more up-to-date ...

party on ...
 
If anyone was following the bit counting thread the other day I mentioned that I := J & NOT (J-1) and several other bit hacks were so useful that it was worthwhile to memorize them.

I := J & (K-1) will return MOD J%K for any J=2^n



For a general case for integer modulus on non-enhanced PLC/5s I was thinking of this method using an intermediate float. Its not the most efficient (Peter hinted on that one), but its easy to follow.
DIV N7:0 N7:1 F8:0 //do a floating point divide
ADD F8:0 -.5 N7:2 //add -.5 and store in integer to truncate the unrounded float
MUL N7:2 N7:1 N7:3 //now multiply by divisor and
SUB N7:0 N7:3 N7:4 //subtract to get the mod.


If frw312 is looking for the ability to do modulus division on a floating point number I was going to point him to a post on another forum where I showed how to truncate a float in a PLC/5, including floats larger than 32767. However that gets into the inner workings of the IEEE-754 float format and is beyond many PLC programmers and I didn't really want to go there if its not necessary - hence hoping that frw312 will tell us more about the processor and the data type.


Ironically, integer mod is easier on a bottom dwelling SLC500 (use the math register) -vs- a non-enhanced PLC/5, even though IMO the PLC/5 for the most part has a superior instruction set.
 
Last edited:

Similar Topics

I am using the following formula and I am getting error, Invalid Expression - too many closing parenthesis. when i copy the formula to notepad or...
Replies
4
Views
159
Preface: Kinda long, so I made section titles Intro: I just want to see if anyone here has seen anything similar. A PLC5-40 series C enhanced...
Replies
3
Views
378
Hi, can anyone help me get a pdf file for this RSP files. They are from a PLC5. Thanks
Replies
5
Views
532
Hello all, I am seeing this behaviour where an integer file (N46:33), has an integer in binary (11110) which is 30 in decimal. I did a...
Replies
7
Views
523
Back
Top Bottom