FloatToStrSE

SwordfishUser.FloatToStrSE History

Hide minor edits - Show changes to output

Changed line 175 from:
  data_in4 = data_in4 + 0.999
to:
  data_in4 = data_in4 + 0.9
Changed line 13 from:
!!!Module Code
to:
!!!Example Code
Added lines 15-25:
Include "Float_conv"

Dim data_str As String(18)
   
FloatToStrSE(-3.1,data_str,5)

// data_str will be "-3.10000"
=]

!!!Module Code
=code [=
Added lines 1-181:
FloatToStrSE module available for SwordFish SE.

This function is smaller, faster and work under SwordFish SE.

=code [=
Public Sub FloatToStrSe(data_in as Float, byref data_str as string(18), digits as byte)
// Convert Float value to string, lenght of string is variable from 1 to 17.
// Ex. FloatToStrSE(-116.12345, data1, 0) , data1 will be "-116"
// Ex. FloatToStrSE(-116.12345, data1, 3) , data1 will be "-116.123"
// Ex. FloatToStrSE(-3.1, data1, 5) , data1 will be "-3.10000"
=]

!!!Module Code
=code [=
{
*****************************************************************************
*  Name    : Float_conv.BAS                                                *
*  Author  : Florin Andrei Medrea                                          *
*  Notice  : Copyright (c) 2007 - YO2LIO -                                  *
*          : All Rights Reserved                                            *
*  Date    : 10/20/2007                                                    *
*  Version : 1.0                                                            *
*  Notes  :                                                                *
*          :                                                                *
*****************************************************************************
}
Module Float_conv

Function StrLen(ByRef data_str As String) As Word
// returns the length, in bytes, of the string data_str
  FSR2 = @data_str
  ASM
    movf POSTINC2,w
    bnz $-1
    movf POSTDEC2,w
  End ASM
  result = FSR2 - @data_str
End Function

Sub StrCutChr(ByRef data_str As String, chr_ As Char)
// remove all specified chr from front of the string data_str
  FSR1 = @data_str
  While POSTINC1 = Byte(chr_) Wend
  FSR2 = @data_str
  WREG = POSTDEC1 // decrement FSR1
  WREG = POSTDEC1 // decrement FSR1
  While POSTINC1 <> 0 POSTINC2 = INDF1 Wend
End Sub

Sub StrCat(ByRef data_str1, data_str2 As String)
// appends data_str2 to data_str1.
  FSR1 = @data_str1
  While POSTINC1 <> 0 Wend
  FSR2 = @data_str2
  WREG = POSTDEC1 // decrement FSR1
  WREG = POSTDEC2 // decrement FSR2
  While PREINC2 <> 0 POSTINC1 = INDF2 Wend
  INDF1 = 0
End Sub

Sub adjust_1()
  ASM
    movf INDF2,w
    andlw 0x0F
    addlw 0x30
    movwf POSTDEC1
    swapf POSTINC2,w
    andlw 0x0F
    addlw 0x30
    movwf POSTDEC1
  End ASM
End Sub

Sub LongWord2StrWithZeros(data_in As LongWord, ByRef data_str As String) // Microchip AN526
Dim loop_cnt, j, buf(9) As Byte
  buf(0) = data_in.Byte0
  buf(1) = data_in.Byte1
  buf(2) = data_in.Byte2
  buf(3) = data_in.Byte3
  buf(4) = 0
  buf(5) = 0
  buf(6) = 0
  buf(7) = 0
  buf(8) = 0
  loop_cnt = 0
  While loop_cnt < 32
    ASM
      rlcf buf+0,f
      rlcf buf+1,f
      rlcf buf+2,f
      rlcf buf+3,f
      rlcf buf+4,f
      rlcf buf+5,f
      rlcf buf+6,f
      rlcf buf+7,f
      rlcf buf+8,f
    End ASM
    If loop_cnt = 31 Then
      FSR1 = AddressOf(data_str) + 10
      FSR2 = AddressOf(buf) + 4
      ASM
        clrf POSTDEC1
      End ASM     
      adjust_1
      adjust_1
      adjust_1
      adjust_1
      adjust_1
      Exit
    End If
    FSR2 = AddressOf(buf) + 3
    j = 0
    While j < 5
      ASM
        movf POSTINC2,w
        movlw 0x03
        addwf INDF2,w
        btfsc WREG,3
        movwf INDF2
        movlw 0x30
        addwf INDF2,w
        btfsc WREG,7
        movwf INDF2
      End ASM
      Inc(j)
    Wend
    Inc(loop_cnt)
  Wend
End Sub

Sub Long2Str(data_in1 As LongInt, ByRef data_str3 As String)
Dim data_str6 As String(11),
    buf As LongWord,
    buf1 As Byte
  data_str3(0) = 0
  data_str3(1) = 0
  buf = LongWord(data_in1)
  buf1 = data_in1.Byte3
  If buf1.7 = 1 Then
    buf = Not(buf) + 1
    data_str3(0) = "-"
  End If
  LongWord2StrWithZeros(buf, data_str6)
  StrCutChr(data_str6, "0")
  If StrLen(data_str6) = 0 Then
    data_str6(0) = "0"
    data_str6(1) = 0
  End If
  StrCat(data_str3, data_str6)
End Sub

Public Sub FloatToStrSE(data_in4 As Float, ByRef data_str4 As String, digit As Byte)
Dim buf_f As LongInt,
    txt10 As String(11),
    SS_FSR1, SS_FSR2 As Word
  SS_FSR1 = FSR1
  SS_FSR2 = FSR2
  buf_f = LongInt(data_in4)
  Long2Str(buf_f, data_str4)
  If digit = 0 Then GoTo loop_p End If
  data_in4 = data_in4 - Float(buf_f)
  If data_in4 < 0.0 Then data_in4 = data_in4 * -100000.0
  Else data_in4 = data_in4 * 100000.0 End If
  data_in4 = data_in4 + 0.999
  LongWord2StrWithZeros(LongInt(data_in4), txt10)
  FSR1 = AddressOf(data_str4)
  FSR2 = AddressOf(txt10) + 5
  While POSTINC1 <> 0 Wend
  WREG = POSTDEC1
  POSTINC1 = "."
  If digit > 5 Then digit = 5 End If
  While digit > 0
    POSTINC1 = POSTINC2
    Dec(digit)
  Wend
  INDF1 = 0
loop_p:
  FSR1 = SS_FSR1
  FSR2 = SS_FSR2
End Sub
=]