View previous topic :: View next topic |
Author |
Message |
matheusmestres28
Joined: 30 Apr 2013 Posts: 20
|
Float to String conversion |
Posted: Tue Apr 30, 2013 1:35 pm |
|
|
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
|
|
Posted: Tue Apr 30, 2013 1:55 pm |
|
|
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
|
|
Posted: Tue Apr 30, 2013 2:12 pm |
|
|
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: 19540
|
|
Posted: Tue Apr 30, 2013 2:32 pm |
|
|
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
|
|
Posted: Tue Apr 30, 2013 2:32 pm |
|
|
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. |
|
|
|