Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

float to ascii conversion MPLAB C18 problem

Status
Not open for further replies.

ahpan

New Member
Hi Guys,

I have float to ASCII function but sometime it displays the output on GLCD after converting float into ASCII and i can print on GLCD but sometime it stops. One of friend on forum suggested to use buffer instead of pointer.


The code for float to ASCII is as below:

Code:
void ftoa(float value, char *string) 
 { 
     if (value < 0) { 
         *string++ = '-'; 
         value = -value; 
     } 
     
	sprintf(string, (const far rom char *) "%lu.%02u", 
                     (long) value, 
                     (int) ((value - (long) value) * 100. + 0.5));
 }

I am receiving value via EUSART and storing in 8 byte long buffer and converting into float and it is working fine using the below code:

Code:
unsigned char buf[9]; //array to hold 9 bytes receive whole string via EUSART
unsigned char buffer[7];//array to hold 7 bytes , print the string of interest 

static float floatval;//

char *str; /* Pointer  */


// receive whole string 
for (i=0; i < 9; i++)
		{
        while (PIR1bits.RCIF == 0)       //wait until there is a character ready
                ;       //do nothing
        buf[i] = RCREG; //copy received char into array at current index position
		}	
// print the whole string on GLCD (8 byte long) 
for (i=1; i < 9; i++) // Print on the GLCD
		{
		PutChar(buf[i]);
		};

// print the string without any alphanumeric character (7 byte long) 
for (i=1;i<8;i++)
	{
	buffer[i] = buf [i];
	}

floatval = atof(buf);  // ASCII to float conversion

ftoa(floatval,str);	// Float to ASCII conversion
	
while ( *str ) {  // Put String on GLCD
	PutChar(*str);
	str++; 	
	}

Help is appreciated

Cheers
 
debug_window.JPG
 
Code:
sprintf(string, (const far rom char *) "%lu.%02u", 
                     (long) value, 
                     (int) ((value - (long) value) * 100. + 0.5));

Why the cast????? What are you going to do with the string afterwards??


ALSO???????

Code:
floatval = atof(buf);  // ASCII to float conversion
 
ftoa(floatval,str);	// Float to ASCII conversion
 
Thanks for your reply.

Once I receive the data I want to convert into other unit. I am receiving the data which is in terms of tonnes (8 character long string).
e.g. 0001.87t

I have to convert into kilonewton(kN). To do so I have to convert the received string into float.

Code:
floatval = atof(buf);  // ASCII to float conversion
kN = floatval* 9.806 ; // convert to kN , I forgot to include in first post 
ftoa(floatval,str);	// Float to ASCII conversion

after this conversion I have to print on the GLCD so again I can't display the float directly therefore need to convert back to string using ftoa.

Cheers
 
The first line is the whole string
The second line is in the kN.

It does work but to make it work I have to reset two to three time (using MCLR).

Cheers
 

Attachments

  • photo (20).JPG
    photo (20).JPG
    434.5 KB · Views: 267
You mean the GLCD doesn't work first time??? I'm having that issue as well at the moment... Its a T6963C but I have to reset more than three times before it runs...

I'm looking into it as we speak...
 
Many Thanks Ian for your time.

GLCD doesn't work first time. Need to reset many time to get it work. Once it starts, GLCD works fine

Cheers
 
Hi Ian,

If I am not wrong the pointer does not allocated any memory for buffers:

char* str;

This is just a pointer which point somewhere in the memory. I am trying to define this way so that actual memory is allocated.

char new[20]; /* Storage for 20 characters */

str=new;

What do you reckon. Does it help ?

Cheers
 
If you increment your pointer like this there is no way to come back to the start of the string. Use proper indexing like I showed you in the other thread.

Bad method:
Code:
char str[20] /* memory for 20 characters */

ftoa(floatval,str);	// Float to ASCII conversion

while ( *str ) {  // Put String on GLCD
	PutChar(*str);
	str++; 	
	}

Good method:
Code:
char str[20] /* memory for 20 characters */

ftoa(floatval,str);	// Float to ASCII conversion

int i=0;
while ( str[i] ) // Put String on GLCD
{ 
    PutChar(str[i]);
    i++; 
}
 
Last edited:
Bad method:

Code:
char str[20] /* memory for 20 characters */
 
ftoa(floatval,str);	// Float to ASCII conversion
 
while ( *str ) {  // Put String on GLCD
	PutChar(*str);
	str++; 	
	}

Oh dear... I must be VERY bad :D:D:D

As the string is no longer required... Who cares...
 
As the string is no longer required... Who cares...

I assumed that the code is called periodically. Incrementing a pointer works fine only if you do it one time..
 
Last edited:
Thanks Ian and misterT for your valuable response

@misterT as you quoted post#10 gives me an error. Because the function for float ASCII conversion is as below :

Code:
void ftoa(float value, char *string) 
 { 
     if (value < 0) { 
         *string++ = '-'; 
         value = -value; 
     } 
 
	sprintf(string, (const far rom char *) "%lu.%02u", 
                     (long) value, 
                     (int) ((value - (long) value) * 100. + 0.5));
 }

Cheers
 
Hi misterT

Got it working. You were correct except this
Code:
char str[20]; // 

ftoa(mod,str);	// Float to ASCII conversion
 
for (i=0; i < 20; i++) // Print on the GLCD // Put String on GLCD
		{ 
			PutChar(str[i]);
			 
		}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top