display float on 16x2 LCD in PIC 16F877 using Hi-tech C compiler

Thread Starter

ecaits

Joined Jan 6, 2014
56
Hi friends,

I want to display float value on LCD display in PIC16F877 using Hi-tech C compiler.

Can anybody help me???
 

Ian Rogers

Joined Dec 12, 2012
1,136
You need to write a little routine that converts a float to ascii..

Here is a very crass routine that does up to 7 digits..

Rich (BB code):
void ftoa(float f,char *buf)
{
    int pos=0,ix,dp,num;
    if (f<0)
    {
        buf[pos++]='-';
        f = -f;
    }
    dp=0;
    while (f>=10.0) 
    {
        f=f/10.0;
        dp++;
    } 
    for (ix=1;ix<8;ix++)
    {
            num = f;
            f=f-num;
            if (num>9)
                buf[pos++]='#';
            else
                buf[pos++]='0'+num;
            if (dp==0) buf[pos++]='.';
            f=f*10.0;
            dp--;
    }
}
 
Last edited:

MrChips

Joined Oct 2, 2009
30,706
There are some typos in your code.

if (f0)

should be

if (f<0)

------------

for (ix=1;ix8;ix++)

should be

for (ix=1;ix<8;ix++)
 

Ian Rogers

Joined Dec 12, 2012
1,136
Not my code... This is from Hitech... Good or what..

I haven't used it for negative floats.... so It never used that part... But I do remember the other typo!! but I don't use floats anymore so it has been a while...

At least you have reminded me to update it... Cheers MrChips...
 

t06afre

Joined May 11, 2009
5,934
I can find ftoa() on the smaller compiler... It seems to be included with the pic18 (hi-tech) only.... This is why I post it here...
It is included in the 9.83 version of the Hi-Tech C compiler. And also in the XC8 compiler. However what we did not mention. Was the fact that it can be more easy often to work with integers. Then place the decimal sign in code. If you use the ftoa function. The use of printf will also follow in this case. And the latter function will require about 2K of code in the ROM. Then using the free version
 

Ian Rogers

Joined Dec 12, 2012
1,136
That cheap version of ftoa() doesn't use a great deal of code... BUT!! of course you are correct, fixed point math is better all round on these small ram devices...
 

MrChips

Joined Oct 2, 2009
30,706
Not my code... This is from Hitech... Good or what..

I haven't used it for negative floats.... so It never used that part... But I do remember the other typo!! but I don't use floats anymore so it has been a while...

At least you have reminded me to update it... Cheers MrChips...
If someone were to attempt to use that code with the typos it would not compile since f0 and ix8 would be flagged as undefined.
 

MrChips

Joined Oct 2, 2009
30,706
If the OP can specify the number of fractional decimal places it would be a lot simpler and more efficient to use fixed point integer arithmetic.
 

Ian Rogers

Joined Dec 12, 2012
1,136
Ok!!! The code was rectified.... two little "<" missing!!! I checked the source and they were there... After posting they weren't.... I used textpad as a container... something must have happened as like I say... the original code is correct....
 

THE_RB

Joined Feb 11, 2008
5,438
...
I haven't used it for negative floats.... so It never used that part... But I do remember the other typo!! but I don't use floats anymore...
...
Funny that! The more experience and skill you acquire in embedded C the less you use floats.

I had to use one a while back, and found myself thinking; "What the heck have I done wrong here that is forcing me to use one of these mongrel floats? I stopped using those years ago..."
 

Ian Rogers

Joined Dec 12, 2012
1,136
RB said:
Funny that! The more experience and skill you acquire in embedded C the less you use floats.
I agree.. I haven't used them for ages... BUT!! I did a project the other day using floats... They aren't as accurate... Any precision required then its easier to use lookups... Imagine all the calculations I need to use for the geometry alone...

If the OP doesn't respond, we could use this thread to go into detail!! LOL...
 
Top