Shop OBEX P1 Docs P2 Docs Learn Events
using variables with floatmath ? — Parallax Forums

using variables with floatmath ?

lfreezelfreeze Posts: 174
edited 2010-04-16 07:43 in Propeller 1
·
Please help, I think the problem described below has exceeded my small skills with the propeller.
·
I am trying to scale the output of an ADC0831 to display 0.0· to 5.0 volts depending on its Output of· from 0 to 255. I am dividing 5 by 255 and using the result of 0.0195 as
A multiplier to determine the voltage
·
Example:··· ADC puts out a value of 127, to convert this to a voltage, I multiply
················ ,0195 by 127 which equals a voltage of· 2.47
·
After spending a lot of time and trying most of the math objects ·to program this, I discovered that you cannot use interger variables in the float math formula. You must use· values that contain a decimal Point and one integer.
·
Example:· you cannot use 127, you must use 127.0 ·
·
Is there an object or formula to convert integer variables to variables containing
A decimal point and the option to add a 0 after the decimal?· Or am I not seeing the forest
for the trees and there is a simple solution I have overlooked?
·
Example:
·
Convert·· the variable 127 to 127.0··
·
The program below is what I have been working on. If you run it, it will not display the
Correct result. If you change the firstnumber variable to 127.0 it will work correctly
·
Many thanks for any solutions, or suggestions..
·
CON
· _clkmode = xtal1 + pll16x
· _xinfreq = 5_000_000
· adcmultiplier =0.0196
···
OBJ
· ser···· : "fullduplexserial"
· f······ : "FloatMath"
· fp····· : "FloatString"
···
VAR
· long firstnumber, secondnumber
·
PUB start
· ser.start(31, 30, 0, 9600)····
·
· firstnumber :=127· 'value from ADC0831 (0 to 255)
·
·· repeat
·························
····· secondnumber:=f.fmul(firstnumber, adcmultiplier)· '· multiplies two decimal numbers···
····· secondnumber:=(fp.FloatToString(secondnumber)) 'converts float number to string·····
····· ser.str(string("· secondnumber·· float to string result = "))·
····· ser.str(secondnumber)··· 'display converted float to string value
·
····· ser.tx(13)'carriage return
····· waitcnt (40_000_000 + cnt)

Comments

  • James NewmanJames Newman Posts: 133
    edited 2010-04-15 17:15
    When using the and of the FloatMath type objects, you must always use floats on the math functions. To get a float from an integer, use the 'FFloat' function provided. To convert from a float back to an integer, use FRound or FTrunc depending on which is applicable. Checkout the sources for FloatMath, these are the first 3 functions.

    so just wrap your integer in fp.FFloat()
  • lfreezelfreeze Posts: 174
    edited 2010-04-15 21:00
    Many THANKS James, I thought I had tried that solution many times, obviously I did not. By simply adding
    firstnumber :=(f.ffloat(firstnumber)) the program works properly.


    CON
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000
    adcmultiplier =0.0196
    OBJ
    ser : "fullduplexserial"
    f : "FloatMath"
    fp : "FloatString"
    VAR
    long firstnumber, secondnumber
    PUB start
    ser.start(31, 30, 0, 9600)
    firstnumber :=115 'value from ADC0831 (0 to 255)
    firstnumber :=(f.ffloat(firstnumber))
    repeat

    secondnumber:=f.fmul(firstnumber, adcmultiplier) ' multiplies two decimal numbers
    secondnumber:=(fp.FloatToString(secondnumber)) 'converts float number to string
    ser.str(string(" secondnumber float to string result = "))
    ser.str(secondnumber) 'display converted float to string value
    ser.tx(13)'carriage return
    waitcnt (40_000_000 + cnt). O
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-04-16 07:43
    Why do you use float? Ok ... good exercise ... but the ADC returns a 8 bit value. Using 32 bit and integer math should give you a result which is adequate enough. Advantage is that integer math will be much faster than float math.
Sign In or Register to comment.