# Write index of contact as Integer to word

## 3 posts in this topic

I am needing to write an integer to a word based on a contact. It's pretty straight forward but I need to do this for 300 contacts. If F0 turns on I want to write a 0 to W22, if F57 turns on I want to write 57 to W22, etc. I need to do this for F0 through F299 writing the index as integer to W22. I don't need to clear W22, it can have any number in it at any time. My OPC server will be looking at a different bit to determine when it needs to look at W22. I'm trying to find an easier way to do this then having 300 instructions doing a MOVP K* W22 when the contact turns on. If more than one F is on I'm okay with whatever the last F to turn on is stored in W22 (at least for now.) Any help is appreciated.

Thanks.

##### Share on other sites

Is this going to be programmed in ladder? If so this may help.

Off the top of my head I can cut you down to about 18 lines of code keeping it simple. 9 or so if you don't mind it being a little complicated. Someone else may have a better idea needing even less code.

Are you familiar with the principle used to group bits into words. Like K2X0 means 2 nibbles of X0 or X0+8 so X0-X7. You can do this in any size up to I think 32 bit words. So if you used K8F0 it would be 8x4 so F0+32  F0-F31 or a double word consisting of the bits at F0-F31.

It is hard to say with out all the details but I would maybe go this route. You would want to repeat something like this until all your faults are covered.

[<> K4F0 k0]--------[MOVP K4F0 W22]

[<> K4F16 k0]--------[MOVP K4F16 W22]

[<> K4F32 k0]--------[MOVP K4F32 W22]

[<> K4F48 k0]--------[MOVP K4F48 W22]

And so on till you reach F299.

If you don't mind it being a little more complicated then use K8F0 but then you will need to break it back down to 16 bit at W22 if it has to be 16 bit.

##### Share on other sites

I would do a FOR-NEXT loop instead. That way, you don't repeat code (I always try to never repeat code since an error in line 1 would also mean an error in line 2,3,4,5......). Second, it's a bit more flexible; if you suddenly have 400 devices instead of 300 devices than simply just increase the FOR count. In addition, it's fewer lines (and steps) of code

I've attached a small sample from Structured Ladder, but you can easily transfer it to Simple Ladder if you prefer.

2 people like this