2

I'm working with PSoC Creator, and I'm trying to print a float between -1 and 1 to an I2C OLED. To get this working I'm using the function sprintf. I added the following flag to my linker -u _printf_float because I'm using newlib-nano and it omits float handling code if not requested. (my problem might have to do something with this... maybe?)

For testing purposes I wrote the lines:

char stringbuffer[20];
sprintf(stringbuffer, "(%1.1f, %1.1f)", 1.1, 1.1);

While debugging I noticed that every non-zero float I try to print is printed as garbage, with a null character up front. Actually every non-zero float is replaced by the following 3 bytes. If I replace all the values with 0.0 then it is printed perfectly fine.

0x00 '000円'
0x2E '.'
0x80 '200円'

The integers that I'm trying to print in the following line are behaving as expected.

Anyone that ran into a similar problem?

chqrlie
153k12 gold badges146 silver badges232 bronze badges
asked Nov 5, 2020 at 20:34
12
  • 1
    Not sure it's the cause of the problem, but the %1.1f format looks a bit weird. This is asking for a (total) width of 1 character and 1 digit after the decimal. Try %3.1f? Commented Nov 5, 2020 at 20:39
  • 1
    @AdrianMole The first 1 is the minimum width, not total width. Still a tad weird. I doubt it is part of the problem. Commented Nov 5, 2020 at 20:43
  • 1
    Does code use a proper prototype for sprintf()?, Proper linking? Commented Nov 5, 2020 at 20:45
  • cannot reproduce godbolt.org/z/346q37 voting to close Commented Nov 5, 2020 at 22:15
  • @AdrianMole you're right, shouldn't have added the 1 in front of the period. But that is not the problem. Commented Nov 6, 2020 at 0:34

1 Answer 1

0

I ran into the same problem with sprintf(), using newlib-nano with floating-point support enabled.

The issue is caused by insufficient heap space. In PSoC Creator, in the Workspace Explorer pane, navigate to System. Expand "Configuration", then change Heap Size (bytes) from the default 0x80 to 0x200. Rebuild, and run.

answered Jan 23, 2024 at 2:43
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.