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.

CCS/LAUNCHXL-CC1350: how to print two decimal points

Part Number: LAUNCHXL-CC1350
Other Parts Discussed in Thread: CC1350, TMP116

Tool/software: Code Composer Studio

Dear experts,

i am working with cc1350 launch pad and i2c example, i was declare floating variable and i want print just 2 decimal points but that prints 4 decimal points i can't limit the decimal points so can you help me to find the solution for my problem.

Display_printf(display, 0, 0, "Temp:%.2fC",temperature); 

thank you

  • Hi Surya,
    Did you have a look at the following thread? e2e.ti.com/.../626415
    Regards,
  • Hi,
    You can sprintf the floating value in a buffer and then print the buffer out as a string on UART.
  • Did you add a field width? "%8.2f"
  • Hi Clement,

    thanks for your reply, but i was trying that sprintf method but i get same result and that is print at only once but i was write the code in while function

    i was added the stdio.h in my header 

    #include <stdint.h>
    #include <stddef.h>
    #include <unistd.h>
    #include <stdio.h>
    //#include <stdlib.h>
    //#include <math.h>
    /* Driver Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/I2C.h>
    #include <ti/display/Display.h>

    after that my code 

    while(1)
    {
    /* Point to the die tempature register and read its 2 bytes */
    txBuffer[0] = TMP116_DIE_TEMP;
    i2cTransaction.slaveAddress = Si7021_ADDR;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 2;

    if (I2C_transfer(i2c, &i2cTransaction))
    {
    char buf[64];
    temper = (rxBuffer[0] << 8) | (rxBuffer[1]);
    temperature = (175.72*temper)/65536;
    temperature -= 46.85;
    sprintf(buf,"Buffer:%3.2fC\n",temperature);
    Display_printf(display, 1, 0, "Temp:%3.2fC\n",temperature);
    Display_printf(display, 1, 0, "%s\n",buf);
    }

    i was trying both methods sprintf and add field width but i get same result and onething if i use sprintf method my CC1350 does not print anything to uart. 

    my current result is 

    Humi:72.3271%
    Temp:27.8181C

    but i need only two decimal points to print my uart

    thank you

  • Hi,

    I can tell you that the following code prints "Temp:12.35C" using SDK v3.10.

    static Display_Handle display;
    Display_init();
    display = Display_open(Display_Type_UART, NULL);        
    char buffer[10];
    float temperature = 12.3456789;
    sprintf(buffer, "%.2f", temperature);
    Display_printf(display, 1, 0, "Temp:%sC\n",buffer);

    Can you tell me which SDK version you are using?

    Regards,

  • /*
     * Copyright (c) 2018-2019, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /*
     *  ======== i2ctmp116.c ========
     */
    #include <stdint.h>
    #include <stddef.h>
    #include <unistd.h>
    #include <stdio.h>
    //#include <stdlib.h>
    //#include <math.h>
    /* Driver Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/I2C.h>
    #include <ti/display/Display.h>
    
    /* Example/Board Header files */
    #include "Board.h"
    
    #define TASKSTACKSIZE       640
    
    /* TMP116 DIE Temp Result Register */
    #define TMP116_DIE_TEMP     0xE3
    #define Si7021_ADDR         0x40
    static Display_Handle display;
    
    /*
     *  ======== mainThread ========
     */
    void *mainThread(void *arg0)
    {
        //uint16_t        sample;
        float        temperature;
        double        temper;
        float        humidity;
        double        humi;
        uint8_t         txBuffer[1];
        uint8_t         rxBuffer[2];
        I2C_Handle      i2c;
        I2C_Params      i2cParams;
        I2C_Transaction i2cTransaction;
    
        /* Call driver init functions */
        Display_init();
        GPIO_init();
        I2C_init();
    
        /* Configure the LED and if applicable, the TMP116_EN pin */
        GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    #ifdef Board_GPIO_TMP116_EN
        GPIO_setConfig(Board_GPIO_TMP116_EN, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_HIGH);
        /* 1.5 ms reset time for the TMP116 */
        sleep(1);
    #endif
    
        /* Open the HOST display for output */
        display = Display_open(Display_Type_UART, NULL);
        if (display == NULL) {
            while (1);
        }
    
        /* Turn on user LED */
        GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
        Display_printf(display, 0, 0, "Starting the i2ctmp116 example\n");
    
        /* Create I2C for usage */
        I2C_Params_init(&i2cParams);
        i2cParams.bitRate = I2C_400kHz;
        i2c = I2C_open(Board_I2C_TMP, &i2cParams);
        if (i2c == NULL) {
            Display_printf(display, 0, 0, "Error Initializing I2C\n");
            while (1);
        }
        else {
            Display_printf(display, 0, 0, "I2C Initialized!\n");
        }
    
        while(1)
        {
            /* Point to the die tempature register and read its 2 bytes */
            txBuffer[0] = 0xE3;
            i2cTransaction.slaveAddress = Si7021_ADDR;
            i2cTransaction.writeBuf = txBuffer;
            i2cTransaction.writeCount = 1;
            i2cTransaction.readBuf = rxBuffer;
            i2cTransaction.readCount = 2;
    
            if (I2C_transfer(i2c, &i2cTransaction))
            {
                //char buf[64];
                temper = (rxBuffer[0] << 8) | (rxBuffer[1]);
                temperature = (175.72*temper)/65536;
                temperature -= 46.85;
               // sprintf(buf,"Buffer:%3.2fC\n",temperature);
                Display_printf(display, 1, 0, "Temp:%3.2fC\n",temperature);
               // Display_printf(display, 1, 0, "%s\n",buf);
            }
            else
            {
                Display_printf(display, 0, 0, "I2C Bus fault.");
            }
    
            txBuffer[0] = 0xE5;
            i2cTransaction.slaveAddress = Si7021_ADDR;
            i2cTransaction.writeBuf = txBuffer;
            i2cTransaction.writeCount = 1;
            i2cTransaction.readBuf = rxBuffer;
            i2cTransaction.readCount = 2;
    
            if (I2C_transfer(i2c, &i2cTransaction))
            {
                humi = (rxBuffer[0] << 8) | (rxBuffer[1]);
                humidity = (125*humi)/65536;
                humidity -=6;
                Display_printf(display, 0, 0, "Humi:%.1f%c",humidity,37);
            }
            else
            {
                Display_printf(display, 0, 0, "I2C Bus fault.");
            }
    
            /* Sleep for 1 second */
            sleep(1);
        }
    
        //I2C_close(i2c);
       // Display_printf(display, 0, 0, "I2C closed!");
    
      //  return (NULL);
    }
    
    hi clement,

    i am using as same sdk version as v3.10

    but i can't get two decimal points in my code

    i am attach my complete code for your reference please help to find what is the mistake in my code.

    i was disable that line in my code.

    thank you

  • %3.2f does NOT print in the form DDD.DD, it should print DDD since the 3 is the field width (including the decimal point and any +/- signs!), not the number of places to the left of the decimal point. You need something like %8.2

    I don't know how your program printed what it did.
  • Dear keith,
    this is my code
    // char buf[64];
    temper = (rxBuffer[0] << 8) | (rxBuffer[1]);
    temperature = (175.72*temper)/65536;
    temperature -= 46.85;
    //sprintf(buf,"Buffer:%.2fC\n",temperature);
    Display_printf(display, 1, 0, "Temp:%.2fC\n",temperature);
    //Display_printf(display, 1, 0, "%s\n",buf);

    i was disable some lines, if i enable that lines, my cc1350 will print the temperature data at only once like
    Temp:27.6358C
    then that is strucked or stop working or i don't know but my cc1350 dont print anything to uart.

    if i upload the code with above disabled lines my cc1350 working fine that is print the data continuously. i don't know what is the mistake in my code.
    can you please help to find the solution for my problem.

    and now i am using while() in main thread for execute the function continuously can you please tell me to how to execute the function periodically, the period interval is user defined
  • Use a timer. It is pretty easy with Ti-RTOS.
  • Can you show me a example for that.
  • Section 8.3 of the BIOS user guide:

    Timer_Handle timerHandle;
    Int main(Void)
    {
    Error_Block eb;
    Timer_Params timerParams;
    Task_Handle taskHandle;
    Error_init(&eb);
    Timer_Params_init(&timerParams);
    timerParams.period = 2000; /* 2 ms */
    timerHandle = Timer_create(Timer_ANY, tickFxn, &timerParams, &eb);
    if (timerHandle == NULL) {
    System_abort("Timer create failed");
    }
    taskHandle = Task_create(masterTask, NULL, &eb);
    if (taskHandle == NULL) {
    System_abort("Task create failed");
    }
    }
    Void masterTask(UArg arg0 UArg arg1)
    {
    ...
    // Condition detected requiring a change to timer period
    Timer_stop(timerHandle);
    Timer_setPeriodMicroSecs(4000); /* change 2ms to 4ms */
    Timer_start(timerHandle);
    ...
    }
    Void tickFxn(UArg arg0)
    {
    System_printf("Current period = %d\n",
    Timer_getPeriod(timerHandle);
    }

  • Dear Keith,
    thanks a lot for this help and still i can't print 2decimal point value in my uart, i was do all things but i can't get result and i can't find what is the mistake.
  • Did you try to run a test program using Clement's snippet?
  • Oh, I can't believe that this has not come up already. Generally any printf() weirdness can be solved by increasing the stack size.
  • Dear Keith, 

    i was already tried that but i can't get result continuously, this is my code

    while(1)
    {
    /* Point to the die tempature register and read its 2 bytes */
    txBuffer[0] = 0xE3;
    i2cTransaction.slaveAddress = Si7021_ADDR;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 2;

    if (I2C_transfer(i2c, &i2cTransaction))
    {
    // char buf[64];
    char buffer[10];
    float tem = 12.3456789;
    sprintf(buffer, "%.2f", tem);
    Display_printf(display, 1, 0, "Test:%sC\n",buffer);

    temper = (rxBuffer[0] << 8) | (rxBuffer[1]);
    temperature = (175.72*temper)/65536;
    temperature -= 46.85;
    //sprintf(buf,"Buffer:%.2fC\n",temperature);
    Display_printf(display, 1, 0, "Temp:%.2fC\n",temperature);
    //Display_printf(display, 1, 0, "%s\n",buf);

    i was use while() for run and print the data continuously but my cc1350 launch pad is print the data at only one not repeat. 

    output:

    Starting the i2ctmp116 example

    I2C Initialized!

    Test:12.35C

    then my launchpad doing nothing like that is struck or hanging or what i don't know.