C Programming: Splitting 16 Bit Word into Bytes

  • Thread starter Thread starter Averagesupernova
  • Start date Start date
Click For Summary
The discussion focuses on using a C compiler to develop firmware for a PIC microcontroller that communicates with a device via an 8-bit data bus, requiring the handling of 16-bit words in two steps. The key method for splitting the upper and lower bytes involves bit arithmetic, specifically using bitwise operators like << (left shift) and & (bitwise AND). The process starts by ensuring the lower byte is clean and then combines the bytes into a 16-bit variable. A suggested function for sending data is proposed, which takes two bytes as input. The conversation also emphasizes that working with unsigned integers and iterating through values requires sending the least significant byte first, followed by the most significant byte, along with a single-byte instruction for the device. The importance of understanding bitwise operations in microcontroller programming is highlighted as essential for effective data manipulation.
Averagesupernova
Science Advisor
Gold Member
Messages
4,834
Reaction score
1,508
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

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