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 CCS Technical Support

Float to String conversion

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
matheusmestres28



Joined: 30 Apr 2013
Posts: 20

View user's profile Send private message

Float to String conversion
PostPosted: Tue Apr 30, 2013 1:35 pm     Reply with quote

Code:

#include <12F675.h>
#include <string.h>
#device   adc=10

#FUSES NOWDT                   
#FUSES XT                     

#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_A2)

float vx,ax;
char str1[10];

void main() {
   set_tris_a(0b00100000);
   setup_adc_ports(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);

   while(TRUE){
            set_adc_channel(0);
            delay_us(10);       
            vx = read_adc();
            vx = vx*0.0048875855327468;
            ax = (vx-1.65)*12.25;   
            str1 = ax.c_str();
            puts("The acceleration, in the x-axis, is: \n\r");
            puts(str1);
   }

}

I'm trying to convert the variable ax into a string (str1) to use as a parameter of puts function.

I'm not using printf because it demands more ROM memory.

I'm not used to deal with CCS and I'm a terrible programmer, but in Borland C++ Builder I used the .c_str() function (or FloatToStr()). I tried to use it in this code, but the compiler says there is an error.

Someone can help me??

I'm sorry if I made any mistakes while posting the code, it's my first post here.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 30, 2013 1:55 pm     Reply with quote

Quote:
I'm not using printf because it demands more ROM memory.

Your current program uses 70% of the PIC's ROM space. This leaves
only about 300 ROM words for a float to ASCII routine. My advice is
to use a larger PIC, or re-arrange your math so it doesn't use floating point.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Apr 30, 2013 2:12 pm     Reply with quote

listen to PCM and redo your code, perhaps to scale your data *100

2 hints for instance:

if scaled *100
* 12.25 is no different than
( *1225 ) /10000; or (*2007)/163384

16384 requires no division - just a shift
-------------
0.0048875855327468;
is appx 1 part in 205
and is the same ratio to .08% error
as (*80)/16384

scaling this to the an error contribution you can stand
with the same approach as example #1 could save a lot of code space
and cycles
----------------
long integer mult math math, with SHIFTS for division will shrink code space and improve speed at the same time.
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Tue Apr 30, 2013 2:32 pm     Reply with quote

If you did have enough memory space, the routine to do the conversion is printf, or sprintf (latter to a string, former directly to the output).
However you do need to think again.
Also be aware that CCS has a built in ability in printf/sprintf, to output an integer as if it is a scaled float, with %w.

Best Wishes
matheusmestres28



Joined: 30 Apr 2013
Posts: 20

View user's profile Send private message

PostPosted: Tue Apr 30, 2013 2:32 pm     Reply with quote

yeah, I agree, I thought there were some way to do this, and my teacher told me to try it, but that's not the whole code. I have to set 2 other pins for the adc and do the same thing with them. I think I'll use pic18f1320.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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