Using high and low bytes, how do I set the number 512?

Does anybody know how to set the number 512 using high and low bytes? I'm working with dynamixels, which are basically servos with their own microcontroller, and I'm trying to set the moving speed to 512. To set it, I'm suppose to do something like this.

ax12SetRegister(15, 32, ?);//15 is the id of the servo, 32 is the low byte of where it stores the moving speed, and '?' is what I what I'm trying to figure out

ax12SetRegister(15, 33, ?);//33 is the high byte

Thanks.

int valueToSet = 512;

  ax12SetRegister(15, 32, valueToSet & 0xFF); // low
  ax12SetRegister(15, 33, valueToSet >> 8);   // high

The number (512)base 10 = (0000 0010 0000 0000)base 2

So you want 0000 0010 for the most significant byte and 0000 0000 for the least significant byte

What whandall offers is a generic method that will work with any value fitting in an int. I would just advise to declare valueToSet as unsigned int rather than an int to avoid any issue with negative numbers when shifting the high bits right

Thanks guys :) I ended up doing this:

ax12SetRegister2(15, 32, 512);//notice the 2 after 'ax12SetRegister'

because for some reason, it didn't work when I tried your guy's ways. It might of been the way I did it. I don't know. But thanks for your guy's help :) I appreciate it a lot

So you mislead us with your question :)

Remember source code is often the ultimate documentation - So out of curiosity I went have a look.

This is the definition and code of the functions

/* Set the value of a [color=red]single-byte register[/color]. */
void ax12SetRegister(int id, int regstart, int data);

/* Set the value of a [color=red]double-byte register[/color]. */
void ax12SetRegister2(int id, int regstart, int data)

So seems the first function you indicated is only meant for [u]single byte registers[/u] while the second one is meant for [u]double bytes registers[/u] and so what you indicate is that it's not possible probably to write directly at the second byte of a 2 byte register, hence the need for the ax12SetRegister[color=blue]2[/color]() function

/* Set the value of a single-byte register. */
void ax12SetRegister(int id, int regstart, int data){
    setTX(id);    
    int checksum = ~((id + 4 + AX_WRITE_DATA + regstart + (data&0xff)) % 256);
    ax12writeB(0xFF);
    ax12writeB(0xFF);
    ax12writeB(id);
    ax12writeB(4);    // length
    ax12writeB(AX_WRITE_DATA);
    ax12writeB(regstart);
    ax12writeB(data&0xff);
    // checksum = 
    ax12writeB(checksum);
    setRX(id);
    //ax12ReadPacket();
}
/* Set the value of a double-byte register. */
void ax12SetRegister2(int id, int regstart, int data){
    setTX(id);    
    int checksum = ~((id + 5 + AX_WRITE_DATA + regstart + (data&0xFF) + ((data&0xFF00)>>8)) % 256);
    ax12writeB(0xFF);
    ax12writeB(0xFF);
    ax12writeB(id);
    ax12writeB(5);    // length
    ax12writeB(AX_WRITE_DATA);
    ax12writeB(regstart);
    ax12writeB(data&0xff);
    ax12writeB((data&0xff00)>>8);
    // checksum = 
    ax12writeB(checksum);
    setRX(id);
    //ax12ReadPacket();
}

You will notice that in the first function they send only the least significant byte of the data by masking data with 0xFF

    ax12writeB(data[color=red]&0xff[/color]);

while in the second function they send both bytes of the data in a way similar to what Whandall suggested by first sending the least significant byte of the data by masking data with 0xFF and then the most significant byte of the data by masking data with 0xFF00 (so keeping only the high part of the int) and then shifting right by 8 positions, moving the most significant byte into the least significant part .

    ax12writeB(data[color=red]&0xff[/color]);
    ax12writeB((data[color=red]&0xff00[/color])[color=red]>>8[/color]);