Unable to print float using uart

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
NaAl_4668831
Level 1
Level 1

Hi Team,

I am not able to print a float variable using the UART communication. Printing of integer and String works fine, but not the float. I have followed the build setting using this link : Float Print on UART . But I am not able to receive an output. The target device is CY8C5888LTI-LP097. The code description is as folllows

++++++++++++++++++++++++++++++++++++++++++++

#include "project.h"

#include "stdio.h"

uint8 x=0;

char b;

CY_ISR(interruptRX)

{

    b = UART_GetChar();

}

int main(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

    /* Place your initialization/startup code here (e.g. MyInst_Start()) */

    UART_Start();

    isrRX_StartEx(interruptRX);

    char to_pri[32];

    char det[] = "detected";

    //int a;

    float y1 = 0.95;

    float y2 = 0.19;

    //float vol;

    //float res;

    //char b;

    for(;;)

    {

         if(b == '1')

        {

            //LED_BLINK_Write(!LED_BLINK_Read());

            //CyDelay(100);

            LED_BLINK_Write(1);

            printf(to_pri,"%f\n",y1);

            UART_PutString(to_pri);

        }

        if(b == '0')

        {

            LED_BLINK_Write(0);

            sprintf(to_pri,"%f\n",y2);

            UART_PutString(to_pri);

        }

}

+++++++++++++++++++++++++

This is the output i receive at the terminal:

Capture1.JPG

Can you guys help me to print the value y1 and y2.

Regards,

Alexander.

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

A few weeks have passed, but no correct answer marked?

So I tried with my CY8CKIT-059.

schematic

000-schematic.JPG

Menu: Project > Build Settings... 

Note: Use newlib-nano Float Formatting: True

000-Linker-config.JPG

Project Explorer > System

Note: Heap Size (bytes) were 0x80 by default and expanded to 0x400

001-heap-0x400.JPG

pins

0011-pins.JPG

main.c

Note: Sorry, I could not help modifying a little.

====================

#include "project.h"

#include "stdio.h"

uint8 x=0;

volatile char b; // changed

volatile int key_entered_flag = 0 ;

CY_ISR(interruptRX)

{

    isrRX_ClearPending() ;

    b = UART_GetChar();

    key_entered_flag = 1 ;

}

void cls(void)

{

    UART_PutString("\033c") ; /* reset */

    CyDelay(100) ;

    UART_PutString("\033[2J") ; /* clear screen */

    CyDelay(100) ;

}

int main(void)

{

    char to_pri[32];

    float y1 = 0.95;

    float y2 = 0.19;

       

    CyGlobalIntEnable; /* Enable global interrupts. */

    UART_Start();

    isrRX_StartEx(interruptRX);

    cls() ;

    UART_PutString("5LP float printing test\n") ;

    for(;;)

    {

        if (key_entered_flag) {

            key_entered_flag = 0 ;

            if(b == '1')

            {

                LED_BLINK_Write(1);

                UART_PutChar(b) ;

                UART_PutChar(' ') ;

                sprintf(to_pri," %f\n",y1);

                UART_PutString(to_pri);

            }

            if(b == '0')

            {

                LED_BLINK_Write(0);

                UART_PutChar(b) ;

                UART_PutChar(' ') ;

                sprintf(to_pri," %f\n",y2);

                UART_PutString(to_pri);

            }

        }

    }

}

====================

TeraTerm log

002-TeraTerm-log.JPG

moto

View solution in original post

0 Likes
5 Replies
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

Alexander,

Can you share your project with us?

I sounds like you followed the forum Float Print on UART recommendations about enabling the printf_float library.  Did you increase the heap space also?

The only thing I noticed is that you used a printf() instead of sprintf() statement in the if( b = '1') conditional.  I assumed it was a typo.

I also notice the third line of your output lists "0.1".

Additionally,  you might want to clear your input character b in each conditional.  Otherwise you will to print to the Terminal continuously without new input.

Len

Len
"Engineering is an Art. The Art of Compromise."
0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

Could you also read my old memo when you have time?

printf and float rhapsody (aka, yet another printf and floating topic)

moto

P.S.

>             printf(to_pri,"%f\n",y1);

this also might have been

<           sprintf(to_pri,"%f¥n",y1);

0 Likes
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

A few weeks have passed, but no correct answer marked?

So I tried with my CY8CKIT-059.

schematic

000-schematic.JPG

Menu: Project > Build Settings... 

Note: Use newlib-nano Float Formatting: True

000-Linker-config.JPG

Project Explorer > System

Note: Heap Size (bytes) were 0x80 by default and expanded to 0x400

001-heap-0x400.JPG

pins

0011-pins.JPG

main.c

Note: Sorry, I could not help modifying a little.

====================

#include "project.h"

#include "stdio.h"

uint8 x=0;

volatile char b; // changed

volatile int key_entered_flag = 0 ;

CY_ISR(interruptRX)

{

    isrRX_ClearPending() ;

    b = UART_GetChar();

    key_entered_flag = 1 ;

}

void cls(void)

{

    UART_PutString("\033c") ; /* reset */

    CyDelay(100) ;

    UART_PutString("\033[2J") ; /* clear screen */

    CyDelay(100) ;

}

int main(void)

{

    char to_pri[32];

    float y1 = 0.95;

    float y2 = 0.19;

       

    CyGlobalIntEnable; /* Enable global interrupts. */

    UART_Start();

    isrRX_StartEx(interruptRX);

    cls() ;

    UART_PutString("5LP float printing test\n") ;

    for(;;)

    {

        if (key_entered_flag) {

            key_entered_flag = 0 ;

            if(b == '1')

            {

                LED_BLINK_Write(1);

                UART_PutChar(b) ;

                UART_PutChar(' ') ;

                sprintf(to_pri," %f\n",y1);

                UART_PutString(to_pri);

            }

            if(b == '0')

            {

                LED_BLINK_Write(0);

                UART_PutChar(b) ;

                UART_PutChar(' ') ;

                sprintf(to_pri," %f\n",y2);

                UART_PutString(to_pri);

            }

        }

    }

}

====================

TeraTerm log

002-TeraTerm-log.JPG

moto

0 Likes
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

Alexander,

There were multiple problems with your main() code as explained in my earlier post.  However the biggest regarding the print float issue is that you needed to increase the Heap size.  You will find the heap size parameter in the Design Wide Resource (DWR) System Tab.  Change it to 0x200.  Before changing the heap, I basically received something similar to your output.  Apparently it takes more than 0x80 bytes of heap to process a float in sprintf().

I fixed some of the other issues as well.

Try this code:

#include "project.h"

#include "stdio.h"

uint8 x=0;

char b;

CY_ISR(interruptRX)

{

    b = UART_GetChar();

}

int main(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

    /* Place your initialization/startup code here (e.g. MyInst_Start()) */

    UART_Start();

    isrRX_StartEx(interruptRX);

    char to_pri[32];

    char det[] = "detected";

    //int a;

    float y1 = 0.95;

    float y2 = 0.19;

    //float vol;

    //float res;

    //char b;

    UART_PutString("Float Test\r\n");

    for(;;)

    {

        if(b == '1')

        {

            //LED_BLINK_Write(!LED_BLINK_Read());

            //CyDelay(100);

            LED_BLINK_Write(1);

            sprintf(to_pri,"%f\r\n",y1);

            UART_PutString(to_pri);

            b = '\0';    // Reset input. this is to prevent continuous output.

        }

        if(b == '0')

        {

            LED_BLINK_Write(0);

            sprintf(to_pri,"%f\r\n",y2);

            UART_PutString(to_pri);

            b = '\0';    // Reset input. this is to prevent continuous output.

        }

    }

}

Len

Len
"Engineering is an Art. The Art of Compromise."
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Most errors regarding printf of a float can be removed by increasing the heap size (Creator ->ystem_Heap size) to at least 256 bytes.

Bob