CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

sprintf for float
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kris



Joined: 16 Jun 2009
Posts: 3

View user's profile Send private message

sprintf for float
PostPosted: Tue Jun 16, 2009 10:30 pm     Reply with quote

Hi there,
I am using PCWHD ver4.093 (latest) but each time when I call:
Code:
sprintf(string, "%1.0f", float_value);

The system resets/restart/crash.
Can anybody tell me what's wrong? I have tried with %2.1f, %3.2f but all failed.
Thanks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 16, 2009 10:33 pm     Reply with quote

Always post your PIC.
kris



Joined: 16 Jun 2009
Posts: 3

View user's profile Send private message

I am using diPic33FJ256GP506
PostPosted: Tue Jun 16, 2009 10:53 pm     Reply with quote

I am using diPic33FJ256GP506. We have no problem with PCWH Ver3.236 with PIC18FXXX.
Since diPic33FJ256GP506 required the upgraded compiler we just bought it found sprintf failed.
Thanks!
kris



Joined: 16 Jun 2009
Posts: 3

View user's profile Send private message

Do I miss some header files
PostPosted: Tue Jun 16, 2009 10:57 pm     Reply with quote

Here is the headers I include. Any header files missing to call sprintf ?
Code:
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <math.h>
//#include <stdio.H>
#include <stdlib.h>
#include <string.h>
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 16, 2009 11:01 pm     Reply with quote

I don't have the PCD compiler, so I can't test it.
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Wed Jun 17, 2009 8:28 am     Reply with quote

This may sound stupid but did you include diPic33FJ256GP506.h ? I didn't see that header file in your list.
Guest








Yes I did
PostPosted: Wed Jun 17, 2009 12:18 pm     Reply with quote

Yes I did:
#include <33FJ256GP506.h>
#DEVICE ADC=12
.....

For now I have to make my own function to convert a float value to a string.
I believe it's a bug of PCWHD ver4.093.
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Wed Jun 17, 2009 2:02 pm     Reply with quote

Sounds like you ought to contact CCS. They aren't the fastest to respond but generally they are very helpful.
Sean631
Guest







same problem
PostPosted: Mon Nov 02, 2009 7:51 am     Reply with quote

I have the same problem with 4.085. I am compiling code for the 30F3014 My update subscription ran out, so i guess I am stuck with this problem for now. Printing floats seems like pretty basic functionality, but it appears to be broken. I've tried doing it every which way with different size floats, using sprintf to a string, etc. Its broken.
Ttelmah
Guest







PostPosted: Mon Nov 02, 2009 10:45 am     Reply with quote

Contact CCS anyway. For 'major' demonstrable faults like this, they will often offer some degree of 'grace' (usually one version with the specific problem fixed). This is so fundamental, that you need to pursue it....

As one 'totally silly' suggestion, have you tried explicitly declaring the variable as float32, float48, and float64, rather than relying on the default 'float' declaration?. I'd not put it past the compiler to accidentally be missing the overload for a default 'float', and trying to print it as the wrong type.

Have you tried without a size at all?. In the past there was a problem with the float on older compilers, that was fixable by explicitly declaring a size. I'd again consider the possibility of this being the 'reverse' fault....

Does it do the same with 'printf', rather than sprintf?. It may be that they have fixed the problem with the former. If so, you can simulate sprintf, by writing your own function to put characters into a string, and calling this with printf.

Best Wishes
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Nov 02, 2009 12:39 pm     Reply with quote

I didn't yet see the said code failing with PCD (up from V4.084). It possibly happens under specific conditions.
Can you show an example that fails reproducible?
sean631
Guest







PostPosted: Mon Nov 02, 2009 3:38 pm     Reply with quote

Found the problem.

I had this in my .h file:

#device ADC=12

I don't know why this would effect fprintf or sprintf, but when I commented the line out, I was able to print floats normally.
jmd02
Guest







same problem
PostPosted: Tue Nov 03, 2009 8:28 am     Reply with quote

Hi there,
I'm having the same problem with the version 4.099 of PCWHD.
When using the printf function trying to show a float on the LCD, the program resets.
I tried to trap the possible errors (address, stack, math, osc., dma, and so on) but no trap is executing.
I have a counter interrupt executing at 500Hz. When the timer/interrupt is disabled the printf executes ok.
I'm using a 24FJ128GB106, and already tried removing the #device... statement with no results.
Any suggestion?

thanks
Juan
sean631
Guest







PostPosted: Tue Nov 03, 2009 10:28 am     Reply with quote

I never really figured the problem out. I realized that I had another program (header file and skeleton code generated from the ccs wizard), where floats printed normally. I looked for differences between the two projects (i'm using MPLAB IDE with CCS compiler) and realized that the only difference was that I had added the #device ADC=12 line to the malfuctioning project's .h. I commented the line out and that fixed it.
My program crashed when fprintf was run with a float as an argument (%f or %g in format string).
I assume that for whatever reason, the fprintf function thought the float was a different size or format that what it actually was, causing the processor to run amok trying to print it.
sean631
Guest







PostPosted: Tue Nov 03, 2009 10:35 am     Reply with quote

I should mention that I tried IEEE formated 32-bit floats, as well as 64 bit floats - neither had any effect, program still crashed.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group