Float to BCD conversion


+ Reply to Thread
Results 1 to 4 of 4

Thread: Float to BCD conversion37 days old

  1. #1
    Member keytapper's Avatar
    Join Date
    Jun 2019
    Posts
    195
    Thumbs Up
    Received: 8
    Given: 5
    Total Downloaded
    331.75 MB

    0 Not allowed!

    Default Float to BCD conversion

    Hello forum,
    I'm struggling to display a float to a 7 segments display, with 5 or 6 digits. In case of negative the sixth digit it would be just the minus sign.

    So I went to read the __Float_2_Ascii_ internal routine. Well I couldn't understand much of that. Surely I haven't studied for long.

    I'd like if there will be a middle way that for each ASCII character it just subtract 48 and the dot, might remain into a buffer rather than currently send out for display.

    Currently I tried to transfer the float to a Word (so 5 digits are sufficient) but the result is not clear.

  2. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  3. #2
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,594
    Thumbs Up
    Received: 339
    Given: 162
    Total Downloaded
    1.99 GB

    0 Not allowed!

    Default Re: Float to BCD conversion

    Using floating point for displays is not really the way to do it. Floating point is not 100% accurate because of the way it is packed. Remember, floating point is just a package of bits to re-organise a value, however, if the value cannot be packed with the rotations required, the remaining digits will not be accurate. In the past, and present, space flights have failed because the "professional" programmers used floating point in their calculations and the inaccuracies of the floating point format accumulated with each calculation, so when the final value was used, it was so far off it caused crashes or misses. The same has happened in many environments that require very accurate calculations, but have actually caused chaos because the programmer did not understand the methodology of floating point, or its pitfalls. Even 64-bit floating point is not fully accurate, so the accumulations of the errors over time will cause failures.

    Yet these silly interpreted languages now available on PC machines default to floating point for their variables, just like the old 8-bit languages from the 1980s! And they say we learn from the past.!!!! B#llSh#t .

    For displays, it is always better to use integer values and, if required, split the floating point into integers, or better still, drop the floating point altogether and only use integers.

    However, for your requirement you can use the compiler's Str$ command:

    Code:
        Dim MyString As String * 20
        
        MyString = Str$(Dec2, 3.14) 
        HRSOutLn MyString
    The above will convert the floating point into a string with enhanced 14-bit core and 18F devices, then it is relatively straightforward to parse the string onto an LED display by scanning it and converting the ASCII characters to the digit values required. But it is not recommended.

    For displaying values with a fractional value, it is always better to use integer divides and use a method of fixed point for the display.
    Last edited by top204; 24th April 2020 at 12:14.

  4. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  5. #3
    Member keytapper's Avatar
    Join Date
    Jun 2019
    Posts
    195
    Thumbs Up
    Received: 8
    Given: 5
    Total Downloaded
    331.75 MB

    0 Not allowed!

    Default Re: Float to BCD conversion

    As always, you're great Mr. Les !!
    The clue is just the greatest.
    I fully agreed that the floating point is just a mere attempt to get fractional number. Furthermore they need to be slimmed down to remain in a small space. As such I dislike to use floating points. I think that using a multiplier and a divider will give me a similar approximation for the result to display over 5~6 digits display.

    I opted to the floating point, just because in term of memory space it's rather similar to calculating with multiplier/divider style.

    Anyways adding strings to the code will create another issue, because I'm currently writing for a 14bit core MCU.

  6. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  7. #4
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,594
    Thumbs Up
    Received: 339
    Given: 162
    Total Downloaded
    1.99 GB

    0 Not allowed!

    Default Re: Float to BCD conversion

    Floating point calculations are a whole lot larger and slower than any integer calculations.

    The compiler makes it look easy and simple and fast in the user's listing, but its underlying assembler code is a totally different matter. :-) Floating point takes a lot more processing on standard microcontrollers and microprocessors, unless they have an FPU attached (Floating Point Unit), and for 8-bit microcontrollers, the time, RAM and code space is significantly higher. So for a smaller device, especially a 14-bit core type, floating point should only be used when absolutely required. For PIC24 and dsPIC33 devices, floating point is a whole lot faster and smaller because these devices have barrel shifting capabilities, hardware divide and hardware multiply, which makes them extremely efficient. That's why I added 64-bit floating point to Proton24, because they are so efficient.

  8. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Replies: 2
    Last Post: 4th January 2015, 15:40
  2. String to Float Conversion
    By SELCUK in forum Proton Plus Compiler v3
    Replies: 4
    Last Post: 25th March 2008, 15:07
  3. BCD and BIN
    By eleckits in forum Proton Plus Compiler v3
    Replies: 3
    Last Post: 17th September 2007, 10:33
  4. Word value to BCD
    By Peter D in forum Proton Plus Compiler v3
    Replies: 10
    Last Post: 4th May 2007, 05:23

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts