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.

TM4C1294NCPDT: TM4C1294NCPDT: Printing float numbers using UARTprinf

Part Number: TM4C1294NCPDT

I was using the suggested code shared in other posts to print float numbers, splitting it in an integer and a fraction part:

void FloatTo2PartInteger(float IntegerToConvert, int32_t* IntegerPart, int32_t* FractionPart){

    *IntegerPart = (int32_t) IntegerToConvert;                   // Convert IntegerPart from float number
    *FractionPart = (int32_t) (IntegerToConvert * 10.0f);        // Convert FractionPart from float number
    *FractionPart = *FractionPart - (*IntegerPart)*10;             // Remove IntegerPart from FractionPart

    if (*FractionPart < 0) {                                             // If FractionPart is negative
        *FractionPart *= -1;                                             // Multiply itself per -1 to obtain positive part
    }

}

But I have observed it is not printing the negative signal for numbers inside the range [-1... 0]. Example: When I was trying to print the number -0.063 I have 0.063 with no negative signal. 

Here a suggested code to fix it:

void PrintFloat(float finput, uint8_t *ui8Sign, int32_t *i32IntegerPart, int32_t *i32FractionPart) {
    //
    // Convert the floating point input to an integer part
    // and fraction part for easy printing.
    //
    *i32IntegerPart = (int32_t)finput;
    *i32FractionPart = (int32_t)(finput * 1000.0f);
    *i32FractionPart = *i32FractionPart - (*i32IntegerPart * 1000);
    if(*i32FractionPart < 0) {
        *i32FractionPart *= -1;
        if(*i32IntegerPart == 0) {
            *ui8Sign = 0x2D;
        }
    }
    else {
        *ui8Sign = 0x00;
    }
}

 And here an use example:

    PrintFloat(fADISData[3], &ui8Sign, &i32IntegerPart, &i32FractionPart);
    UARTprintf("X_ACCEL: %c%d.%03d ", ui8Sign, i32IntegerPart, i32FractionPart);

I think the problem was, when the integer part is zero it is not possible to hold the sign information.