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

A

Andre

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
 
J

James Kuyper

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().
 
E

Eric Sosman

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.
 
B

Ben Bacarisse

Andre said:
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.
 
J

Johannes Bauer

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

--
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 <[email protected]>
 
J

Johannes Bauer

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

--
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 <[email protected]>
 
J

James Kuyper

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.
 
A

Andre

Le 30/10/2012 16:37, Johannes Bauer a écrit :
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é
 
B

Ben

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
 
E

Edward A. Falk

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
 
B

Ben Bacarisse

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.
 
B

Ben

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
 
A

Andre

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top