C Programming: Splitting 16 Bit Word into Bytes

  • Thread starter Averagesupernova
  • Start date
In summary, the C programmer needs to be able to split up the upper and lower bytes of a hex value in order to send it to a device with an 8 bit wide data bus. This can be done with bit shift operators, as well as a function that accepts a 2 byte word.
  • #1
Averagesupernova
Science Advisor
Gold Member
4,460
1,234
I am using a C compiler to develop firmware for a PIC microcontroller. This microcontroller needs to comunicate with a device with an 8 bit wide data bus. However, the device takes in 16 bit words in 2 consecutive steps. I need to be able to split up the upper and lower bytes in my program. How is this done easily? I'm familiar with BASIC and the hex() function and all the string manipulations/conversions to/from decimal, etc. but new to C. Is there an easier way than dumping the 16 bit hex word into memory and then picking it out one byte at a time? I believe C allows this and I may have to resort to it. Any advice?
 
Technology news on Phys.org
  • #2
Well... usually the way people would do this is with bit arithmetic. Let's say that you have:

uint16_t twobytes;
uint16_t onebyte;

...and twobytes is where you eventually want your stored value to be, and onebyte is one of the 8 bit words you have just taken in.

You can first say:

onebyte = onebyte & 0xFF;
twobytes = 0;

Just to make certain that twobytes is clean, and only the low-order bits are occupied in onebyte. Then you can say:

twobytes = twobytes | onebyte;

to copy onebyte into the low-order byte of twobytes, or

twobytes = twobytes | (onebyte << 8);

to copy onebyte into the high-order byte of twobytes.

You would probably be well-served by searching on google for "bitwise arithmetic" and seeing what comes up. If you're going to be doing much microcontroller programming then you are going to be spending a *LOT* of time with the << and & operators.
 
  • #3
the device takes in 16 bit words in 2 consecutive steps

So, you are going to send 2 bytes at a time to your device, right?

How about devising a function like: int send_data(char byte0, char byte1)?

I need to be able to split up the upper and lower bytes in my program.

If you work in C, utilizing a variable of type char already implies one byte of data.
So, I guess, there is no difficulty of splitting bytes anymore, right?
 
  • #4
Thanks a lot Coin. The bit shift operators will work perfectly.
-
For the record, I am starting with a 2 byte word (unsigned int) and doing a number of iterations that will increment it. This 2 byte word needs to be sent out in 2 consecutive steps on an 8 bit data bus each iteration. So for example, I'm starting out with 0x1C00 and ending with 0x1FFF. First step is to send the the least significant byte (0x00) then the most significant byte (0x1C). Then a single byte instruction is sent to tell the device what to do with this data. Then the loop starts over and the least significant byte is sent (this time 0x01) then the next byte is sent (0x1C). Again a single byte instruction is sent out and it happens over and over until 0x1FFF has been sent out.
-
Eus, I'm not sure you fully understood what I wanted to do. Just thought I'd let you know. Thanks again.
 

Related to C Programming: Splitting 16 Bit Word into Bytes

1. How do you split a 16 bit word into bytes in C programming?

To split a 16 bit word into bytes in C programming, you can use bitwise operators and bit shifting. First, you can use the bitwise AND operator to isolate the first byte by performing a logical AND operation with the original 16 bit word and a mask value of 0xFF (11111111 in binary). Then, you can use the right bit shift operator to shift the remaining bits to the right by 8 places, leaving you with the second byte.

2. What is the purpose of splitting a 16 bit word into bytes in C programming?

Splitting a 16 bit word into bytes in C programming allows for more efficient memory usage and manipulation. It also allows for easier access to specific parts of the 16 bit word, as each byte can be accessed separately.

3. How can you combine two bytes into a 16 bit word in C programming?

To combine two bytes into a 16 bit word in C programming, you can use the bitwise OR operator to combine the two bytes into one 16 bit value. First, you can use the left bit shift operator to shift the first byte to the left by 8 places. Then, you can use the bitwise OR operator to combine the shifted byte with the second byte.

4. What is the difference between little endian and big endian in relation to splitting a 16 bit word into bytes in C programming?

Little endian and big endian are two different ways of storing data in memory. In little endian, the least significant byte is stored in the lower memory address, while the most significant byte is stored in the higher memory address. In big endian, the most significant byte is stored in the lower memory address, while the least significant byte is stored in the higher memory address. When splitting a 16 bit word into bytes in C programming, the order of the bytes will depend on the endianness of the system.

5. Are there any built-in functions in C programming to split a 16 bit word into bytes?

No, there are no built-in functions in C programming specifically for splitting a 16 bit word into bytes. However, as mentioned earlier, you can use bitwise operators and bit shifting to achieve this task. Additionally, some compilers may have built-in functions or libraries that can assist with splitting a 16 bit word into bytes.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
7
Views
3K
  • Sticky
  • Programming and Computer Science
Replies
13
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Electrical Engineering
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Science and Math Textbooks
Replies
1
Views
962
  • Electrical Engineering
Replies
6
Views
1K
Back
Top