# Thread: How to split an int, into 2-Bytes???? Controlling PWM with an int.

1. ## How to split an int, into 2-Bytes???? Controlling PWM with an int.

Hey guys.
Im using some motors which run off PWM pins.
Theres a High byte and Loq byte register (PWMH,PWML).

I have an int which i need to put into these registers but i dont know how???
so for example
int:84 -> PWMH=0x00 , PWML=0x54
int:310 -> PWMH=0x01 , PWML=0x36
int:11588 -> PWMH=0x2D , PWML=0x44

Appreciate any help

2. You can use bit-wise operators to mask the upper and lower bytes separately and assign the results to your registers (the masked upper byte would require a shift to get the bits in the correct position).

3. Expanding on Matticus's post, which really has enough information, but anyway...

Given the (base 10) number 11588, this is 0x2D44 when represented in hexadecimal. When the number is expressed using hexadecimal the operations you need to undertake become much clearer (in my opinion anyway). One hex digit is 4-bits, and 2 hex digits are 4 bits (this never changes and is why hex <--> binary conversions can easily be done in your head).

Okay, so you need to split 0x2D44 into two "bytes": 0x2D and 0x44

The numbers are "combined", though: 0xhhll where hh is the "high byte" and ll is the "low byte".

To get the low byte, you mask the bits that you're not interested in using a bitwise AND; i.e. anything other than the 'll' bits (0xFF) you want to get rid of.

The high byte is, well, those in the 'hh' position so mask with 0xFF00. They are in the wrong position, though... so you need to shift them to the right. I'll leave it you to work out the number of right shifts required because there is more than enough position in this post to determine that.

Just a quick note, I am using the word "byte" very liberally when I've used it in this comment. In C a char is always 1 byte. But be careful because the number of bits in the byte is not necessarily 8 (the number of bits must be >= 8)