Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Question about C

  1. Jan 2, 2008 #1
    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?
  2. jcsd
  3. Jan 3, 2008 #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.
  4. Jan 3, 2008 #3


    User Avatar

    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)?

    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?
  5. Jan 3, 2008 #4
    Thanks alot 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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Question about C
  1. Questions about C (Replies: 7)