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.

TMS320F280049C: How to split float into 8-bit chunks and vice versa

Part Number: TMS320F280049C

How to split  float into 8-bit chunks and combine 8-bit chunks into float? I want to save float variables in EEPROM and send float in CAN. Also, I want to combine 8-bit chunks from EEPROM and CAN RX packets and recombine to update float variables.

  • Hi Arpan,

    C28x does not directly support an 8-bit data type and smallest unit is 16-bit data.  A float variable is represented as a 32-bit data following IEEE754 standard.  What you can do is assign 16-bit data type (char) to hold the value of the 8-bit data that you intend to transmit as CAN data. You would need to do some pointer assignments and bit manipulation to deconstruct a float data type into something that you can transmit as CAN data and do the reverse when assembling CAN data as float.  See attached example code using the values of pi as the floating point data.

        float pival, flt;
        char *cdata,candata[8];
    
        pival = 3.1415926;
    
        // 1. de-construct float data into 8-bit segments for CAN transmission
        cdata = (char *)&pival;
        candata[0]=cdata[0]&0xff;
        candata[1]=cdata[0]>>8;
        candata[2]=cdata[1]&0xff;
        candata[3]=cdata[1]>>8;
    
        // 2. build float data from 8-bit CAN segments
        cdata[0] = candata[0] + (candata[1]<<8);
        cdata[1] = candata[2] + (candata[3]<<8);
    
        flt = *(float *)&cdata[0];
    

    Hope this helps.

    Regards,

    Joseph

  • Hi Joseph, Thank you so much for the detailed answer. Is there any use of __byte() as an alternate solution?

  • Hi Arpan,

    __byte() or bp_16 typedef still evaluates as 16-bit data.  You would still need to do some bit manipulation and store it as char (again treated as16-bit data by the F280049 device).

    Regards,

    Joseph

  • Hi Joseph,

    I couldn't understand the difference.

  • Hi Joseph,

    This code is working only if  chunkdata starting address is even. If &chunkdata[0] is odd, the starting address is being decremented by 1 and it is combining wrong set of bytes into float. 

    char chunkdata[2] = {0};
    uint16_t array_read[20] = {...};//initialized with some values
    float32_t var;
    
    chunkdata[0] = array_read[OFFSET] + (array_read[OFFSET+1] << 8);
    chunkdata[1] = array_read[OFFSET+2] + (array_read[OFFSET+3] << 8);
    var = *(float_t *)(&chunkdata[0]);

  • Hi Joseph,

    Changing to the code below solved the problem I was facing. But this code is violating MISRA C. What to do?

    float fvar;
    char * chunkdata
    chunkdata = (char *)&fvar;
    
    uint16_t array_read[20] = {...};//initialized with some values
    float32_t var;
    
    chunkdata[0] = array_read[OFFSET] + (array_read[OFFSET+1] << 8);
    chunkdata[1] = array_read[OFFSET+2] + (array_read[OFFSET+3] << 8);
    var = *(float_t *)(&chunkdata[0]);

  • Hi Arpan,

    Data type float is 32-bit that has to always start at an even address, so your application has to take care of the memory allocation of your variables.  What was the MISRA C violation?

    Regards,

    Joseph