C Programming: Splitting 16 Bit Word into Bytes

  • Thread starter Thread starter Averagesupernova
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around how to split a 16-bit word into two 8-bit bytes in C programming, specifically for firmware development on a PIC microcontroller that communicates with an 8-bit wide data bus. Participants explore methods for achieving this, including bitwise operations and function design.

Discussion Character

  • Technical explanation, Exploratory, Homework-related

Main Points Raised

  • One participant seeks advice on efficiently splitting a 16-bit word into bytes in C, expressing familiarity with BASIC but not with C.
  • Another participant suggests using bit arithmetic, providing examples of how to manipulate bytes using bitwise operators like << and &.
  • A different participant proposes creating a function to send two bytes at a time, indicating that using a char type in C simplifies the process of handling bytes.
  • A later reply clarifies the specific use case of sending a 2-byte word in iterations, detailing the sequence of sending the least and most significant bytes.
  • One participant expresses gratitude for the advice received and notes a misunderstanding from another participant regarding their original question.

Areas of Agreement / Disagreement

Participants generally agree on the use of bitwise operations for splitting bytes, but there is some uncertainty regarding the understanding of the original question and the specific implementation details.

Contextual Notes

Some assumptions about the data types and operations in C may not be fully articulated, and the discussion does not resolve all potential methods for splitting bytes or sending data.

Averagesupernova
Science Advisor
Gold Member
Messages
4,914
Reaction score
1,564
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
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.
 
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?
 
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.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • Sticky
  • · Replies 13 ·
Replies
13
Views
8K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
8
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K