Shop OBEX P1 Docs P2 Docs Learn Events
Help with phsa — Parallax Forums

Help with phsa

Andrew CAndrew C Posts: 14
edited 2009-12-16 02:17 in Propeller 1
Hi,
When I put this code into my program:

time := (phsa - 624) #> 0···············
a := time

the time variable will·display a number like 773 in hyper terminal.

However, when I display a, it will show a number such at 1e-42.

Can someone please explain to me why a does not read the same as time?

Comments

  • RaymanRayman Posts: 14,876
    edited 2009-12-15 22:38
    I think you need to say exactly how you are outputting those vars to hyperterminal...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    My Prop Info&Apps: ·http://www.rayslogic.com/propeller/propeller.htm
  • Andrew CAndrew C Posts: 14
    edited 2009-12-15 23:28
    Gah,
    I need to put a filter on my thinking. Yep, I converted to string!
  • RaymanRayman Posts: 14,876
    edited 2009-12-15 23:33
    Hmm... Are a and time both longs? I would think either a and time are different types or something is horribly wrong!
    What are you using to convert to string?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    My Prop Info&Apps: ·http://www.rayslogic.com/propeller/propeller.htm
  • Andrew CAndrew C Posts: 14
    edited 2009-12-16 00:06
    The b variable reads 1108384835 and never changes, although I have it set up to a variable resistor on pin 17
    a is multiplying ok, but it appears to be the "b" that is not adding correctly.

    Strange, here is the code:


    long a
    long b
    long c
    long time


    dira[noparse][[/noparse]17] := outa[noparse][[/noparse]17] := 1
    waitcnt(clkfreq/10_000 + cnt)
    phsa~
    dira[noparse][[/noparse]17]~
    waitcnt(clkfreq + cnt)
    time := (phsa - 624) #> 0
    Debug.Str(String(13, "time = "))
    debug.dec(time)
    Debug.Str(String(" which correlates to "))
    a := fMath.FMul(time, 1.5279)
    b := fMath.FAdd(a, 36.1526)
    debug.dec(b)
    Debug.Str(String(" ohms"))
    Debug.Str(String(13, "a = "))
    Debug.dec(a)
    Debug.Str(String(13, "b = "))
    Debug.dec(b)
    Debug.Str(String(13, "c = "))
    Debug.dec(c)
    Debug.Str(String(13, "Dec version of time = "))
    Debug.dec(time)
    Debug.Str(String(13, "Dec version of a = "))
    Debug.dec(a)
    waitcnt(clkfreq + cnt)

    Post Edited (Andrew C) : 12/16/2009 12:11:08 AM GMT
  • RaymanRayman Posts: 14,876
    edited 2009-12-16 00:31
    Ok, you need someone who has used floating point with the Prop before (not me)!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    My Prop Info&Apps: ·http://www.rayslogic.com/propeller/propeller.htm
  • Andrew CAndrew C Posts: 14
    edited 2009-12-16 00:53
    @ Rayman, Thanks for trying!
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2009-12-16 01:12
    Within the floatmath object there is a function to convert integers to floats, this will be needed to convert phsa so it makes sense within floating point mathematics I think.

    In the floatstring object there is a function to convert a float to a string which would be useful when outputting to the terminal.

    There is a float demo showing the use of floatstring that comes with the prop tool.

    Cheers,

    Graham

    p.s. to make your code more readable surround it with code tags [noparse][[/noparse]code ] and [noparse][[/noparse]/code ]

    (but remove the spaces between the square brackets)
  • Andrew CAndrew C Posts: 14
    edited 2009-12-16 02:17
    @Graham,

    Thank you! I'm learning more again. Here is the code I used to help understand:
    '' File: 1Test.spin
    
    OBJ
    
       fM: "Float32"             ' Use floating math routines
       p: "FullDuplexSerialPlus" ' Use with Parallax Serial Terminal
       fS: "FloatString"         ' Use floating math to String routines
    
    CON
       _clkmode = xtal1 + pll16x ' System clock → 80 MHz
       _xinfreq = 5_000_000
    
    VAR
       long num1
       long num2
       long x
       long y
    
    
    Pub Init
    
       p.Start(31, 30, 0, 9600)       ' Initialize the serial terminal
       fM.start                       ' Initialize the floating point math in a cog
       waitcnt(clkfreq * 3 + cnt)     ' Wait 3 seconds to press "Enable" on Serial Terminal
    
       main                           ' start the main program
    
    PUB main
    
    
       p.str(string(16))              ' Clear the terminal screen 
       p.str(string("Begin output", 13))
       num1 := clkfreq
       p.dec(num1)
       p.str(string(13))              ' CR/LF
    
       'x := 1.1                      'Begin output
       'y := 2.2                      '80000000
       'num2 := x * y                 '-259040215 (because variables are fp)
       'p.dec(num2)                   'End output
    
       'x := fM.ffloat(1.1)           'Begin output
       'y := fM.ffloat(2.2)           '80000000
       'num2 := fM.FMul(x, y)         '1568564747 (because answer is in fp)
       'p.dec(num2)                   'End output
    
       'x := 1.1                      'Begin output
       'y := 2.2                      '80000000
       'num2 := fM.FMul(x, y)         '1568564747 (because answer is in fp)
       'p.dec(num2)                   'End output
    
       x := 1.1                       'Begin output
       y := 2.2                       '80000000
       num2 := fM.FMul(x, y)          '2.42 (correct)
       p.str(fS.FloatToString(num2))  'End output (needs to convert answer to fp)
    
       p.str(string(13))
    
       x := 2
       y := 3
       num2 := x * y
       p.dec(num2)                    'displays "6" correct (because answer is integer)
    
       p.str(string(13))
    
       'x := 1.1                             'Begin output
       'y := 2.2                             '80000000
       'num2 := fM.FMul(num1, 1.0)           '2.42             (correct)
       'p.str(fS.FloatToString(num2))        '6                (correct)
                                             '4.624469e-36     (because num1 is integer)
    
       x := 1.1                              'Begin output
       y := 2.2                              '80000000
       num2 := fM.FMul(fM.FFloat(num1), 2.0) '2.42            (correct)
       p.str(fS.FloatToString(num2))         '6               (correct)
                                             '160000000       (correct - converted num1 to float before multiplying)
       
       p.str(string(13,"End output"))
    



    Post Edited (Andrew C) : 12/16/2009 2:49:19 AM GMT
Sign In or Register to comment.