probleme with printf("TB %.1f",temperature)

Discussion in 'C Programming' started by Andre, Oct 30, 2012.

  1. Andre

    Andre Guest

    This explode my pic16f648 a memory!!
    I am using PICC Light ( as a retired I can't afford a pro )
    So does anyone know about a way to convert float to ascii without using
    printf??
    Many thanks in advances
    Andre
    Andre, Oct 30, 2012
    #1
    1. Advertising

  2. Andre

    James Kuyper Guest

    On 10/30/2012 10:23 AM, Andre wrote:
    > This explode my pic16f648 a memory!!


    That shouldn't happen, and if it is happening, your first priority
    should be to find out why. The two most plausible failure mechanisms:
    failure to #include <stdio.h>, or 'temperature' has a type other float
    or double. If neither of those is the problem, then if you want our help
    to figure out the problem, you need to provide more information:

    1. a short, complete, compilable piece of code demonstrating the problem.
    2. The complete set of commands you used to compile and link it.
    3. The exact symptoms you observed when it failed. If there was an error
    message of any kind, the full text of that message is needed if we're to
    find out what went wrong.

    > I am using PICC Light ( as a retired I can't afford a pro )
    > So does anyone know about a way to convert float to ascii without using
    > printf??


    You could use sprintf(). However, there's a good chance that whatever
    caused the printf() problem will also occur if you use sprintf().
    You could also try writing your own dtostr() function. However, the
    details of such a function are a bit complicated, and it's probably much
    simpler to figure out what went wrong with your printf() call, than it
    would be write dtostr().
    --
    James Kuyper
    James Kuyper, Oct 30, 2012
    #2
    1. Advertising

  3. Andre

    Eric Sosman Guest

    On 10/30/2012 10:23 AM, Andre wrote:
    > This explode my pic16f648 a memory!!


    I guess this means printf() and its support functions make
    your program too large. If that's not the problem, just ignore
    what follows ...

    > I am using PICC Light ( as a retired I can't afford a pro )
    > So does anyone know about a way to convert float to ascii without using
    > printf??


    First, see whether you can use the fcvt() function. It's not
    a standard C library function, but many systems offer it.

    If fcvt() doesn't work out, then for the simple use you've
    shown you could multiply by ten, round to the nearest integer,
    convert the integer, and then insert the decimal point. This
    approach does not deal well with infinities, NaN's, or values
    of large magnitude -- but for "moderate" numbers and just a few
    decimal places it may suffice.

    --
    Eric Sosman
    d
    Eric Sosman, Oct 30, 2012
    #3
  4. Andre <> writes:

    > This explode my pic16f648 a memory!!
    > I am using PICC Light ( as a retired I can't afford a pro )
    > So does anyone know about a way to convert float to ascii without
    > using printf??


    It's hard in general, but your example format -- %.1f -- is easier. One
    way might be:

    double f10 = f * 10 + (f < 0 ? -0.5 : 0.5);
    printf("%d.%d\n", (int)(f10/10), (int)fabs(fmod(f10, 10)));

    You won't necessarily get the same rounding that %.1f gives you, and it
    won't work when f is more than a little larger than INT_MAX (or much
    smaller than INT_MIN). You can extend the range by converting to long
    int or long long int rather than plain int, but that might cause more
    pain on a low-spec CPU.

    --
    Ben.
    Ben Bacarisse, Oct 30, 2012
    #4
  5. On 30.10.2012 16:01, James Kuyper wrote:
    > On 10/30/2012 10:23 AM, Andre wrote:
    >> This explode my pic16f648 a memory!!

    >
    > That shouldn't happen, and if it is happening, your first priority
    > should be to find out why.


    Why not? The device he's using has 8kB of Flash. Since it has no FPU,
    the softfloat stuff is already in apparently (1-2kB) and I've seen
    printf (depending on the functionality) see take 4-6kB on a 8 Bit uC
    dozens of times. It looks perfectly reasonable, actually.

    This is why it's a really really bad idea to use FP on 8-bitters.

    Regards,
    Johannes

    --
    >> Wo hattest Du das Beben nochmal GENAU vorhergesagt?

    > Zumindest nicht öffentlich!

    Ah, der neueste und bis heute genialste Streich unsere großen
    Kosmologen: Die Geheim-Vorhersage.
    - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$>
    Johannes Bauer, Oct 30, 2012
    #5
  6. On 30.10.2012 15:23, Andre wrote:
    > This explode my pic16f648 a memory!!
    > I am using PICC Light ( as a retired I can't afford a pro )
    > So does anyone know about a way to convert float to ascii without using
    > printf??


    Does the device NEED to transport the decimal representation in ASCII or
    is it sufficient to transport the float VALUE? If the latter is a
    possibility (for example you're doing some RS232 communication with a PC
    on the other side), then send the value directly as they're stored
    within the devices memory and do the conversion on the PC.

    If you're doing some calculation and want to display it, for example on
    a LCD, think about using fixed point arithmetic. It's going to save you
    a lot of flash space and is magnitudes faster than floating point.

    Best regards,
    Johannes

    --
    >> Wo hattest Du das Beben nochmal GENAU vorhergesagt?

    > Zumindest nicht öffentlich!

    Ah, der neueste und bis heute genialste Streich unsere großen
    Kosmologen: Die Geheim-Vorhersage.
    - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$>
    Johannes Bauer, Oct 30, 2012
    #6
  7. Andre

    James Kuyper Guest

    On 10/30/2012 11:34 AM, Johannes Bauer wrote:
    > On 30.10.2012 16:01, James Kuyper wrote:
    >> On 10/30/2012 10:23 AM, Andre wrote:
    >>> This explode my pic16f648 a memory!!

    >>
    >> That shouldn't happen, and if it is happening, your first priority
    >> should be to find out why.

    >
    > Why not? The device he's using has 8kB of Flash. Since it has no FPU,
    > the softfloat stuff is already in apparently (1-2kB) and I've seen
    > printf (depending on the functionality) see take 4-6kB on a 8 Bit uC
    > dozens of times. It looks perfectly reasonable, actually.
    >
    > This is why it's a really really bad idea to use FP on 8-bitters.


    Now that Eric and you have both interpreted his statement as indicating
    that he ran out of memory, that seems the most reasonable
    interpretation, but it didn't occur to me at the time. I had just
    assumed that he was referring to a memory access violation of some kind.
    This is a good example of why it's a good idea to post the full text of
    any relevant error messages - they're less open to misinterpretation.
    James Kuyper, Oct 30, 2012
    #7
  8. Andre

    Andre Guest

    Le 30/10/2012 16:37, Johannes Bauer a écrit :
    > On 30.10.2012 15:23, Andre wrote:
    >> This explode my pic16f648 a memory!!
    >> I am using PICC Light ( as a retired I can't afford a pro )
    >> So does anyone know about a way to convert float to ascii without using
    >> printf??

    >
    > Does the device NEED to transport the decimal representation in ASCII or
    > is it sufficient to transport the float VALUE? If the latter is a
    > possibility (for example you're doing some RS232 communication with a PC
    > on the other side), then send the value directly as they're stored
    > within the devices memory and do the conversion on the PC.
    >
    > If you're doing some calculation and want to display it, for example on
    > a LCD, think about using fixed point arithmetic. It's going to save you
    > a lot of flash space and is magnitudes faster than floating point.
    >
    > Best regards,
    > Johannes
    >

    Well, I like to do both.
    Send to a PC where it's entered in RRDTOOL database and displayed
    To make the convertion on the PC I will need to convert in HEX, as
    serial com, will probably have trouble with binary.

    AND

    I whishes to display it on a LCD, but most probably I will have to move
    all this on a pic16F877 that I have in a drawer somewhere.

    I promess, as soon as I complete this, I will move to AVR µctl, and avr-gcc.
    PICC Light is wasting 40% of memory and that leads to thi kind of
    situations.
    Many tanks to everybody.
    André
    Andre, Oct 30, 2012
    #8
  9. Andre

    Ben Guest

    Andre skrev 2012-10-30 15:23:
    > This explode my pic16f648 a memory!!
    > I am using PICC Light ( as a retired I can't afford a pro )
    > So does anyone know about a way to convert float to ascii without using
    > printf??
    > Many thanks in advances
    > Andre



    Do you have to use float to represent the value. When dealing with those
    braindead micro's it's better to use some form of fixed point notation.
    As soon as you begin use float and long every operation will use lot of
    memory, avoid to use them if you can.

    A good site to read about the quirks of PICC and Microchips miros is at
    http://www.microchipc.com/

    /Ben
    Ben, Nov 1, 2012
    #9
  10. In article <>,
    Ben Bacarisse <> wrote:
    >Andre <> writes:
    >
    >> This explode my pic16f648 a memory!!
    >> I am using PICC Light ( as a retired I can't afford a pro )
    >> So does anyone know about a way to convert float to ascii without
    >> using printf??

    >
    >It's hard in general, but your example format -- %.1f -- is easier. One
    >way might be:
    >
    > double f10 = f * 10 + (f < 0 ? -0.5 : 0.5);
    > printf("%d.%d\n", (int)(f10/10), (int)fabs(fmod(f10, 10)));


    That's a pretty good approach.

    You could also do this:

    int f10 = f * 10 + (f < 0 ? -0.5 : 0.5);
    printf("%d.%d\n", f10/10, abs(f10)%10);

    Assuming, of course, that -214748364.8 <= f <= 214748364.7

    --
    -Ed Falk,
    http://thespamdiaries.blogspot.com/
    Edward A. Falk, Nov 1, 2012
    #10
  11. (Edward A. Falk) writes:

    > In article <>,
    > Ben Bacarisse <> wrote:
    >>Andre <> writes:
    >>
    >>> This explode my pic16f648 a memory!!
    >>> I am using PICC Light ( as a retired I can't afford a pro )
    >>> So does anyone know about a way to convert float to ascii without
    >>> using printf??

    >>
    >>It's hard in general, but your example format -- %.1f -- is easier. One
    >>way might be:
    >>
    >> double f10 = f * 10 + (f < 0 ? -0.5 : 0.5);
    >> printf("%d.%d\n", (int)(f10/10), (int)fabs(fmod(f10, 10)));

    >
    > That's a pretty good approach.


    No it isn't! You made me look again and I saw a bug. Drat.

    > You could also do this:
    >
    > int f10 = f * 10 + (f < 0 ? -0.5 : 0.5);
    > printf("%d.%d\n", f10/10, abs(f10)%10);
    >
    > Assuming, of course, that -214748364.8 <= f <= 214748364.7


    You have it too: for negative f but where f > -0.95 the sign gets lost
    because there's no integer -0 (well, that's one way of putting it).

    If one is prepared to loose the range (I was aiming for something that
    could handle the full int range) I think, now, that I'd do:

    double f2 = fabs(f + (f < 0 ? -0.05 : 0.05));
    printf("%s%d.%d\n", f < 0 ? "-" : "", (int)f2, (int)(f2 * 10) % 10);

    which is nice and simple. To get the full int range I'd do:

    double f2 = fabs(f + (f < 0 ? -0.05 : 0.05));
    printf("%s%d.%d\n", f < 0 ? "-" : "", (int)f2,
    (int)(fmod(f2, 1) * 10) % 10);

    which is only a little more fussy, bu given the context, fmod might not
    be available.

    --
    Ben.
    Ben Bacarisse, Nov 2, 2012
    #11
  12. Andre

    Ben Guest

    Andre skrev 2012-10-30 15:23:
    > This explode my pic16f648 a memory!!
    > I am using PICC Light ( as a retired I can't afford a pro )
    > So does anyone know about a way to convert float to ascii without using
    > printf??
    > Many thanks in advances
    > Andre


    Another problem with PICC, at least was, is that you had to write your
    own putch () function which their printf used for output. There is no
    relevant stdout in a tiny microcontroller. Reading the PICC 8.83 manual,
    they are actually references to stdout but i can't find any info about
    what this stdout stream really is. Personally I usually write my own
    IO-functions for and use sprintf if I want string formatting.

    /Ben
    Ben, Nov 2, 2012
    #12
  13. Andre

    Andre Guest

    Re: probleme with printf("TB %.1f",temperature) what I did..

    Le 30/10/2012 15:23, Andre a écrit :
    > This explode my pic16f648 a memory!!
    > I am using PICC Light ( as a retired I can't afford a pro )
    > So does anyone know about a way to convert float to ascii without using
    > printf??
    > Many thanks in advances
    > Andre




    int fInput;
    int lWhole=0;
    int ulPart=0;
    char CC = ' ';
    fInput = (scratchpad[1]<<8) + scratchpad[0];
    if ( fInput & 0x8000 ) {
    fInput = ~fInput +1;
    CC = '-';
    }
    lWhole = ( fInput & 0x0FF0) >>4;
    ulPart = (fInput & 0x00F);
    printf("\n\rTE[%c%d.%.1d]",CC,lWhole,ulPart);

    Although not perfect, it fits my need.
    To answer Ben, I have downloaded and I use ser.c whit full interrupt and
    buffering. So far So good..
    Many thanks to everybody.
    Andre
    Andre, Nov 2, 2012
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    1,638
    Joe Knapka
    Jul 24, 2006
  2. Flemming Hansen

    CPU temperature reading with C

    Flemming Hansen, Mar 16, 2005, in forum: C Programming
    Replies:
    8
    Views:
    1,226
    Thomas Matthews
    Mar 17, 2005
  3. deanfamily11

    Temperature conversion

    deanfamily11, Sep 10, 2005, in forum: C++
    Replies:
    1
    Views:
    424
    Shezan Baig
    Sep 10, 2005
  4. Newbie

    CPU temperature reading

    Newbie, Dec 9, 2005, in forum: C++
    Replies:
    1
    Views:
    345
    Victor Bazarov
    Dec 9, 2005
  5. arnuld
    Replies:
    4
    Views:
    460
    arnuld
    Mar 4, 2007
Loading...

Share This Page