High Byte, Low Byte Help
DiablodeMorte
Posts: 238
Hello all,
I'm working on a project which requires me to send the high and low bytes of a variable seperatly over a com port. I know how to send individual bytes via the com port but I don't know how to split an individual variable into high and low bytes. Before, with the basic stamp I think you use .HIGHBYTE and .LOWBYTE but that doesn't seem to exist in Spin.
Any suggestions?
I was thinking something like
var * %1100 but I'm not that proficient with spin.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Current Projects:
Robot Control Via Skype API - Dev Stage(50% Complete) - Total(25%)
Robot Localization Via Xbee's - Research Stage
IR Tracking with Propeller - Research Stage
I'm working on a project which requires me to send the high and low bytes of a variable seperatly over a com port. I know how to send individual bytes via the com port but I don't know how to split an individual variable into high and low bytes. Before, with the basic stamp I think you use .HIGHBYTE and .LOWBYTE but that doesn't seem to exist in Spin.
Any suggestions?
I was thinking something like
var * %1100 but I'm not that proficient with spin.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Current Projects:
Robot Control Via Skype API - Dev Stage(50% Complete) - Total(25%)
Robot Localization Via Xbee's - Research Stage
IR Tracking with Propeller - Research Stage
Comments
a:=Variable.Byte[noparse][[/noparse]3]
b:=Variable.Byte[noparse][[/noparse]2]
c:=Variable.Byte[noparse][[/noparse]1]
d:=variable.Byte[noparse][[/noparse]0]
Usual variables are longs, so have 4 bytes...
When I have a negative variable.. what do I get?
In the manual, if I wanted to express a -200 it tells me to put 255 as the high byte and 56 as the low byte... Why is that?
Also, if I use the above example: Which is the high byte? Which is the low byte?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Current Projects:
Robot Control Via Skype API - Dev Stage(50% Complete) - Total(25%)
Robot Localization Via Xbee's - Research Stage
IR Tracking with Propeller - Research Stage
The Propeller is little endian so the least significant byte occupies the lower address. (see page 332 of the manual)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
Post Edited (Paul Baker (Parallax)) : 4/9/2008 12:35:11 AM GMT
0=00000000_00000000_00000000_00000000
and
-1=11111111_11111111_11111111_11111111
A trick to get a negative value in two's compliment form is to invert the bits and add one. So the steps required are ...
I have the following function:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Current Projects:
Robot Control Via Skype API - Dev Stage(50% Complete) - Total(25%)
Robot Localization Via Xbee's - Research Stage
IR Tracking with Propeller - Research Stage
When including square brackets in the forum it's necessary to add spaces or the forum text parser treats them as formatting commands and what gets displayed isn't what's intended and can be confusing.
Yes, you can do that. Not sure the way you're doing waitcnt() is ideal but that's not the issue.
You don't need to worry about explicitly handling two's complementing here because where you specify "test(-495)" the compiler will have done the work for you; it will convert "-" and "495" into the required 32-bit two's complement number.
Fyi, the correct spelling is "two's complement". "Compliment" means something entirely different (and nonsensical in this context).
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Michael Park
PS, BTW, and FYI:
To search the forum, use search.parallax.com (do not use the Search button).
Check out the Propeller Wiki: propeller.wikispaces.com/
Here's what I call in my MAIN function:
and here's my drive function:
and here's the excerp from the manual I'm following:
When I run my code the little thing doesn't do anything, any suggestions as to what I'm doing wrong?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Current Projects:
Robot Control Via Skype API - Dev Stage(50% Complete) - Total(25%)
Robot Localization Via Xbee's - Research Stage
IR Tracking with Propeller - Research Stage
When including square brackets in the forum it's necessary to add spaces or the forum text parser treats them as formatting commands and what gets displayed isn't what's intended and can be confusing.
What you have looks like it should be correct. Maybe the delays between each byte are introducing a problem ? You can add some debugging code to show what you're sending. You could re-direct the serial to a terminal emulator which can show binary values of bytes sent and check that you are sending what you should be, at the right baud rate, correct polarity etc.
Added : Velocity[noparse][[/noparse] 1 ] and Velocity[noparse][[/noparse] 0 ] are not bytes, ditto for Radius[noparse][[/noparse] 1 ] and Radius[noparse][[/noparse] 0 ].
What do you mean? I am confused.. I'll try your suggestions of removing the pauses.
EDIT: Nope, I removed the pauses and I got nothing.. Any other suggestions?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Current Projects:
Robot Control Via Skype API - Dev Stage(50% Complete) - Total(25%)
Robot Localization Via Xbee's - Research Stage
IR Tracking with Propeller - Research Stage
Post Edited (DiablodeMorte) : 4/10/2008 1:15:37 AM GMT
Note: the code works fine, I can get the robot to sing and do demos, I just can't seem to get it to move under my control.
EDIT: I just tried manually this code:
and it works fine. My problem must be with the high byte low byte thing. Hope that is more helpful!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Current Projects:
Robot Control Via Skype API - Dev Stage(50% Complete) - Total(25%)
Robot Localization Via Xbee's - Research Stage
IR Tracking with Propeller - Research Stage
Apologies, that was a bit terse.
var[noparse]/noparse is an array of long, var.byte[noparse]/noparse selects a byte of the long, so as in you earlier example where you were using the later to get at the bytes you need to do the same here ...
PUB DriveDirect(RightVelocity, LeftVelocity)
if m_Init
Link.tx(Cmd_DriveWheels)
Link.tx(~~RightVelocity >> 8)
Link.tx(RightVelocity)
Link.tx(~~LeftVelocity >> 8)
Link.tx(LeftVelocity[noparse][[/noparse]0])
Anybody want to explain that to me?. I kind get the >> 8.. but not really, I'm looking up the ~~ right now but doubt i'll understand it.
Ok, yep, I get the >> part.. Still don't really see why ~~ is needed...Answers anyone?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Current Projects:
Robot Control Via Skype API - Dev Stage(50% Complete) - Total(25%)
Robot Localization Via Xbee's - Research Stage
IR Tracking with Propeller - Research Stage
Post Edited (DiablodeMorte) : 4/10/2008 3:19:37 AM GMT
The "& $FF" is important but it is not always necessary - as with calling link.tx() - if that truncates the output to 8-bit bytes.
The ~~ is used to sign extend a 16-bit value to fill the entire 32-bits of a long variable. As the code only deals with 16-bit values it is pointless to do in this case.