简体   繁体   中英

How to write floating value on LCD?

I am calculating body mass ratio, which is floating value like 862.145, 56.21,664.008 this value I want to put on 16x2 lcd I am using PIC18F4550 with MPLAB IDE v8.83

here is my code

float BMR_Male(unsigned char Age,unsigned int Weight,unsigned int Height)
{
    float BmrForMale = 0,term1=0,term2=0,term3=0;
    int intValue = 0,anotherIntValue = 0;
    float diffValue = 0;
    char str1[20],str2[20];
    unsigned age1 = Age;
    unsigned int Weight1 = Weight;
    unsigned int Height1 = Height;
    term1 = 13.75 * Weight;
    intValue = (int)term1;
    diffValue = term1 - (float)intValue;
    anotherIntValue = (int)(diffValue * 1000.0);
    sprintf(str1, (const far rom char*)"%s", intValue);
    sprintf(str2, (const far rom char*)"%s", anotherIntValue);
    for(z=0;z!='\0';z++)
    {
        LCD_write(str1[z]);    
        myMsDelay(5);
    }
    LCD_write('.');
    myMsDelay(5);
    for(z=0;z!='\0';z++)
    {
        LCD_write(str2[z]);    
        myMsDelay(5);
    }
//  term2 = 5 + Height1;
//  term3 = 6.76 * age1;
//  BmrForMale = (term1)+(term2)-(term3)+66;
    return BmrForMale ;
}

here i have displayed only one term... term2 and term3 is commented for simplicity..

here i am getting only '.' on lcd .. not str1 and str2

There is absolutely no reason to use float in this case. Instead of writing things like 13.75 in the code, you can as well write 1375 . Try to restrict calculations to 8 and 16 bit integers. And when all calculations are done, print the decimal comma wherever you want it.

The difference is that your PIC18 compiler will have to barf out an extremely slow and memory consuming program if you use floating point numbers. You are not programming a PC! You don't have a FPU and we're talking about what might very well be the slowest CPU in the world still in production here...

You only need to use floating point numbers if your program is doing things like trigonometry, signal processing, advanced math etc etc. In which case you picked the completely wrong MCU.

Similarly, your program will also turn extremely slow and memory consuming if you use the printf family of functions. sprintf will probably kill every single resource available in your PIC. In this case, simply roll out your own integer to string conversion routine. It is trivial and there is plenty of code for such available on SO.

Given that you only have a few kb of RAM available, your present code most likely has stack overflow and has probably consumed all available RAM with this function alone.

In your code, you're using wrong format specifier. Change

sprintf(str1, (const far rom char*)"%s", intValue);
sprintf(str2, (const far rom char*)"%s", anotherIntValue);

to

sprintf(str1, "%d", intValue);
sprintf(str2, "%d", anotherIntValue);

as, intValue and anotherIntValue are of type int which needs %d format specifier.

Change

sprintf(str1, (const far rom char*)"%s", intValue);
sprintf(str2, (const far rom char*)"%s", anotherIntValue);

To

sprintf(str1, "%d", intValue);
sprintf(str2, "%d", anotherIntValue);

But you can simply use

snprintf(str1, 20, "%f", term1);
z=0;
while(str1[z++]!= '\0')
{
    LCD_write(str1[z]);    
    myMsDelay(5);
}

or

int strlen = snprintf(str1, 20, "%f", term1);
for (z=0; z<strlen; z++)
{
    LCD_write(str1[z]);    
    myMsDelay(5);
}

The only thing to check is if the '.' char has the same code in your LCD. If not check and change it to the correct value before sending to LCD through LCD_write() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM