This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Extract bytes out of word

Other Parts Discussed in Thread: CONTROLSUITE

Hi all, 

I'm struggling to move some code from PIC24 to the f28069. 

As the c28x is 16bit, all the definitions I was expecting to be 8 bits, are actually 16...

I have the following union. How could I define this in order to be able to extract the 2 bytes from the word? 

Thank you for your time. 

typedef union

{

    unsigned short    Word;

    unsigned char    Byte[2];

} BTW;

  • Hey Emanuel,

    Check out the header files in ControlSUITE under controlSUITE\device_support\f2806x\vXXX\F2806x_headers\include.  I think they are doing something similar to what you want. For example inside of F2806x_Adc.h:

    struct ADCCTL2_BITS { // bits description
        Uint16 CLKDIV2EN:1; // 0 ADC CLK Div2 enable
        Uint16 ADCNONOVERLAP:1; // 1 Non Sample Overlap enable
        Uint16 CLKDIV4EN:1; // 2 ADC CLK Div4 enable
        Uint16 rsvd1:13; // 15:3 Reserved
    };

    union ADCCTL2_REG {
        Uint16 all;
        struct ADCCTL2_BITS bit;
    };

    So I think if you do something like:

    struct  def_of_bytes { 
        Uint16 byte1:8; 
        Uint16 byte2:8; 
    };

    union name_of_register {
        Uint16 all;
        struct def_of_bytes byte;
    };

    Then you can do:

    Uint16 var1 = name_of_register.all;

    or

    Uint16 var1 = name_of_register.byte.byte1;

    or

    Uint16 var1 = name_of_register.byte.byte2;

    Let me know if this works for you.

  • Emanuel Eni said:

    I'm struggling to move some code from PIC24 to the f28069. 

    As the c28x is 16bit, all the definitions I was expecting to be 8 bits, are actually 16...

    I have the following union. How could I define this in order to be able to extract the 2 bytes from the word? 

    In addition to Devin's suggestion - there are some compiler intrinsics to help with byte processing on a 28x.  Please see this wiki for some details:

    http://processors.wiki.ti.com/index.php/Byte_Accesses_with_the_C28x_CPU

    Happy Coding :)

    Lori

  • Devin, Thank, but that didn't seem to be helpful for me, at least I couldn't make use of it. 

    Lori, generally when people came to e2e with questions which aren't too complicated it's because they are beginners (like me) or have searched the entire forum for the solution. 

    You have posted this exact same answer everywhere people tried to do something similar to what I want, why do you think I started a new topic when I could search the forum? This isn't helpful. 

    How I managed to solve it is: 

    struct CHAR_WORDS {
    unsigned int B1:8;
    unsigned int B2:8;
    } ;
    typedef union
    {
    unsigned int Word;
    struct CHAR_WORDS Byte;
    } UBYTETOWORD;