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

Discussion in 'Embedded Systems and Microcontrollers' started by ecaits, Feb 17, 2014.

  1. ecaits

    Thread Starter Member

    Jan 6, 2014
    52
    0
    Hi friends,

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

    Can anybody help me???
     
  2. WBahn

    Moderator

    Mar 31, 2012
    17,716
    4,788
    What part of this is causing you problems?
     
  3. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    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..

    Code ( (Unknown Language)):
    1.  
    2. [COLOR=#000000][COLOR=#0000BB]void ftoa[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]float f[/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000BB]char [/COLOR][COLOR=#007700]*[/COLOR][COLOR=#0000BB]buf[/COLOR][COLOR=#007700])
    3. {
    4.     [/COLOR][COLOR=#0000BB]int pos[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000BB]0[/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000BB]ix[/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000BB]dp[/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000BB]num[/COLOR][COLOR=#007700];
    5.     if ([/COLOR][COLOR=#0000BB]f[/COLOR][COLOR=#0000BB]<0[/COLOR][COLOR=#007700])
    6.     {
    7.         [/COLOR][COLOR=#0000BB]buf[/COLOR][COLOR=#007700][[/COLOR][COLOR=#0000BB]pos[/COLOR][COLOR=#007700]++]=[/COLOR][COLOR=#DD0000]'-'[/COLOR][COLOR=#007700];
    8.         [/COLOR][COLOR=#0000BB]f [/COLOR][COLOR=#007700]= -[/COLOR][COLOR=#0000BB]f[/COLOR][COLOR=#007700];
    9.     }
    10.     [/COLOR][COLOR=#0000BB]dp[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000BB]0[/COLOR][COLOR=#007700];
    11.     while ([/COLOR][COLOR=#0000BB]f[/COLOR][COLOR=#007700]>=[/COLOR][COLOR=#0000BB]10.0[/COLOR][COLOR=#007700])
    12.     {
    13.         [/COLOR][COLOR=#0000BB]f[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000BB]f[/COLOR][COLOR=#007700]/[/COLOR][COLOR=#0000BB]10.0[/COLOR][COLOR=#007700];
    14.         [/COLOR][COLOR=#0000BB]dp[/COLOR][COLOR=#007700]++;
    15.     }
    16.     for ([/COLOR][COLOR=#0000BB]ix[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000BB]1[/COLOR][COLOR=#007700];[/COLOR][COLOR=#0000BB]ix[/COLOR][COLOR=#0000BB]<8[/COLOR][COLOR=#007700];[/COLOR][COLOR=#0000BB]ix[/COLOR][COLOR=#007700]++)
    17.     {
    18.             [/COLOR][COLOR=#0000BB]num [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]f[/COLOR][COLOR=#007700];
    19.             [/COLOR][COLOR=#0000BB]f[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000BB]f[/COLOR][COLOR=#007700]-[/COLOR][COLOR=#0000BB]num[/COLOR][COLOR=#007700];
    20.             if ([/COLOR][COLOR=#0000BB]num[/COLOR][COLOR=#007700]>[/COLOR][COLOR=#0000BB]9[/COLOR][COLOR=#007700])
    21.                 [/COLOR][COLOR=#0000BB]buf[/COLOR][COLOR=#007700][[/COLOR][COLOR=#0000BB]pos[/COLOR][COLOR=#007700]++]=[/COLOR][COLOR=#DD0000]'#'[/COLOR][COLOR=#007700];
    22.             else
    23.                 [/COLOR][COLOR=#0000BB]buf[/COLOR][COLOR=#007700][[/COLOR][COLOR=#0000BB]pos[/COLOR][COLOR=#007700]++]=[/COLOR][COLOR=#DD0000]'0'[/COLOR][COLOR=#007700]+[/COLOR][COLOR=#0000BB]num[/COLOR][COLOR=#007700];
    24.             if ([/COLOR][COLOR=#0000BB]dp[/COLOR][COLOR=#007700]==[/COLOR][COLOR=#0000BB]0[/COLOR][COLOR=#007700]) [/COLOR][COLOR=#0000BB]buf[/COLOR][COLOR=#007700][[/COLOR][COLOR=#0000BB]pos[/COLOR][COLOR=#007700]++]=[/COLOR][COLOR=#DD0000]'.'[/COLOR][COLOR=#007700];
    25.             [/COLOR][COLOR=#0000BB]f[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000BB]f[/COLOR][COLOR=#007700]*[/COLOR][COLOR=#0000BB]10.0[/COLOR][COLOR=#007700];
    26.             [/COLOR][COLOR=#0000BB]dp[/COLOR][COLOR=#007700]--;
    27.     }
    28. }
    29. [/COLOR][/COLOR]
    30.  
     
    Last edited: Feb 18, 2014
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Look up the function FTOA() in the manual for a start. Given that you have your LCD code working
     
  5. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    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...
     
  6. MrChips

    Moderator

    Oct 2, 2009
    12,425
    3,359
    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++)
     
  7. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    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...
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  9. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    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...
     
  10. MrChips

    Moderator

    Oct 2, 2009
    12,425
    3,359
    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.
     
  11. MrChips

    Moderator

    Oct 2, 2009
    12,425
    3,359
    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.
     
  12. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    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....
     
  13. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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..."
     
  14. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    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...
     
Loading...