2 Replies Latest reply on Apr 4, 2017 1:58 AM by Lockdog

    printf (rare problem), does not print variables


      Hello friends, I have a very rare problem, when printing text varibables (%f) only prints the text ... I tested with different projects, (now comes the rare) with exactly the same code in two different projects, one and variable text prints correctly, but the other only prints text. Does anyone know why it might be? Any configuration of the same project that limits this?

      Example: printf("Temperature %2.2f \n", GetTemperature());

      1: Temperature

      2: Temperature 25.35


      Intel Quark D2000



        • 1. Re: prinf (rare problem), does not print variables

          Intel(r) QMSI implementation of the printf() function supports only a very limited set of the format specifiers. See pico_printf() function here: qmsi/newlib-syscalls.c at master · quark-mcu/qmsi · GitHub  that implements the printf functionality. It appears that the following format specifiers are supported:

          %d - signed integer

          %u - unsigned integer

          %x - hexadecimal output for integer

          %s - char * (string/zero-terminated char array)

          long "%l" prefix (as in %ld, %lu) is ignored, since on a 32-bit architecture both int and long are 32-bit


          As you can see float (%f)  is not supported. This does not surprise me, as the floating point calculations are not common (and not efficient) on MCUs without floating point unit.

          And so, one question I have in mind, is how your GetTemperature() function obtains/calculates the temperature? Chances are that it reads the output of an ADC (either integrated in the MCU, or an external ADC, for example integrated in a digital temperature sensor), that returns an integer result anyway.

          From MCU programming perspective it might be simpler to modify the GetTemperature() code to return an integer result using smaller units - for example instead of degrees Celsius, return units of 0.01C (e.g. 2425 for 24.25C). And then print the interger and the fraction parts separately, using something like: printf(Temperature: "%d.%02d", temp / 100, temp % 100); (You can use this technique with a float variable by multiplying it by 100 first).

          Unfortunately QMSI does not support padding of the output (by adding numeric prefix to the specifiers, e.g. "%02d") either. While ago I submitted a patch to make that work, but it had been rejected... In case you're interested, I can try finding that patch.

          • 2. Re: printf (rare problem), does not print variables


            Try to use sprintf() before, and then print the required string.