trying display floating point components on LCD without success

Started by basiclover, Jan 16, 2022, 08:33 AM

Previous topic - Next topic

basiclover

Using the folowing statement in the manual
« Variables Used by the Floating Point Libraries.
Several 8-bit RAM registers are used by the math routines to hold the operands for and results
of floating point operations. Since there may be two operands required for a floating point operation
(such as multiplication or division), there are two sets of exponent and mantissa registers
reserved (A and B). For argument A, PBP_AARGHHH holds the exponent and
PBP_AARGHH, PBP_AARGH and PBP_AARG hold the mantissa. For argument B,
PBP_BARGHHH holds the exponent and PBP_BARGHH, PBP_BARGH and PBP_BARG hold
the mantissa. »
I tried to diplay on an LCD the exponent and highbyte mantissa of two operands 118.5 and 18.75.
I expected for 18.75 : exponent 10000011 and highbyte mantissa 00101100
I expected for 118.5 : exponent 1000101 and highbyte mantissa 11011010
But what is displayed is wrong :
First line : all zeros except the last (16th bit) is 1
Second line : all zeros
Also
1) I don't see how the floating values 18.75 and 118.5  are selected by the compiler as either argument A or argument B.
2)To access the floating point components (exponents and mantissa) is it necessary to specify an operation or can it be done with a single floating value specified
Thanks for help

-------------------------------------------------------------------------------------------------------------------------
'****************************************************************
'*  Name    : display floating point components.BAS                                      *
'*  Author  : [select VIEW...EDITOR OPTIONS]                    *
'*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 1/15/2022                                         *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 16F877A

Config FOSC_HS, WDTE_OFF, PWRTE_OFF, BOREN_ON, LVP_OFF, CPD_OFF, WRT_OFF, DEBUG_OFF, CP_OFF

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

 ' LCD defs
    Declare LCD_Type = 0       'HITACHI HD44780 controller
    Declare LCD_DTPin PORTB.4  '4 pin config, data on 4 top pins(PortB4 to B7) of port B
    Declare LCD_RSPin = PORTB.3
    Declare LCD_ENPin = PORTB.2
    Declare LCD_Lines = 2
    Declare LCD_DataUs 75     'LCD time to wait beween data sent(microsec)for some older 16x2 44780 compatible displays (default = 50)
   
    Declare Xtal = 20
   
    Dim exponent_A As Byte
    Dim mantissa_A_HighByte As Byte
    Dim exponent_B As Byte
    Dim mantissa_B_HighByte As Byte     
    Dim PBP_AARGHHH As Byte
    Dim PBP_AARGHH As Byte
    Dim PBP_BARGHHH As Byte
    Dim PBP_BARGHH As Byte
    mantissa_A_HighByte= PBP_AARGHH
    exponent_A=PBP_AARGHHH
    mantissa_B_HighByte= PBP_BARGHH
    exponent_B=PBP_BARGHHH
    Dim Flt1 As Float
    Dim flt2 As Float
    Dim MyFloat As Float
    Flt1=18.75
    flt2=118.5
    MyFloat=flt1+flt2
    ADCON1 = 6 ' Set all ports A to digital
    TRISA=%00000000 'set all portA to output
    TRISB=%00000011 'RB0 RB1=inputs, other=outputs
    TRISC=%00000100 'RC2 =input
    TRISD=%00000000 'RCD =ouputs
   
    Cls
    GoTo main
main:
    Print At 1,1,Bin8 exponent_A
    Print At 1,9,Bin8 mantissa_A_HighByte
    Print At 2,1,Bin8 exponent_B
    Print At 2,9,Bin8 mantissa_B_HighByte
    GoTo main
    End


trastikata

Too complicated for nothing, try this instead:

Print At 1,1,Bin8 Flt1.Byte3
Print At 1,9,Bin8 Flt1.Byte2
Print At 2,1,Bin8 Flt2.Byte3
Print At 2,9,Bin8 Flt2.Byte2

basiclover


basiclover


top204

This is all that is required to see the four seperate bytes of a 32-bit Floating Point variable:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Print the four separate bytes of a 32-bit floating point variable on an LCD and also transmit them to a serial terminal
'
' Written for the Positron8 compiler by Les Johnson
'
    Device = 16F877                             ' Tell the compiler what device is being compiled for
    Declare Xtal = 4                            ' Tell the compiler what speed the device will be operating at
    Declare Float_Display_Type = Fast           ' Use the faster, more accurate, but larger, floating point display library routine
'
' Setup the alphanumeric LCD
'
    Declare LCD_DTPin = PORTD.4
    Declare LCD_RSPin = PORTE.0
    Declare LCD_ENPin = PORTE.1
    Declare LCD_Interface = 4
    Declare LCD_Lines = 2
    Declare LCD_Type = Alphanumeric  
'
' Setup HRSout
'
    Declare Hserial_Baud = 9600                  ' Set the Baud rate to 9600 for HRSout
'
' Create a variable for the demo
'   
    Dim MyFloat As Float                       
   
'---------------------------------------------------
' The main program starts here
'
Main:
    MyFloat = 3.14159                           ' Load MyFloat with a value
   
    Cls                                         ' Clear the LCD
    Print At 1,1, Dec5 MyFloat, " is:"
    Print At 2,1, Dec3 MyFloat.Byte3, ":", Dec3 MyFloat.Byte2, ":", Dec3 MyFloat.Byte1, ":", Dec3 MyFloat.Byte0
   
    HRSOutLn Dec5 MyFloat, " is: ", Dec3 MyFloat.Byte3, ":", Dec3 MyFloat.Byte2, ":", Dec3 MyFloat.Byte1, ":", Dec3 MyFloat.Byte0

And below is a screenshot of the above program working within a simulator:
Screenshot.jpg

basiclover


top204

If the separate bytes of the floating point variable are required for manipulation or comparisons, they can be aliased to the original Float variable as well:

  
    Dim MyFloat As Float
    Dim Float_Byte0 As MyFloat.Byte0            ' Create an aliased Byte variable of the first byte of the 32-bit Float                      
    Dim Float_Byte1 As MyFloat.Byte1            ' Create an aliased Byte variable of the second byte of the 32-bit Float
    Dim Float_Byte2 As MyFloat.Byte2            ' Create an aliased Byte variable of the third byte of the 32-bit Float
    Dim Float_Byte3 As MyFloat.Byte3            ' Create an aliased Byte variable of the fourth byte of the 32-bit Float
   
'---------------------------------------------------
' The main program starts here
'
Main:
    MyFloat = 3.14159                           ' Load MyFloat with a value
   
    Cls                                         ' Clear the LCD
    Print At 1,1, Dec5 MyFloat, " is:"
    Print At 2,1, Dec3 Float_Byte3, ":", Dec3 Float_Byte2, ":", Dec3 Float_Byte1, ":", Dec3 Float_Byte0
   
    HRSOutLn Dec5 MyFloat, " is: ", Dec3 Float_Byte3, ":", Dec3 Float_Byte2, ":", Dec3 Float_Byte1, ":", Dec3 Float_Byte0

The AARG and BARG system variable only come into play when the Floating Point libraries are used for calculations or conversions etc, and they are used internally, not externally in the user's program itself. Unless they are understood fully and brought out to make code more streamlined and faster.