Using MOD in CPT block Rlogix5K

matthew1

Member
Join Date
Feb 2011
Location
virginia
Posts
100
Hello,



Trying to understand the MOD in Logix 5000 when used in a CPT block.



I'm familiar with MOD or modulo as a ratio or division remainder.



However when it is used in a CPT block is seems to set a restriction/reset value for your calculation.



See below, When Index_REG_Length is set to 1000 the loop goes to 1000 and resets. I removed any fault handlers just to make sure and it still works as long as the value is not larger than a DINT maximum.



**Never mind cant upload pictures.
2018-10-01%2022_37_24-STUROCK_PLC%20-%20VMware%20Player.png
2018-10-01%2022_37_24-STUROCK_PLC%20-%20VMware%20Player.png






The MOD is used in help file examples but not expanded on.



Am I looking at this right or is there something else going on here?


Thanks,






Matthew
 
Last edited:
Good idea!


CPT Index_DINT "(Index_DINT+1)MOD Index_REG_LENGTH"

It's been a long time since I messed with MOD on PLC5. And I no longer have a PLC5 to test this logic on, so take it for what it's worth - just running from memroy.

1001 mod 1000 = 1. I used it as a way to make sure I didn't fault the PLC with a wayward calculation for an indirect reference.

I also used it to take accumulated time, a counter incremented each second, and split it into days, hours, minutes and seconds.

Counter mod 60 was seconds. (counter / 60) mod 60 was minutes. (Counter / 3600) mod 24 was hours. etc

Is this sort of example what you are looking for?
 
I've also seen it used for a counter reset. Let's say you want a heartbeat integer to count from 0-255 and then start again from zero.

You can:
1. Add one to the integer
2. Check to see if the integer >= 256
3. If so, MOV 0 to the integer

Or, you can:
CPT Integer = (Integer + 1) MOD 256

If the integer is 0-254 prior to the CPT execution, it'll basically just increment by 1 - the "MOD 256" has no effect since the result of the "Integer + 1" part is less than 256.

If the integer is 255 prior to the CPT execution, it'll add one to make it 256, then MOD it with 256 which will return zero. Hey presto, auto-resetting incrementing integer with one instruction!

The same concept presumably applies when limiting a value before using it as an array pointer. You can check the limits and clamp if necessary using three or four instructions, or you can use a CPT with a MOD and do it in one. In this case I'd tend to favour the check-and-clamp approach simply because if you do somehow get an out-of-bounds value, your CPT/MOD approach will surely catch it and return the value to a valid one, but you don't necessarily know what value that is. Whereas with the check-and-clamp, you specify explicitly what to do if you find it out of bounds. When you're just resetting an incrementing counter, less of an issue.
 
Okay, this makes sense now in how RA is using MOD. Thank you for the detailed explanations. Have tested this in 5k emulate, next time the ole PLC 5 test rack is out will experiment it as well. would assume that it works just the same. Think we still have around 200+ PLC 5s that are constantly changing will probably be a good method to learn.


Thanks again!
 
Yes that is the exact app that was confusing just because not knowing what MOD actually did. I had an index control bite me once because I did not limit the FB counter to a limit in an ST routine. Now a fault handler is added to catch this exception just in case i forget to limit the values. adding MOD into the expression looks like a great secondary check/limit method.



Thank you!
 

Similar Topics

Hey guys, I'm working on an existing system where the old programmer (from like 25 years ago) used a bunch of conditional scripts in Wonderware...
Replies
2
Views
1,727
I have a project to automate four generator sets. The system will monitor and store the load demand of the factory. Once there's Power outage, the...
Replies
0
Views
45
Adding ethernet equipment to an existing panel that has none. We have some solid ethernet cables coming from other remote cabinets that I plan to...
Replies
3
Views
109
I'm trying to control a device via MODBUS RTU and the ModbusRtuMasterV2_PcCOM in Twincat 3. I've configured a device with the right com port and...
Replies
7
Views
217
Hi, I'm trying to use the IO Device Library (Product Versions) which is configured to work with the 1756-EN4TR & 1756-EN2TR but my system uses...
Replies
0
Views
54
Back
Top Bottom