Shop OBEX P1 Docs P2 Docs Learn Events
Most Significant Bits Set and Cleared — Parallax Forums

Most Significant Bits Set and Cleared

BrainStrainBrainStrain Posts: 30
edited 2013-07-02 23:43 in Robotics
In trying (and failing) to write SPIN code to get a Pololu 24v23 motor controller to run a motor, I have come across references and requirements concerning most significant bits (MSB) and least significant bits (LSB). I've seen references to MSBs and LSBs in my readings while boot-strapping myself up for SPIN, but I figured I would look into what this means when I needed to. Well, that time has come.

The Pololu PDF document Simple_Motor_Controlers.pdf, page 59, says, "Command bytes always have their most significant bits set, while data bytes almost always have their most significant bits cleared"

As strange as it might sound, my searches through the Parallax forums don't come up with any info for this. I don't claim to be a master search artist, but I try. Even stranger, Wikipedia and the Internet in general don't produce much for queries about setting an MSB, except for examples in the C programming language... I don't know C, and I can't see what they are doing code-wise or mathematically to make any sense out of it.

It is easy enough to understand what an MSB is. It is generally the leftmost bit in a binary number.

So, I ask, what does it mean to "set" an MSB?

What does it mean to "clear" an MSB?

I expect this has something to do with Pololu command byte decimal values ranging from 128 - 255, while data bytes range from 0-127. That gives the left-most digit of all command bytes a 1 and all data bytes a 0.

And, how is this done in SPIN, particularly regarding serial communications with a Pololu controller?

Annnd, the tiny bit of SPIN code I've seen does not transmit binary numbers, but Hex values. Pololu's own document shows some binary numbers, but mostly hex. So if anyone can navigate me through the issues of binary numbers with MSBs set and cleared, and how that translates to transmitting hex, that would be great!

I somewhat asked how to get a Prop to communicate with a Pololu controller in my Omni Wheel Configuration post, but I have not had any responses. It seems to me, just from info on the Pololu site, that their lack of SPIN code examples indicates that they are not putting out much effort to support SPIN. I hope I'm wrong. I have put a query into Pololu, but I expect they will respond with a policy that they don't write code for people. Whatever, please respond, if only to part of my query. I will greatly appreciate it.

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-06-30 19:42
    Annnd, the tiny bit of SPIN code I've seen does not transmit binary numbers, but Hex values. Pololu's own document shows some binary numbers, but mostly hex. So if anyone can navigate me through the issues of binary numbers with MSBs set and cleared, and how that translates to transmitting hex, that would be great!

    The Prop (and other uCs) only transmit binary. Hex and decimal numbers are ways of displaying the binary numbers to make them easier for us humans to read them.

    It seems you've got the LSB and MSB stuff figured out (what they are anyway).

    There's a variety of ways to set bits in Spin. If you look under "Bitwise operators" in the Propeller Manual index, you'll see list of bitwise operators. The shift operators and decode and encode operators are really useful.

    "Setting" a bit means setting it to one. "Clearing" a bit is setting it to zero.

    Here are a few forum threads about setting bits in Spin.

    Setting a bit.
    Clearing a single bit.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-06-30 21:28
    In your particular case, we're talking about setting the most significant bit of a byte or clearing the most significant bit of a byte. The value of that bit is 128 (2^7) so you can set it by adding 128 to the byte. Any data byte will have to be less than 128 (in the range 0-127) and will have the most significant bit cleared by its very nature (value). The easiest way to handle the command bytes is to include the 128 in their value, so, if a command to set the motor speed is 35, you'd define a constant "CON setSpeed = 35 + 128" and use the name "setSpeed" anywhere you need the command.
  • BrainStrainBrainStrain Posts: 30
    edited 2013-07-02 23:43
    Thank you very much guys. I'll go forth and see if I can figure this out.
Sign In or Register to comment.