quick question about IF...THEN and negative values
kingspud
Posts: 128
Hello all,
In the following code I have an IF...THEN statement that
button_press:
· BUTTON 12,0,255,0,btn,1,press·· ' button [noparse][[/noparse] subtract 20 ]
GOTO button_press
press:············· 'Press button [noparse][[/noparse]1] on PIN 5 and goto Begin_301
··· score = score - 20
····· DEBUG SDEC score, CR···
··· IF score < 0 THEN
···· GOTO reset
··· ENDIF
··· GOSUB DecodeMode
··· Digit0 = score DIG 0 + $0100
··· Digit1 = score DIG 1 + $0200
··· Digit2 = score DIG 2 + $0300
··· GOSUB board_display:
··· GOTO button_press
DecodeMode:
· LOW Load
· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$090F\16]
· HIGH Load
· RETURN
board_display:
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit0\16]
··· HIGH Load
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit1\16]
··· HIGH Load
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit2\16]
··· HIGH Load
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit3\16]
··· HIGH Load
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit4\16]
··· HIGH Load
··· PAUSE 250
RETURN
I have a button that when pressed it subtracts 20 from a score of 301 and shows it on the screen and three 7-segment displays
as you continue the press the button the score gets lower by 20 until it gets to 1 and the next time you press the button
the screen quickly shows -19 and then goes to a value of 65517!
I'm not sure what is wrong?
score is set to "word" size
Thanks for any help
Joe
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“Intellectual growth should commence at birth and cease only at death”
Albert Einstein
In the following code I have an IF...THEN statement that
button_press:
· BUTTON 12,0,255,0,btn,1,press·· ' button [noparse][[/noparse] subtract 20 ]
GOTO button_press
press:············· 'Press button [noparse][[/noparse]1] on PIN 5 and goto Begin_301
··· score = score - 20
····· DEBUG SDEC score, CR···
··· IF score < 0 THEN
···· GOTO reset
··· ENDIF
··· GOSUB DecodeMode
··· Digit0 = score DIG 0 + $0100
··· Digit1 = score DIG 1 + $0200
··· Digit2 = score DIG 2 + $0300
··· GOSUB board_display:
··· GOTO button_press
DecodeMode:
· LOW Load
· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$090F\16]
· HIGH Load
· RETURN
board_display:
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit0\16]
··· HIGH Load
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit1\16]
··· HIGH Load
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit2\16]
··· HIGH Load
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit3\16]
··· HIGH Load
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit4\16]
··· HIGH Load
··· PAUSE 250
RETURN
I have a button that when pressed it subtracts 20 from a score of 301 and shows it on the screen and three 7-segment displays
as you continue the press the button the score gets lower by 20 until it gets to 1 and the next time you press the button
the screen quickly shows -19 and then goes to a value of 65517!
I'm not sure what is wrong?
score is set to "word" size
Thanks for any help
Joe
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“Intellectual growth should commence at birth and cease only at death”
Albert Einstein
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
' {$STAMP BS2px}
' {$PBASIC 2.5}
'
[noparse][[/noparse] I/O Definitions ]
'0 sets pin as input
'button PIN 0 makes the word button = pin 0
DIRS = $000E 'pins 1 2 3 output,all other pins outputs
DOUT PIN 1 'pin 1 of stamp to pin 1 of 7219
Load PIN 2 'pin 2 of stamp to pin 12 of 7219
CLK PIN 3 'pin 3 of stamp to pin 13 of 7219
'
[noparse][[/noparse] Constants ]
IsLow CON 0
IsHigh CON 1
'
[noparse][[/noparse] Variables ]
Digit0 VAR Word
Digit1 VAR Word
Digit2 VAR Word
games VAR Byte
Value VAR Nib
score VAR Word
player VAR Byte
change VAR Byte
'
[noparse][[/noparse] Variables ]
btn VAR Bit
total VAR Byte
temp VAR Byte
'
[noparse][[/noparse] Initialization ]
Digit0 = $010F
Digit1 = $020F
Digit2 = $030F
Value = 0
total = 0
games = 0
change = 0
score = 301
'
[noparse][[/noparse] Main Program ]
main:
GOSUB DecodeMode
GOSUB ShutDownMode
GOSUB ScanLimit
GOSUB Intensity
'----[noparse][[/noparse] stuff ]
button_press:
BUTTON 12,0,255,255,btn,1,press ' button [noparse][[/noparse] 20 ]
DEBUG DEC score," ",CR
GOTO button_press
'
[noparse][[/noparse] Subroutines ]
press: 'Press button on PIN 5 and goto Begin_301
score = score - 20
DEBUG SDEC score, CR
IF score < 0 THEN
GOTO reset
ENDIF
'GOSUB DecodeMode
Digit0 = score DIG 0 + $0100
Digit1 = score DIG 1 + $0200
Digit2 = score DIG 2 + $0300
GOSUB board_display:
GOTO button_press
GOTO button_press
'
[noparse][[/noparse] GOSUB RESET ]
reset:
score = 0
Digit0 = $010F
Digit1 = $020F
Digit2 = $030F
GOSUB board_display:
GOTO reset
'
[noparse][[/noparse] GOSUB BEGIN_401 ]
'
[noparse][[/noparse] Subroutines ]
'Decode mode for no decoding to BCD
'Least sig nibble:
'0 = no decode
'1 = decode Digitit 0
'F = decode Digitits 0-3
'FF = decode Digitits 0-7
DecodeMode:
LOW Load
SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$090F\16]
HIGH Load
RETURN
'Set shutdown mode so device is active
ShutDownMode:
LOW Load
SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$0C01\16]
HIGH Load
RETURN
'Set scan limit mode for three Digitits
ScanLimit:
LOW Load
SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$0B02\16]
HIGH Load
RETURN
'Set intensity of LEdDs
'LSNibble 0 = min intensity and F = max intensity
Intensity:
LOW Load
SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$0A09\16]
HIGH Load
RETURN
board_display:
LOW Load
SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit0\16]
HIGH Load
LOW Load
SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit1\16]
HIGH Load
LOW Load
SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit2\16]
HIGH Load
PAUSE 250
RETURN
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“Intellectual growth should commence at birth and cease only at death”
Albert Einstein
Although -99 is obviously less than 100, the program will say it is greater. The problem is that -99 is internally represented as the two's complement value 65437, which (using unsigned math) is greater than 100. This phenomena will occur whether or not the negative value is a constant, variable or expression.
Maybe its just me but this still doesn't help me and I'm not sure how to check for a number less than 0!
Anyone?????
Joe
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“Intellectual growth should commence at birth and cease only at death”
Albert Einstein
In your case button is on pin 12 (not 5) and btn needs to be a byte variable (not bit) and cleared before use.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
In addition to what Stephen found, the following routine is an infinite loop:
reset:
score = 0
Digit0 = $010F
Digit1 = $020F
Digit2 = $030F
GOSUB board_display:
GOTO reset
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I know the loop is infinite but it is only for a test program and it gets hit once so it will stop the program.
The main issue is the negative number and why I can't seem to see it in the IF statement.
This is what I need help on!
Can we focus on this issue please.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“Intellectual growth should commence at birth and cease only at death”
Albert Einstein
Now, "2's complement" format (the one the BS2 uses) inverts all the bits and adds 1.
So, for example, the value 10. In binary, this is 0000 1010. To get negative 10, we invert -- 1111 0101, then add 1 -- 1111 0110. And in hexadecimal, 1111 0110 is $F6, and in decimal 246.
Now, you're doing "Word" quantities (16 bits) so -10 would be $FFF6.
The bottom line of this example is -- positive numbers go from 0 to 65535. Negative numbers go from -32768 to 0 to 32767. And negative numbers always have the most significant bit set.
So, if you know you're doing signed math, you can check if the most significant bit is set on a number -- that means it's negative.
If you want to display the negative numbers on the debug screen, just use
DEBUG SDEC score ' signed decimal
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
Forgive me for my brash response; I didn't realize that people would have a MAX7219 chip, the Professional Development board and a Basic Stamp 2 chip... I just thought people or administrators would be able to quickly answer the question without running the code.
Sometimes I get caught up in the coding and I hit a wall that stops my momentum right in my tracks… At these times I just ask a quick question hoping for a fix or a sample code from someone. I forget that people may want to try the code out for themselves.
I have to keep this in mind!!!!
I agree you Stephen, I should have produced a complete and correct program before asking for help.
This was my bad and I will not do it in the future.
Thanks for everyone’s help and I do appreciate it!
Joe
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“Intellectual growth should commence at birth and cease only at death”
Albert Einstein