Output decimal serially to user while not being allowed to use floating point???

Discussion in 'Programmer's Corner' started by danielb33, Jan 22, 2013.

  1. danielb33

    Thread Starter Member

    Aug 20, 2012
    I have serial communication set up for a motor controller. I am outputting much data (current, temperature, voltage, etc.) and would like to NOT require the user to think, "Oh yeah, I need to move the decimal two places to the left before writing my answer down." I am working with a stm32F1, lots of processing power, but my boss says not to use floating point during this project.

    I KNOW that the decimal will ALWAYS be shifted one, two, or three decimals to the left. Is there a way to make decimal format outputted?

    I am using a standard _IO_putchar() function and printf() to output data.

    Thanks in advance for any help!
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    You could use a strncpy or similar function to pull the last 1-3 digits out of the output string, insert a decimal point, then print the rest of the digits.

    What size is the buffer holding the number? If it is always 2 decimal places, displaying a point can be done without floating point math.
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    Do you really need printf()? It's a big function. See if you have the itoa() function, for Int to ASCII (ItoA)... then you have a string you can insert the decimal point in.

    If you scale your numbers correctly you can avoid using a float. Example: Say you do your computations in millivolts... then tag the DP where it need be.
  4. danielb33

    Thread Starter Member

    Aug 20, 2012
    ThatoneGuy- Not sure how to use strcpy. I am using printf with _io_putchar so every time I send a character via printf(), that character is automatically stored in a buffer via hardware to be transmitted thru an interrupt. The converted values I am sending thru printf() are ints, so I don't think strcpy will work.

    ErnieM - itoa() does not work with my micro.

    Anybody- Any other ways to convert integers to strings???? This is a good idea. If I can do this I can insert the decimal and use printf().
  5. Ian Rogers


    Dec 12, 2012
    I did my own....
    Code ( (Unknown Language)):
    2. void printFloat(char * flt, long number, char digits)
    3.     {
    4.     if(number < 0)
    5.         {
    6.         number = ABS(number);
    7.         }
    8.     if(digits == 2)
    9.         {
    10.         flt[0] = number / 1000 + 48;
    11.         if(flt[0] == 48) flt[0] = 0x20;
    12.         flt[1] = ((number % 1000) / 100)+ 48;
    13.         flt[2] = 46;
    14.         flt[3] = ((number % 100) / 10)+ 48;
    15.         flt[4] = (number % 10) + 48;
    16.         flt[5] = 0;
    17.         }
    18.     else
    19.         {
    20.         flt[0] = number / 100 + 48;
    21.         if(flt[0] == 48) flt[0] = 0x20;
    22.         flt[1] = ((number % 100) / 10)+ 48;
    23.         flt[2] = 46;
    24.         flt[3] = (number % 10) + 48;
    25.         flt[4] = 0;
    26.         }
    27.     }
    I needed one or two decimal places with 5 overall digits..... It works for me..

    PS you need an ABS() function if any numbers is negative..
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    itoa() sometimes lives in stdlib.h, which is a strange place to find it as it's NOT part of the C standard. If your lib doesn't have it you can always google a version.
  7. t06afre

    AAC Fanatic!

    May 11, 2009
    Also remember that you do have to convert the data to string before sending it down the serial link. At least as long as you also control the software in the receiving end. And this is running on a standard PC.
  8. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    If you are only dealing with one byte at a time and putchar, what order are you sending the data? L->R or R->L?

    Count the number of characters sent, if it is equal to x, hold current byte and send decimal point, then current byte, and continue sending the rest.