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.

LCD and float question

Status
Not open for further replies.

pxds

New Member
I'm on dire need of displaying a float value on the LCD. The problem is, i can't do it :(

So far i've tried:

void func(void){
float x=3.14;
char buf[16];
sprintf(buf,"%f",x);
*send buf to the LCD*
}

Also tried the code on **broken link removed** wich converts float to string.

Both have failed and now i need your help.
 
Last edited by a moderator:
You could print the integer part and then do a fixed number of decimal places.

Something like,
Code:
    float x=3.1416;
    myint=(int)x;
    //send myint to LCD
    //send decimal point to LCD
    x-=myint;	//keep only decimal part
    for(i=0;i<4;i++){
        x*=10;
        myint=(int)x;
        x-=myint;
        //send myint to LCD
    }

Mike.
 
didn't work :/ i even tried something similar, where i divide the float into high and low int parts. something like:

Code:
        float f;
	int hi, lo;
	char buf[16];
        f = 25.64;
	hi = (int)f;
	lo = (int)(f - hi)*100;
        sprintf(buf,"%d.%d", hi, lo);
        // send buf to LCD

but all i get on the LCD is 25.0, any help?
 
char buf[8];
sprintf(buf,"%d.%02u", (int)f , ((int) ((f)-(int)f) * 100) );

this code was taken from Microchip oficial forum, but it gives the same 25.00 on the LCD. damn this is starting to bug me...
 
didn't work :/ i even tried something similar, where i divide the float into high and low int parts. something like:

Code:
        float f;
	int hi, lo;
	char buf[16];
        f = 25.64;
	hi = (int)f;
	lo = (int)(f - hi)*100;
        sprintf(buf,"%d.%d", hi, lo);
        // send buf to LCD

but all i get on the LCD is 25.0, any help?

Try this.
Multiply your floatx1000 into a long:

buf=voltfp((unsigned long) f*1000.0), buf);

Code:
char* voltfp(unsigned long millvolts, char *strprt)             // convert unsigned long (millvolts) to voltage string
{
    long                        iw = 0, ip = 0;
    float                       voltfrak;
    voltfrak = (float)millvolts / 1000;
    iw = (long)((float)voltfrak);
    ip = (long)((float)voltfrak * 100) - iw * 100;
    sprintf(strprt, "%d.%02d", (int)iw, (int)ip);
    return strprt;
}

Code:
int     ABS(int i)
{
    if (i < 0)
        return -i;
    else
        return i;
}

char* ahfp(long millah, char *strprt)           // convert long (.1 of Ah) to Ah string
{
    int                         iw = 0, ip = 0;
    float                       ahfrak;
    char                        sign = '+';

    if (millah < 0) sign = '-';

    ahfrak = (float)millah / 10;
    iw = (long)((float)ahfrak);
    ip = (long)((float)ahfrak * 10) - iw * 10;
    sprintf(strprt, "%c%d.%01d", sign, ABS(iw), ABS(ip));
    return strprt;
}
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top