News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Dig command for floats

Started by Fanie, Sep 05, 2025, 03:14 PM

Previous topic - Next topic

Fanie

How do one modify the Dig command to display a float value ?

Multiply the float value with 100 (for 2 decimals), load into a word or Dword and then display ?

trastikata

Quote from: Fanie on Sep 05, 2025, 03:14 PMMultiply the float value with 100 (for 2 decimals), load into a word or Dword and then display ?

Load it in SDword to allow for negative values.

top204

#2
If extracting seperate ASCII digit characters from a Floating Point value, as the Dig function does with integer values. It does not matter if the value is positive or negative.

So a procedure can be created to extract ASCII digits, by converting the Floating Point value into a String, then reading an element from the String that corresponds to the Digit required to read. For example, the simplistic procedure in the code listing below, does just that:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes pMin PIC Tick!
'
' Demonstrate a type of Dig procedure for a Floating Point value
' Written for the Positron8 compiler by Les Johnson.
'
    Device = 18F25K20                                       ' Tell the compiler what device to compile for
    Declare Xtal = 16                                       ' Tell the compiler what frequency the device will be operating at (in MHz)
    Declare Float_Display_Type = Fast                       ' Tell the compiler to use its more accurate Float to ASCII converter library routine 
'
' Setup USART1
'
    Declare Hserial_Baud = 9600                             ' Set the Baud rate to 9600
    Declare HRSOut1_Pin  = PORTC.6                          ' Set the TX pin
'
' Create Global variables here
'     
    Dim bDigitChar As Byte                                  ' Holds the returned ASCII digit character
    Dim bDigit     As Byte                                  ' Holds which digit to read from the Floating Point value
    Dim MyFloat    As Float                                 ' Holds the Floating Point value to read
 
'----------------------------------------------------------------------------
' The main program starts here
' Extract ASCII digits from a Floating Point value, and transmit them to a serial terminal
'
Main:
    MyFloat = -123.456
    For bDigit = 0 To 6                                     ' Create a loop for the amount of characters to read from the floating point value
        bDigitChar = Dig_Float3(MyFloat, bDigit)            ' Return an ASCII digit character
        HRSOut1 bDigitChar                                  ' Transmit it to a serial terminal
    Next
    HRSOut1 13                                              ' Terminate the serial data
   
'----------------------------------------------------------------------------
' Return an ASCII digit value from a Floating Point value
' Input     : pValue holds the Floating Point value to return a digit from
'           : pDigit holds which digit to read, from Right to Left of the value
' Output    : Returns the ASCII value of the digit
' Notes     : Because the Floating Point value is converted to a String, it will always use 3 digits as the remainder (Dec3)
'
Proc Dig_Float3(pValue As Float, pDigit As Byte), Byte
    Dim DigStr As String * 16 Heap                          ' Holds the converted Floating Point value
    Dim bLen   As Byte                                      ' Holds the length of the Floating Point value
      
    pValue = Abs(pValue)                                    ' Make the Floating Point value always positive
    DigStr = Str$(Dec3, pValue)                             ' Convert the Floating Point value to a String, with a 3 digit remainder
    bLen = Len(DigStr)                                      ' Find the amount of characters in the Floating Point value
    bLen = bLen - 1                                         ' Subtract 1 from the length, because a String's elements start at 0  
    Result = DigStr[bLen - pDigit]                          ' Return the element from the pDigit position of DigStr
EndProc

On the serial terminal, it will show:

654.321

Because the digits do not change if it is negative, and they are read from right to left, as the Dig function does for integers.

Fanie

Thank you Les.  From the comments I got an idea of what it is supposed to do.
My programming skill is far below yours, I'll do something tomorrow.