String conversion into float point

Discussion on projects that are created by users and posted on mikroElektronika website.
Post Reply
Author
Message
bilal.iq.lhr
Posts: 28
Joined: 10 Aug 2016 18:16

String conversion into float point

#1 Post by bilal.iq.lhr » 09 Oct 2016 16:31

Code: Select all

char text[] = "G01 X97.166667 Y-5.794118 Z4.666667";
char zh;
char g_code;
char x_axis;
char y_axis;
char z_axis;
char x_txt[] = "9";
char y_txt[] = "5";
char z_txt[] = "0";
float x_res;
float y_res;
float z_res;
char xreturn;
char yreturn;
char zreturn;
sbit LCD_RS at RA0_bit;
sbit LCD_EN at RA1_bit;
sbit LCD_D4 at RA2_bit;
sbit LCD_D5 at RA3_bit;
sbit LCD_D6 at RA4_bit;
sbit LCD_D7 at RA5_bit;
sbit LCD_RS_Direction at TRISA0_bit;
sbit LCD_EN_Direction at TRISA1_bit;
sbit LCD_D4_Direction at TRISA2_bit;
sbit LCD_D5_Direction at TRISA3_bit;
sbit LCD_D6_Direction at TRISA4_bit;
sbit LCD_D7_Direction at TRISA5_bit;
void main() {
ANSEL = 0x00;
ANSELH = 0x00;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
zh = strtok(text, ' ');
Lcd_Out(1,1,zh);
x_axis = strtok(0, " ");
Lcd_Out(2,1,x_axis);
y_axis = strtok(0, " ");
Lcd_Out(3,1,y_axis);
z_axis = strtok(0, " ");
Lcd_Out(4,1,z_axis);
//x_res = atof(x_axis);
//y_res = atof(y_axis);
//z_res = atof(z_axis);
//floattostr(x_res, xreturn);
//floattostr(y_res, yreturn);
//floattostr(z_res, zreturn);
//Lcd_Out(1,1,xreturn);
//Lcd_Out(2,1,yreturn);
//Lcd_Out(3,1,zreturn);

}
proble is this
at point Lcd_Out(4,1,z_axis) i have 4 values which are as
zh
x_axis
y_axis
z_axis
i can also remove x y z from the starting of the string.
but unable to convert strings in to float for calculation purpose
and again turning float back to string for display purpose
any ideas and help plz waiting.

bilal.iq.lhr
Posts: 28
Joined: 10 Aug 2016 18:16

Re: String conversion into float point

#2 Post by bilal.iq.lhr » 09 Oct 2016 16:34

I want this when data comes from UART terminal and save at *text then it is broken into tokens
and then each token is converted into floating point type and after calculations result will turn back into string from float
in C lang sprintf is a good function but on helping site of mikroC libraries there is example of sprintf but compiler donot have sprintf library init and thats why still stucked at that point.
unable to find stringf.mpkg at libstock

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: String conversion into float point

#3 Post by Aleksandar.Mitrovic » 10 Oct 2016 16:45

Hi,

Can you for the start tell me which compiler exactly are you using?

Maybe it will be easier to post whole project in small zipped form which we can use for testing.
More information You should try to find in the compiler Help page and example for sprintf in the compiler.

Kind regards,
Aleksandar

bilal.iq.lhr
Posts: 28
Joined: 10 Aug 2016 18:16

Re: String conversion into float point

#4 Post by bilal.iq.lhr » 11 Oct 2016 03:45

thanks for replaying.
my compiler is mikro c pro 5.5.6 the problem is this no
stringf.library in compiler only stringi and l are which will not act for float or double value.
my mcu is pic16f887
and today i ll upload my complet project
with problematic area

bilal.iq.lhr
Posts: 28
Joined: 10 Aug 2016 18:16

Re: String conversion into float point

#5 Post by bilal.iq.lhr » 11 Oct 2016 15:40

and i m trying to prase string for my job
string is Gcode of the software output but unable to prase the string

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: String conversion into float point

#6 Post by Aleksandar.Mitrovic » 12 Oct 2016 10:13

Hi,

Did you forget to add project?

Kind regards,
Aleksandar

bilal.iq.lhr
Posts: 28
Joined: 10 Aug 2016 18:16

Re: String conversion into float point

#7 Post by bilal.iq.lhr » 15 Oct 2016 05:13

both files are added
pic 16f887

bilal.iq.lhr
Posts: 28
Joined: 10 Aug 2016 18:16

Re: String conversion into float point

#8 Post by bilal.iq.lhr » 15 Oct 2016 05:21

code file
Attachments
13-october.rar
(46.81 KiB) Downloaded 277 times

bilal.iq.lhr
Posts: 28
Joined: 10 Aug 2016 18:16

Re: String conversion into float point

#9 Post by bilal.iq.lhr » 17 Oct 2016 02:00

my question is this
when i m having double value then why i m unable to view them on screen.
the values are true as u can see that checked with the help of if statement.
and if u find any thing plz let me know that thanks
Attachments
string conversion.rar
(34.29 KiB) Downloaded 309 times

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: String conversion into float point

#10 Post by Aleksandar.Mitrovic » 17 Oct 2016 16:37

Hi,

I have made you small example, you can start from here:

Code: Select all

char text[40] ="G01 X97.166667 Y-5.794118 Z4.666667";

float  xC, yC, zC;


char *res;

struct Cordinates
{
char xG[9];
char yG[9];
char zG[9];
}axis;

void main()
{
  ANSEL = 0x00;
  ANSELH = 0x00;
  TRISD = 1;
  
 res = strchr(text,'X');          // Pointer to X location in the string
 strncat(axis.xG,res+1, 9);       // copy 9 caracters from the location+1

 res = strchr(text,'Y');          // Pointer to Y location in the string
 strncat(axis.yG,res+1, 9);       // copy 9 caracters from the location+1

 res = strchr(text,'Z');          // Pointer to Z location in the string
 strncat(axis.zG,res+1, 9);       // copy 9 caracters from the location+1
 
 // convert to float
 xC = atof(axis.xG);
 yC = atof(axis.yG);
 zC = atof(axis.zG);

}
Kind regards,
Aleksandar

Post Reply

Return to “User Projects”