"Is there some kind of calculator I could use that would take a number larger than 256 and break it into bytes?"
The '/' for division and the '&' are valid to use in the Python code , and as long as you define you Number as a WORD in PBASIC you can directly derive the BYTEs making up the WORD variable.... i.e.
For W0 you would use B0 and B1
for W1 you would use B2 and B3
for W2 you would use B4 and B5
... and so on...
Short of that if you really want a calculator , the calculator in Windows will convert to Binary if you switch it to Scientific mode. .... View --> Scientific ... The radio buttons on the upper left of the Calculator window allow you to select between Hex Dec Oct and Bin
I think I'm starting to grasp it a little. The Windows calculator lets me enter a WORD and then convert it to a BYTE.
So using an & is like dividing a number by another, and then leaving only a value below 256.
After playing around with some numbers in the calculator I noticed something strange. Any number below 256 is displayed as 0 when I convert it to a BYTE. Once I reach 256, it shows a 1 and then starts counting up by 1. That part makes sense. When I enter 383 it shows 127 and then 384 is -128. 385 is -127 and so on until it reaches 0 and then starts counting up into the positive. I know this is not really relevant to my project, but what is the reason for this behavior?
"The Windows calculator lets me enter a WORD and then convert it to a BYTE." - There is no conversion from WORD to BYTE, what;s happening is that the HIGH byte is truncated .... similar to using the '&'
"...high byte 2 and low byte 238" are correct for 750. You can always double check your answer by multiplying the HIGH byte by 256 and then adding the LOW byte.
Would this work? It looks like there are two different loops in this code, which would seem to be a problem. The tutorial I got the code from indicated that there needs to be a loop for the servo to maintain its position. But how do I do that when I also need the stamp to be continuously requesting serial input?
The DO LOOP is not available for the BS1, only the BS2, so those two lines need to be removed.
' {$STAMP BS1}
' {$PBASIC 1.0}
SYMBOL SIn = 0
SYMBOL Baud = N2400
SYMBOL Result = W0 ' B1 = High Byte of W0 ; B0 = Low Byte of W0
'Note: In order for the servos to update properly, the PC needs to send a continuous data
' stream which includes the SYNC bytes and data bytes.
Main:
SERIN SIn, Baud, ("ABCD"), B1 , B0 'At 2400 Baud, this takes 2.5ms Min to 5ms Max
'' 1/2400 x 6 BYTES = 2.5ms ... if the interval is missed
'' the delay takes at MAX a complete additional cycle, thus
'' 5ms
DEBUG #Result, CR 'At 4800 Baud, this can take about 1ms
''4800 is the default serial speed for the BS1
'' 1/4800 x 5 characters = 1.05ms
'' Since the DEBUG is sent with a #, each character is sent
'' as a BYTE. This encompasses a value ranging from
'' 1000 TO 2000 plus a return character 'CR'
Result = Result / 10 'The BS1 can only resolve an increment of 10us for PULSOUT
PULSOUT 0, Result 'Send Pulse .. can take from 1ms to 2ms
'PAUSE 20 'The average time required by the SERIN/DEBUG/PULSOUT
PAUSE 14 'and general overhead is about 6ms, so instead of a
'PAUSE 20, a PAUSE 14 will suffice
GOTO Main
END
Comments
The purpose of this project is simply to control a servo with a maximum of 90° of rotation. So I only need to send values between 250 and 750.
Is there some kind of calculator I could use that would take a number larger than 256 and break it into bytes?
The '/' for division and the '&' are valid to use in the Python code , and as long as you define you Number as a WORD in PBASIC you can directly derive the BYTEs making up the WORD variable.... i.e.
For W0 you would use B0 and B1
for W1 you would use B2 and B3
for W2 you would use B4 and B5
... and so on...
Short of that if you really want a calculator , the calculator in Windows will convert to Binary if you switch it to Scientific mode. .... View --> Scientific ... The radio buttons on the upper left of the Calculator window allow you to select between Hex Dec Oct and Bin
So using an & is like dividing a number by another, and then leaving only a value below 256.
After playing around with some numbers in the calculator I noticed something strange. Any number below 256 is displayed as 0 when I convert it to a BYTE. Once I reach 256, it shows a 1 and then starts counting up by 1. That part makes sense. When I enter 383 it shows 127 and then 384 is -128. 385 is -127 and so on until it reaches 0 and then starts counting up into the positive. I know this is not really relevant to my project, but what is the reason for this behavior?
I'm glad I was able to figure this out, even though I now see that the code you gave me would automatically take care of it.
The negative behavior is how binary numbers represent a value in " 2's complement " ... See: http://en.wikipedia.org/wiki/Two%27s_complement
"...high byte 2 and low byte 238" are correct for 750. You can always double check your answer by multiplying the HIGH byte by 256 and then adding the LOW byte.
2 * 256 + 238 = 750
If I have the following code, how should I modify it, and where would I place it in relation to my existing PBASIC code?
DO
PULSOUT 0,500
PAUSE 20
LOOP
' {$PBASIC 1.0}
SYMBOL SIn = 0
SYMBOL Baud = N2400
SYMBOL Result = W0 ' B1 = High Byte of W0 ; B0 = Low Byte of W0
Main:
SERIN SIn, Baud, ("ABCD"), B1 , B0
DEBUG #Result, CR
DO
PULSOUT 0, #Result
PAUSE 20
LOOP
GOTO Main
END
Would this work? It looks like there are two different loops in this code, which would seem to be a problem. The tutorial I got the code from indicated that there needs to be a loop for the servo to maintain its position. But how do I do that when I also need the stamp to be continuously requesting serial input?
Also, with the code you just shared, the servo should maintain its position? It is still part of a loop?