I am trying to write a code to control a motor controller using the CanOpen protocol.
For the motor command I am using a pot that is going to mapped like this:
0, 1023, -1000, 1000
Being -1000 full reverse and 1000 full forward.
Now I have to convert the command value into an hex value, if we take the example of the full reverse value (-1000) into hex it will be something like 0xFFFFFC71, so far so good, now I have to take that hex value and seperate it into byte values because it has to be sent like this:
0x71 0xFC 0xFF 0xFF (byte4, byte5, byte6, byte7)
If it is the full forward (0x3E8) it will be like this:
0xE8 0x03 0x00 0x00
How can I do it seperate the long hex value into seperate bytes?
Any example code I can use?
Thank you for your code groundfungus, it almost worked well until I tested with a shorter hex value like "0x3E8", it gave me this result:
long version 3E8
byte 0 = E8
byte 1 = 3
byte 2 = 0
byte 3 = 0
When I should have this:
long version 3E8
byte 0 = E8
byte 1 = 03
byte 2 = 00
byte 3 = 00
Any suggestion of what must be changed on the code to get the right values?