PDA

View Full Version : Help with phsa



Andrew C
12-16-2009, 06:31 AM
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?

Rayman
12-16-2009, 06:38 AM
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 C
12-16-2009, 07:28 AM
Gah,
I need to put a filter on my thinking. Yep, I converted to string!

Rayman
12-16-2009, 07:33 AM
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 C
12-16-2009, 08:06 AM
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[17] := outa[17] := 1
waitcnt(clkfreq/10_000 + cnt)
phsa~
dira[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

Rayman
12-16-2009, 08:31 AM
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 C
12-16-2009, 08:53 AM
@ Rayman, Thanks for trying!

Graham Stabler
12-16-2009, 09:12 AM
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 [code ] and [/code ]

(but remove the spaces between the square brackets)

Andrew C
12-16-2009, 10:17 AM
@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