Negative back to positive angle
I am reading an incremental encoder to measure angle. Based on the encoder, I will INC or DEC a word I call "angle" representing the angle in tenths of degrees (3600 = 360 degrees). What I would like to happen is for the angle to switch from -999·to 2600 when decreasing and from 2600 to -999 when increasing.
I thought I could do this, but it only seems to work for increasing. When decreasing, the number just keeps decreasing. In this example, the value of angle remains -1000
Am I doing something wrong?
Thanks for any help
Post Edited (Clint) : 7/1/2008 7:44:08 AM GMT
I thought I could do this, but it only seems to work for increasing. When decreasing, the number just keeps decreasing. In this example, the value of angle remains -1000
angle VAR Word angle=-1000 IF angle=2601 THEN angle=-999 ELSEIF angle=-1000 THEN angle=2600 ENDIF
Am I doing something wrong?
Thanks for any help
Post Edited (Clint) : 7/1/2008 7:44:08 AM GMT
Comments
I do not have the answer for you as to why this is not working, but I have learned in using SX SIM on your code is that the compiler is taking your statement of "ELSEIF angle=-1000 THEN" and making it "ELSEIF angle=1000 THEN". Why the minus value is being made a positive value is something others will need to explain. I have checked the help file and see no numeric limits for the IF..THEN..ELSEIF..ENDIF command.
The VIEW LIST of the code for "angle=-1000" is:
182 0067 0C18 MOV angle_LSB,#(-1000 & 255) ; angle=-1000
0068 002D
183 0069 0CFC MOV angle_MSB,#(-1000 >> 8)
006A 002E
The VIEW LIST for the code of "ELSEIF angle=-1000 THEN" is:
196 007B 0CE8 CJNE angle_LSB,#1000 & 255,@__ELSE_2
007C 008D 0743 0010 0A8B
197 0080 0C03 CJNE angle_MSB,#1000 >> 8,@__ELSE_2
0081 008E 0743 0010 0A8B
I know someone will be able to help us better understand this.
Gary
As a workaround, define a CON such as TestLow, giving it a value of -1000. Then use that CON name in the IF..ENDIF area. This makes the program function as desired now, but does not explain my earlier observation.
Gary
Here's what I'm trying
Am I doing something wrong?
· WORD variables can only hold positive values. When you do a comparison to a negative value, the compiler takes the "two's complient" of the negative value, and uses that. This works fine for "=" and "<>", but it won't work for ">" or "<".
· So when you say "IF var < -999" you are really saying "IF var < 64538" which isn't what you mean.
· One solution is to simply offset the value before doing the comparisons so everything is positive.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A government big enough to give you everything you want, is big enough to take away everything you·have."·· Thomas Jefferson
"It is our choices, Harry, that show what we truly are, far more than our abilities."·Dumbledore from Harry Potter
www.iElectronicDesigns.com
Post Edited (Bean (Hitt Consulting)) : 7/9/2008 11:04:42 AM GMT