This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to Split the 16bit word?

Hi! i want to spilt the 16bit word into two 8bit words,
is there any key words for it. i use the p89V51 mcu and c language Cx51 keil compiler ver3.

regards,
K.T.Venkatesan.

Parents
  • Code generated by Keil C51 v8 (or v7) often benefits greatly from the union method.

    You can isolate the byte-order dependency with variant declarations controlled by a #define. In fact, if you're that concerned with portability, you're going to need a section of several such macros.

    typedef union
        {
        U16 u16;
    #if defined(CPU_ENDIAN_BIG)
        struct { U8 msb; U8 lsb; } bytes;
    #elif defined(CPU_ENDIAN_LITTLE)
        struct { U8 lsb; U8 msb; } bytes;
    #else
    #error Must define CPU_ENDIAN_BIG or _LITTLE
    #endif
        } MultiByte16;
    

    Bitfields can be assigned from the MSB or LSB at the implementation's whim, so you also need a "bitfield endianess" as well as a multi-byte-word endianenss.

Reply
  • Code generated by Keil C51 v8 (or v7) often benefits greatly from the union method.

    You can isolate the byte-order dependency with variant declarations controlled by a #define. In fact, if you're that concerned with portability, you're going to need a section of several such macros.

    typedef union
        {
        U16 u16;
    #if defined(CPU_ENDIAN_BIG)
        struct { U8 msb; U8 lsb; } bytes;
    #elif defined(CPU_ENDIAN_LITTLE)
        struct { U8 lsb; U8 msb; } bytes;
    #else
    #error Must define CPU_ENDIAN_BIG or _LITTLE
    #endif
        } MultiByte16;
    

    Bitfields can be assigned from the MSB or LSB at the implementation's whim, so you also need a "bitfield endianess" as well as a multi-byte-word endianenss.

Children