How do you turn a bit on and off in data word?

3 posts in this topic


I am using syswin and i am doing an exercise on saving and moving data. My challenge is am monitoring temperature from a sensor and the range is -20'C to 100'C from the 4-20mA sensor, after conversion and testing for positive and negative the values are stored in D205. (I have done the conversion and testing for positive and negative values only assigning of bit 15 in D205 is left)

The simple program has to turn on Bit 15 if the value is supposed to be negative.
1000 0000 0000 0001, a simple approach to telling us this is -1’C. 

0000 0000 0000 0001, is telling us this is 1’C.

Bit 15 is going to be our Sign Flag, so we have to ensure that if the temperature is positive then  Bit 15 = 0, how can we do this make Bit 15 of D205 = 0?

If the temperature is negative then  Bit 15 = 1, how can we do this make Bit 15 of D205 = 1?

How can i turn on this bit inside a data word? for negative i thought i could just add #8000 to the values after conversion then storing in D205 but for positive values how do i ensure that bit 15 is 0?

How could you approach this? Am a bit stuck, all thoughts and ideas are much appreciated if possible you could add a snapshot from syswin or cx-programmer.

I have attached the WTANK.SWP file which can be opened in syswin or cx-programmer.

Look in block Main 8 Temp Buffer, Network 3 Convert to 'C


Share this post

Link to post
Share on other sites

The CPM2 does not handle signed integers very well.  There are two easy options. The NEG instruction will do the 2's complement math with one instruction. It will need to be mapped in via the Expansion Instructions in the project tree in CX Programmer. I've ran into issues mapping instructions in this PLC and I've never done this using SysWin. Don't forget to cycle the power to PLC after mapping the instruction.

The second option is to do the 2's complement math. The COM instruction will complement all the bits in the word. You'll then have to add 1.

There are other options like ANDW or ORW if you are interested.

Share this post

Link to post
Share on other sites

IO_Rack: Thanks for the guidance, i have decided to do bit masking by using your last option of ANDW or ORW, for positive values am performing #0FFF ANDW (Result of comparison) then for negative values am performing #8000 ORW (result of comparison). This has workout great for turning bit 15 to 1 or 0. These two operations are having no alterations on the read temperature since am only using the first right most 12 bits for storing the temperature values.

Your help is much appreciated.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now