News:

Let's find out together what makes a PIC Tick!

Main Menu

FloatToString

Started by JackB, Today at 04:50 PM

Previous topic - Next topic

JackB

Here's the code I used:
I was able to work with the StringToFloat(pValAddr), but with FloatToString(pValue, pDigits) I've got
an error (Insufficient Amount of Parameters) even using '0' or '5' for pDigits

I assumed the pValue is the Float Value (TotalVDC = 2.348) or which parameter is missing

if anyone can help it would be much appreciated


Device = 18F25K22
Declare Xtal = 64

' Floating to String
Include "Strings.inc"

;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Config_Start
  FOSC = INTIO7    ;Internal oscillator block, CLKOUT function on OSC2
  PLLCFG = On    ;Oscillator multiplied by 4
  PRICLKEN = On    ;Primary clock enabled
  FCMEN = OFF    ;Fail-Safe Clock Monitor disabled
  IESO = OFF    ;Oscillator Switchover mode disabled
  PWRTEN = OFF    ;Power up timer disabled
  BOREN = SBORDIS  ;Brown-out Reset enabled in hardware only (SBOREN is disabled)
  BORV = 190    ;VBOR set to 1.90 V nominal
  WDTEN = OFF    ;Watch dog timer is always disabled. SWDTEN has no effect.
  WDTPS = 32768    ;1:32768
  CCP2MX = PORTC1  ;CCP2 input/output is multiplexed with RC1
  PBADEN = On    ;PORTB<5:0> pins are configured as analog input channels on Reset
  CCP3MX = PORTB5  ;P3A/CCP3 input/output is multiplexed with RB5
  HFOFST = On    ;HFINTOSC output and ready status are not delayed by the oscillator stable status
  T3CMX = PORTC0   ;T3CKI is on RC0
  P2BMX = PORTB5   ;P2B is on RB5
  MCLRE = INTMCLR  ;RE3 input pin enabled; MCLR disabled
  STVREN = On    ;Stack full/underflow will cause Reset
  LVP = OFF        ;Single-Supply ICSP enabled if MCLRE is also 1
  XINST = OFF    ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
  Debug = OFF    ;Disabled
  Cp0 = OFF        ;Block 0 (000800-001FFFh) not code-protected
  CP1 = OFF        ;Block 1 (002000-003FFFh) not code-protected
  CP2 = OFF        ;Block 2 (004000-005FFFh) not code-protected
  CP3 = OFF        ;Block 3 (006000-007FFFh) not code-protected
  CPB = OFF        ;Boot block (000000-0007FFh) not code-protected
  CPD = OFF        ;Data EEPROM not code-protected
  WRT0 = OFF    ;Block 0 (000800-001FFFh) not write-protected
  WRT1 = OFF    ;Block 1 (002000-003FFFh) not write-protected
  WRT2 = OFF    ;Block 2 (004000-005FFFh) not write-protected
  WRT3 = OFF    ;Block 3 (006000-007FFFh) not write-protected
  WRTC = OFF    ;Configuration registers (300000-3000FFh) not write-protected
  WRTB = OFF    ;Boot Block (000000-0007FFh) not write-protected
  WRTD = OFF    ;Data EEPROM not write-protected
  EBTR0 = OFF    ;Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
  EBTR1 = OFF    ;Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
  EBTR2 = OFF    ;Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
  EBTR3 = OFF    ;Block 3 (006000-007FFFh) not protected from table reads executed in other blocks
  EBTRB = OFF    ;Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
Config_End

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------

TRISA = %00000000               
TRISB = %11111111               
TRISC = %00000000               
TRISE = %00000000
ANSELA = 0                     
ANSELB = 0                     
ANSELC = 0                     
OSCCON  = %01110000  '$70       
OSCTUNE = %01000000  '$40       

' var setup
Dim TotalVDC As Float           
Dim MyString As String * 20         
Dim Length As Byte

PORTA.0 = 1
DelayMS 10                   

TotalVDC = 2.348
'mystring = "2.348"

' StringToFloat(pValAddr)
' FloatToString(pValue, pDigits)

'totalvdc = StringToFloat(MyString)
MyString = FloatToString(TotalVDC,5)


'MyString = Left$(MyString,5)
'Length = Len (MyString)
 
'SerOut PORTA.0,84,[" TotalVDC*  : ",Str MyString," Len : ",Dec Length,13,10]

SerOut PORTA.0,84,[" TotalVDC*  : ",Str MyString,13,10]
'SerOut PORTA.0,84,[" TotalVDC*  : ",dec totalvdc,13,10]

End



Pepe

write

MyString = FloatToStr(TotalVDC,5,0)

Henk57

I did also a bit with String Thing,
I got mostly an error, except in this form.


Str SRC_Array = "Default    "
Str LC_Array = Str SRC_Array

top204

The Strings library include file can be found in the 'Includes' directory. Here... "C:\Users\User Name\PDS\Includes\"

Each of the procedures have header texts that tell the user what each parameter is for, and what is returned (if anything).

The FloatToStr procedure has a third parameter that will enable/disable rounding of the floating point value.

For example, the code listing below shows the StrToFloat and FloatToStr procedures working:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Code template for a PIC18F25K22 device, operating at 64MHz using its internal oscillator.
'
' Written by Les Johnson for the Positron8 BASIC compiler.
' https://sites.google.com/view/rosetta-tech/positron-compilers-experimenters-notebook.
'
    Device = 18F26K22                                                   ' Tell the compiler what device to compile for
    Declare Xtal = 64                                                   ' Tell the compiler what frequency the device is operating at (in MHz)
    Declare Float_Display_Type = Fast                                   ' Use the compiler's faster, and more accurate, floating point display routine
    Declare Auto_Heap_Arrays = On                                       ' Make all arrays "Heap" types, so they always get placed after standard variables
    Declare Auto_Heap_Strings = On                                      ' Make all Strings "Heap" types, so they always get placed after standard variables
    Declare Auto_Variable_Bank_Cross = On                               ' Make sure all multi-byte variables remain within a single RAM bank
'
' Set the USART1 pins for an Amicus8 board
'
    Declare Hserial1_Baud = 9600                                        ' Set USART1 Baud rate to 9600
    Declare HSerout1_Pin  = PORTC.6                                     ' Set the TX pin for USART1
    Declare HSerin1_Pin   = PORTC.7                                     ' Set the RX pin for USART1

    Include "Strings.inc"                                               ' Load the Strings library from the "Includes" folder
'
' Create any global variables, aliases, or constants here
'
    Dim MyString As String * 32
    Dim MyFloat As Float
    Dim MyDword As SDword

'--------------------------------------------------------------------------
' The main program starts here
' Use the String to Float, and vice-versa procedures from the "Strings.inc" library file
'
Main:
    Setup()                                                             ' Setup the program and any peripherals

    MyString = "3.1416"                                                 ' Load the String with the value of Pi
    MyFloat = StrToFloat(MyString)                                      ' Convert the String value into a floating point variable
    HRsout1Ln "MyFloat = ", Dec4 MyFloat                                ' Transmit the value held in MyFloat to a serial terminal

    MyFloat = 3.1416                                                    ' Load the Float variable with the value of Pi
    MyString = FloatToStr(MyFloat, 4, 0)                                ' Convert the value in MyFloat to a String, without any rounding
    HRsout1Ln "MyString = ", MyString                                   ' Transmit the contents of MyString to a serial terminal

    MyString = FloatToStr(MyFloat, 4, 1)                                ' Convert the value in MyFloat to a String, with rounding
    HRsout1Ln "Rounded MyString = ", MyString                           ' Transmit the contents of MyString to a serial terminal


'----------------------------------------------------------------------------
' Setup the program and any peripherals
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    Osc_64MHz()                                                         ' Set the device to 64MHz operation using its internal oscillator
'
' More setup code here
'
EndProc

'---------------------------------------------------------------------------------------------
' Set the PIC18F25K22 device to 64MHz operation using its internal oscillator
' Input     : None
' Output    : None
' Notes     : None
'
Proc Osc_64MHz()
    OSCCON  = $70
    OSCCON2 = $04
    OSCTUNE = $40
    DelayMS 10                                                          ' A small delay to allow the oscillator to stabilise
EndProc

'---------------------------------------------------------------------------------------------
' Setup the fuses to use the internal oscillator on a PIC18F25K22 device.
' OSC pins RA6 and RA7 are general purpose I/O.
'
Config_Start
    FOSC     = INTIO67                                                  ' Internal oscillator
    PRICLKEN = Off                                                      ' Primary clock disabled
    MCLRE    = EXTMCLR                                                  ' MCLR pin enabled
    WDTEN    = Off                                                      ' Watchdog Timer disabled
    PLLCFG   = Off                                                      ' Oscillator used directly
    FCMEN    = Off                                                      ' Fail-Safe Clock Monitor disabled
    IESO     = Off                                                      ' Oscillator Switchover mode disabled
    PWRTEN   = On                                                       ' Power up timer enabled
    BOREN    = Off                                                      ' Brown-Out Reset disabled
    BORV     = 190                                                      ' VBOR set to 1.9 V nominal
    WDTPS    = 128                                                      ' Watchdog Timer Postscale is 1:128
    HFOFST   = Off                                                      ' The Access clock is not held Off until the HF-INTOSC is stable
    PBADEN   = Off                                                      ' PORTB<4:0> pins are configured as digital on reset
    CCP2MX   = PORTC1                                                   ' CCP2 input/output is multiplexed with RC1
    CCP3MX   = PORTB5                                                   ' P3A/CCP3 input/output is multiplexed with RB5
    T3CMX    = PORTC0                                                   ' T3CKI is on RC0
    P2BMX    = PORTB5                                                   ' P2B is on RB5
    STVREN   = On                                                       ' Stack full/underflow will cause a reset
    LVP      = Off                                                      ' Single-Supply ICSP disabled
    Debug    = Off                                                      ' Background debugger disabled. RB6 and RB7 configured as general purpose I/O pins
    XINST    = Off                                                      ' Extra mnemonics disabled
    Cp0      = Off                                                      ' Block 0 (000800-001FFF) not code protected
    CP1      = Off                                                      ' Block 1 (002000-003FFF) not code protected
    CPB      = Off                                                      ' Boot block (000000-0007FF) not code protected
    CPD      = Off                                                      ' Data EEPROM not code protected
    WRT0     = Off                                                      ' Block 0 (000800-001FFF) not write protected
    WRT1     = Off                                                      ' Block 1 (002000-003FFF) not write protected
    WRTB     = Off                                                      ' Boot block (000000-0007FF) not write protected
    WRTC     = On                                                       ' Configuration registers (300000-3000FF) write protected
    WRTD     = Off                                                      ' Data EEPROM not write protected
    EBTR0    = Off                                                      ' Block 0 (000800-001FFF) not protected from table reads executed in other blocks
    EBTR1    = Off                                                      ' Block 1 (002000-003FFF) not protected from table reads executed in other blocks
    EBTRB    = Off                                                      ' Boot block (000000-0007FF) not protected from table reads executed in other blocks
Config_End

On the serial terminal it will show:

MyFloat = 3.1416
MyString = 3.1415
Rounded MyString = 3.1416


Regards
Les


JackB

Many thanks to Pepe, Henk57 and Les

Let me give you an update so far,

I did use Pepe code with a result of 2056 bytes program, and 123 variables bytes used

I edited the Strings.inc files to replace:

original line :

$define FloatToString(pValue, pDigits) FloatToStr(pValue, pDigits)

to

$define FloatToString(pValue) Str$(Dec pValue)

with a result of 764 bytes program, and 71 variables bytes used


Seem both method work fine with a value TotalVDC = 12.348.

any comments is welcome.