• PicŪ Basic


  • Proton24 Float and Double conversion procedures

    Presented here is an include file that has procedures for conversion of Double or Float to an ASCII string, and an ASCII string to Float or Double.

    The procedures are written in Proton24 BASIC so that they can be changed to suit a particular programmer's requirement.

    The procedures are:

    DoubleToASCII
    Code:
    '
    ' Demonstrate the 64-bit floating point to ASCII string procedure
    ' For use with the Proton24 compiler version 1.0.1.2 onwards
    '
        Device = 24HJ128GP502
        Declare Xtal = 79.23
        
        Declare Hserial_Baud = 9600                 ' UART1 baud rate
        Declare Hrsout1_Pin = PORTB.3               ' Select which pin is to be used for TX with USART1
    
        Include "Convert24.inc"                     ' Load the conversion procedures into the program
        
        Dim Doublein As Double
    
    '------------------------------------------------------------------------
    Main:        
        PLL_Setup(43, 2, 2, $0300)                              ' Configure the Oscillator to operate the device at 79.23MHz     
        PPS_Output(cOut_Pin_RP3, cOut_Fn_U1TX)                  ' Make Pin RB3 U1TX
    
        HRSOut "Double to ASCII Conversion\r"
        For Doublein = 1000 To 1001 Step 0.0001
            HRSOut DoubleToASCII(Doublein, 4), ", "
        Next
        HRSOut 13
    
    '----------------------------------------------------------------------------------------------------------------------------------------
    ' Configure for internal 7.37MHz oscillator with PLL
    ' OSC pins are general purpose I/O
    '
        Config FBS = BWRP_WRPROTECT_OFF, BSS_NO_FLASH, BSS_NO_BOOT_CODE
        Config FSS = SWRP_WRPROTECT_OFF, SSS_NO_FLASH, RSS_NO_SEC_RAM
        Config FGS = GWRP_OFF, GCP_OFF
        Config FOSCSEL = FNOSC_FRCPLL, IESO_OFF
        Config FOSC = POSCMD_NONE, OSCIOFNC_ON, IOL1WAY_OFF, FCKSM_CSDCMD
        Config FWDT = WDTPOST_PS256, WINDIS_OFF, FWDTEN_OFF
        Config FPOR = FPWRT_PWR128, ALTI2C_OFF
        Config FICD = ICS_PGD1, JTAGEN_OFF
    FloatToASCII
    Code:
    '
    ' Demonstrate the 32-bit floating point to ASCII string procedure
    ' For use with the Proton24 compiler version 1.0.1.2 onwards
    '
        Device = 24HJ128GP502
        Declare Xtal = 79.23
        
        Declare Hserial_Baud = 9600                 ' UART1 baud rate
        Declare Hrsout1_Pin = PORTB.3               ' Select which pin is to be used for TX with USART1
    
        Include "Convert24.inc"                     ' Load the conversion procedures into the program
        
        Dim Floatin As Float
    
    '------------------------------------------------------------------------
    Main:        
        PLL_Setup(43, 2, 2, $0300)                              ' Configure the Oscillator to operate the device at 79.23MHz     
        PPS_Output(cOut_Pin_RP3, cOut_Fn_U1TX)                  ' Make Pin RB3 U1TX
        
        HRSOut "Float to ASCII Conversion\r"
        For Floatin = 20 To 20.5 Step 0.01
            HRSOut FloatToASCII(Floatin, 2), ", " 
        Next
        HRSOut 13
        
    '----------------------------------------------------------------------------------------------------------------------------------------
    ' Configure for internal 7.37MHz oscillator with PLL
    ' OSC pins are general purpose I/O
    '
        Config FBS = BWRP_WRPROTECT_OFF, BSS_NO_FLASH, BSS_NO_BOOT_CODE
        Config FSS = SWRP_WRPROTECT_OFF, SSS_NO_FLASH, RSS_NO_SEC_RAM
        Config FGS = GWRP_OFF, GCP_OFF
        Config FOSCSEL = FNOSC_FRCPLL, IESO_OFF
        Config FOSC = POSCMD_NONE, OSCIOFNC_ON, IOL1WAY_OFF, FCKSM_CSDCMD
        Config FWDT = WDTPOST_PS256, WINDIS_OFF, FWDTEN_OFF
        Config FPOR = FPWRT_PWR128, ALTI2C_OFF
        Config FICD = ICS_PGD1, JTAGEN_OFF
    ASCIIToDouble
    Code:
    '
    ' Demonstrate the ASCII string to 64-bit floating point conversion procedure
    ' For use with the Proton24 compiler version 1.0.1.2 onwards
    '
        Device = 24HJ128GP502
        Declare Xtal = 79.23
        
        Declare Hserial_Baud = 9600                 ' UART1 baud rate
        Declare Hrsout1_Pin = PORTB.3               ' Select which pin is to be used for TX with USART1
    
        Include "Convert24.inc"                     ' Load the conversion procedures into the program
        
        Dim Doublein As Double
        Dim MyString As String * 32
    
    '------------------------------------------------------------------------
    Main:        
        PLL_Setup(43, 2, 2, $0300)                              ' Configure the Oscillator to operate the device at 79.23MHz     
        PPS_Output(cOut_Pin_RP3, cOut_Fn_U1TX)                  ' Make Pin RB3 U1TX 
        
        HRSOut "ASCII To Double Conversion\r"
        MyString = "3000.14567"
        HRSOut DoubleToASCII(ASCIIToDouble(MyString), 3), 13
    
    '----------------------------------------------------------------------------------------------------------------------------------------
    ' Configure for internal 7.37MHz oscillator with PLL
    ' OSC pins are general purpose I/O
    '
        Config FBS = BWRP_WRPROTECT_OFF, BSS_NO_FLASH, BSS_NO_BOOT_CODE
        Config FSS = SWRP_WRPROTECT_OFF, SSS_NO_FLASH, RSS_NO_SEC_RAM
        Config FGS = GWRP_OFF, GCP_OFF
        Config FOSCSEL = FNOSC_FRCPLL, IESO_OFF
        Config FOSC = POSCMD_NONE, OSCIOFNC_ON, IOL1WAY_OFF, FCKSM_CSDCMD
        Config FWDT = WDTPOST_PS256, WINDIS_OFF, FWDTEN_OFF
        Config FPOR = FPWRT_PWR128, ALTI2C_OFF
        Config FICD = ICS_PGD1, JTAGEN_OFF
    ASCIIToFloat
    Code:
    '
    ' Demonstrate the ASCII string to 32-bit floating point conversion procedure
    ' For use with the Proton24 compiler version 1.0.1.2 onwards
    '
        Device = 24HJ128GP502
        Declare Xtal = 79.23
        
        Declare Hserial_Baud = 9600                 ' UART1 baud rate
        Declare Hrsout1_Pin = PORTB.3               ' Select which pin is to be used for TX with USART1
    
        Include "Convert24.inc"                     ' Load the conversion procedures into the program
        
        Dim Floatin As Float
        Dim MyString As String * 32
    
    '------------------------------------------------------------------------
    Main:        
        PLL_Setup(43, 2, 2, $0300)                              ' Configure the Oscillator to operate the device at 79.23MHz     
        PPS_Output(cOut_Pin_RP3, cOut_Fn_U1TX)                  ' Make Pin RB3 U1TX
    
        HRSOut "ASCII To Float Conversion\r"
        MyString = "300.145"
        HRSOut FloatToASCII(ASCIIToFloat(MyString), 3), 13 
    
    '----------------------------------------------------------------------------------------------------------------------------------------
    ' Configure for internal 7.37MHz oscillator with PLL
    ' OSC pins are general purpose I/O
    '
        Config FBS = BWRP_WRPROTECT_OFF, BSS_NO_FLASH, BSS_NO_BOOT_CODE
        Config FSS = SWRP_WRPROTECT_OFF, SSS_NO_FLASH, RSS_NO_SEC_RAM
        Config FGS = GWRP_OFF, GCP_OFF
        Config FOSCSEL = FNOSC_FRCPLL, IESO_OFF
        Config FOSC = POSCMD_NONE, OSCIOFNC_ON, IOL1WAY_OFF, FCKSM_CSDCMD
        Config FWDT = WDTPOST_PS256, WINDIS_OFF, FWDTEN_OFF
        Config FPOR = FPWRT_PWR128, ALTI2C_OFF
        Config FICD = ICS_PGD1, JTAGEN_OFF
    The Convert24.inc file and a demo program can be found here:

    Double and Float conversion procedures

    Important.
    In order for all programs to use the conversion procedures, copy the Convert24.inc file to the user's Includes directory.