User Manual
MCP Series
Brushed DC Motor Controllers
MCP Series User Manual
65
2.2.9 Handling values larger than a byte
Many Packet Serial commands require values larger than a byte can hold. In order to send or
receive those values they need to be broken up into 2 or more bytes. There are two ways this
can be done, high byte rst or low byte rst. Roboclaw expects the high byte rst. All command
arguments and values are either single bytes, words (2 bytes) or longs (4 bytes). All arguments
and values are integers (signed or unsigned). No oating point values (numbers with decimal
places) are used in Packet Serial commands.
To convert a 32bit value into 4 bytes you just need to shift the bits around:
unsigned char byte3 = MyLongValue>>24; //High byte
unsigned char byte2 = MyLongValue>>16;
unsigned char byte1 = MyLongValue>>8;
unsigned char byte0 = MyLongValue; //Low byte
The same applies to 16bit values:
unsigned char byte1 = MyWordValue>>8; //High byte
unsigned char byte0 = MyWordValue; //Low byte
The oposite can also be done. Convert several bytes into a 16bit or 32bit value:
unsigned long MyLongValue = byte3<<24 | byte2<<16 | byte1<<8 | byte0;
unsigned int MyWordValue = byte1<<8 | byte0;
Packet Serial commands, when a value must be broken into multiple bytes or combined from
multiple bytes it will be indicated either by (2 bytes) or (4 bytes).