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.

Display a float value on the LCD

How I can show a float on the LCD for my temperature measurement?
this is my code, I'm using the function HalLcdWriteValue:

medicion_temp void (void)
{

       gradosC float;
      
       TR0 | = 0x01, / / ​​Must be modified prior TR0 to ATEST,
       ATEST | = 0x01 / / P0_0 and P0_1 Otherwise Forced with 0
       uint16 tempSample = HalAdcRead (HAL_ADC_CHN_TEMP, HAL_ADC_RESOLUTION_12);
       TR0 & = ~ 0x01;
       ATEST & = ~ 0x01;
      
       gradosC = tempSample *  0.016891;
      
       HalLcdWriteValue void (gradosC, 10, HAL_LCD_LINE_1);

}

But it is coming out this error: "expected an identifier" and I do not know why

  • Hello David,

    My first response to showing a float would be DON'T!  In previous generations of the 8051 8-bit MCU, the general rule is you never use floats in an 8 bit MCU.

    The ADC value you get isn't a float, so you shouldn't try to convert it to one.  Use all integer values, mutiply values to get rid of decimal and don't go beyond the resolution of the ADC.  You can then convert the integer to a string and add the decimal point and units.

    Here are some functions you can use, don't remember where they came from and to be honest, I don't know if using floats on the modern 8051 derrivatives like the CC25xx is still an issue.  Not sure either if this method is faster or uses less memory, but is would be interesting to find out. 

    Either way, these functions will allow you to format your output to the LCD by calling the HalLcdWriteString ( char *str, uint8 option) and also allows you to show +25.62 C or  -10.82 C or any other Unit you wish. See UTIL_int2float2str( uint32 value ) and UTIL_integer2str( uint16 value ) functions.

    OR you could use the:

    lcd_WriteStringValue("Temp: ",ValueBeforeDecimalPlace,10,1);
    lcd_WriteStringValue(".",ValueAfterDecimalPlace,10,1);


    /* Private function prototypes -----------------------------------------------*/
    static void _int2str( uint8* ptr, int X, int digit, bool flagunsigned, bool fillwithzero );

    /* Private functions ---------------------------------------------------------*/

    /*******************************************************************************
    *
    *                    _int2str
    *
    *******************************************************************************/
    /**
    *
    *  Translate a integer into a string.
    *
    *  @param[in,out] ptr            A pointer to a string large enough to contain
    *                                the translated 32 bit word.
    *  @param[in]     X              The integer to translate.
    *  @param[in]     digit          The amount of digits wanted in the result string.
    *  @param[in]     flagunsigned   Is the input word unsigned?
    *  @param[in]     fillwithzero   Fill with zeros or spaces.
    *
    **/
    /******************************************************************************/
    static void _int2str( uint8* ptr, int X, int digit, bool flagunsigned, bool fillwithzero )
    {
        uint8      c;
        bool    fFirst   = 0;
        bool    fNeg     = 0;
        uint8  DIG      = 1;
        int   i;
        int8   r;

        for ( i = 1; i < digit; i++ )
        {
            DIG *= 10;
        }

        if ( !flagunsigned && ( X < 0 ) )
        {
            fNeg = 1;
            r    = -X;
        }
        else
        {
            r = X;
        }

        for ( i = 0; i < digit; i++, DIG /= 10 )
        {
            c  = ( r / DIG );
            r -= ( c * DIG );

            if ( fillwithzero || fFirst || c || ( i == ( digit - 1 ) ) )
            {
                if (( fFirst == 0 ) && !flagunsigned )
                {
                    *ptr++ = fNeg ? '-' : ' ';
                }

                *ptr++ = ( c % 10 ) + '0';
                fFirst = 1;
            }
            else
            {
                *ptr++ = ' ';
            }
        }

        *ptr++ = '\0';
    }


    /* Public functions ----------------------------------------------------------*/


    /*******************************************************************************
    *
    *                   UTIL_uint2str
    *
    *******************************************************************************/
    /**
    *
    *  Convert an <b>unsigned</b> integer into a string.
    *  Using this function leads to much smaller code than using the sprintf()
    *  function from the C library.
    *
    *  @param [out]  ptr    The output string.
    *  @param [in]   X      The unsigned value to convert.
    *  @param [in]   digit  The number of digits in the output string.
    *  @param [in]   fillwithzero  \li 0   fill with blanks.
    *                              \li 1   fill with zeros.
    *
    **/
    /********************************************************************************/
    void UTIL_uint2str( uint8* ptr, uint8 X, uint8 digit, bool fillwithzero )
    {
        _int2str( ptr, X, digit, 1, fillwithzero );
    }

    /*******************************************************************************
    *
    *                   UTIL_int2str
    *
    *******************************************************************************/
    /**
    *
    *  Convert a <b>signed</b> integer into a string.
    *  Using this function leads to much smaller code than using the sprintf()
    *  function from the C library.
    *
    *  @param [out]  ptr    The output string.
    *  @param [in]   X      The unsigned value to convert.
    *  @param [in]   digit  The number of digits in the output string.
    *  @param [in]   fillwithzero  \li 0   fill with blanks.
    *                              \li 1   fill with zeros.
    *
    **/
    /******************************************************************************/
    void UTIL_int2str( uint8* ptr, int X, uint8 digit, bool fillwithzero )
    {
        _int2str( ptr, X, digit, 0, fillwithzero );
    }

    /**
      * @brief  Example of timeout situation management.
      * @param  None.
      * @retval None.
      */
    uint8 I2C2_TIMEOUT_UserCallback(void)
    {
      /* Use application may try to recover the communication by resetting I2C
        peripheral (calling the function I2C_SoftwareResetCmd()) then re-start
        the transmission/reception from a previously stored recover point.
        For simplicity reasons, this example only shows a basic way for errors
        managements which consists of stopping all the process and requiring system
        reset. */


      /* Display error message on screen */
     //   sprintf(msg_buf, "! ERROR !");

        return I2C_ERROR;
    }
    /*******************************************************************************
    *
    *                                UTIL_int2float2str
    *
    *******************************************************************************/
    /**
    *
    *  This function is used to display the current ADC/DAC values with.
    *  two decimal places
    *
    *
    **/
    /******************************************************************************/
    void UTIL_int2float2str( uint32 value )
    {
        uint8 c;
        uint16 r = 0;

        // Display modified ADC/DAC value by dividing the 3 digit value by a
        // factor of 10 each time and then inserting the decimal point
        /* 1 000 digit*/
        c = (( value - r ) / 1000 );
        r = r + ( c * 1000 );
        // if the leading value is zero then display a blank space
        if (c){
            ValueTextBuffer[0] = c + 0x30;
        }else{
            ValueTextBuffer[0] = 0x20;        
        }
        /* 100 digit*/
        c = (( value - r ) / 100 );
        r = r + ( c * 100 );
        ValueTextBuffer[1] = c + 0x30;

        /* Dot*/
        ValueTextBuffer[2] = '.';

        /* 10 digit*/
        c = (( value - r )/10);
        r = r + ( c * 10 );
        ValueTextBuffer[3] = c + 0x30;

        /* 1 digit*/
        c = (( value - r ));
        r = r +  c ;
        ValueTextBuffer[4] = c + 0x30;

        /* Volt*/
     //   ValueTextBuffer[5] = 'V';

        /* Celcius*/
     //   ValueTextBuffer[5] = 'C';

        /* Farenheit*/
     //   ValueTextBuffer[5] = 'F';
    }

    /*******************************************************************************
    *
    *                                UTIL_integer2str
    *
    *******************************************************************************/
    /**
    *
    *  This function is used to display the current ADC/DAC values with.
    *  two decimal places
    *
    *
    **/
    /******************************************************************************/
    void UTIL_integer2str( uint16 value )
    {
        uint8 c;
        uint16 r = 0;

        // Display modified ADC/DAC value by dividing the 3 digit value by a
        // factor of 10 each time and then inserting the decimal point
        /* 1 000 digit*/
        c = (( value - r ) / 1000 );
        r = r + ( c * 1000 );
        // if the leading value is zero then display a blank space
        if (c){
            ValueTextBuffer[0] = c + 0x30;
        }else{
            ValueTextBuffer[0] = 0x20;        
        }
        /* 100 digit*/
        c = (( value - r ) / 100 );
        r = r + ( c * 100 );
        ValueTextBuffer[1] = c + 0x30;

        /* 10 digit*/
        c = (( value - r )/10);
        r = r + ( c * 10 );
        ValueTextBuffer[2] = c + 0x30;

        /* 1 digit*/
        c = (( value - r ));
        r = r +  c ;
        ValueTextBuffer[3] = c + 0x30;

    }

     lcd_WriteStringValue("Time: ",milisec_time,10,1);
     lcd_WriteStringValue(".",Tenths,10,1);

  • I suspect greenja's very good reply is too much for Useche, whose first language is probably Spanish and is probably a beginner. There are some syntax errors. The formula assumes 0 value at 0C. It probably should not. Possible minimal change code.

    void medicion_temp(void)
    {
           gradosC float;
           uint16 tempSample;
          
           TR0 |= 0x01; /* ​​Must be modified prior TR0 to ATEST, */
           ATEST |= 0x01; /* P0_0 and P0_1 Otherwise Forced with 0 */
           tempSample = HalAdcRead (HAL_ADC_CHN_TEMP, HAL_ADC_RESOLUTION_12);
           TR0 &= ~0x01;
           ATEST &= ~0x01;
          
           gradosC = 25.0 + (((float)tempSample-1480.0)/4.5);
          
           HalLcdWriteValue((uint32)gradosC, 10, HAL_LCD_LINE_1);

    }

    Negative C temperatures are NOT supported. The temperature is truncated to an integer. As greenja has noted, the code will be very slow and very large.

  • thanks very much Norman for your answer, I would want to ask you another question, how I can show the temperature of the end devices to the coordinator (in LCD), or rather how I can read the temperature being measured by end devices from the coordinator and display on the LCD.

  • Sorry, I do not have experience with your platform. My previous answer was based upon the reading the datasheet. Perhaps you should start a new post.