Strugling without floating point


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Round two, or three depending on who's counting

    Ok, I needed an intermission for this one.

    So we are left with the equation:
    ____________________________________
    definitions:
    result (analog result from A/D conversion)
    P (pressure in kPa)
    VS (voltage supply ie 1024)
    ____________________________________

    result = VS * (((25 * P) + 271) / 6775)

    So to start solving for P .....

    result * 6775 = VS * ((25 * P) + 271)

    (result * 6775) / VS = 25 * P + 271

    ((result * 6775) / VS) - 271 = 25 * P

    (((result * 6775) / VS) - 271) / 25 = P

    Or P = (((result * 6775) / VS) - 271) / 25

    It only hurts a little when I try to figure these things out. I suppose the more you do it, the less it hurts. Or if you are a math guru, maybe this is fun. You guys are sick!

  2. #2
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Darrel pointed out that I could further reduce the equation, and I had the following error:

    The range for A/D conversion is only from 0 to 1023, not 1024 like I had stated. Alain caught that too, but I missed that.

    So to further simplify the equation (notice how complicated it is to simplify things?)

    We start with the same thing we ended with in the last post.

    P = (((result * 6775) / VS) - 271) / 25

    Put in the known high range value for VS

    P = (((result * 6775) / 1023) - 271) / 25

    Don't be afraid of the decimals here ....

    Dividing by 25 we get:

    P = (((result * 271) / 1023) - 10.84)

    271/1023 = .2649

    P = (result * .2649) - 10.84

    We can get there by .... (Thanks again Darrel!)
    Code:
    ADCIN 0, result
    Pressure = result * 2649
    Pressure = DIV32 1000
    Pressure = Pressure - 108
    
    LCDOUT DEC Pressure/10,".",DEC Pressure//10
    The above code converts the .2649 to 2.649 by multiplying result * 2649, then dividing by 1000. It subtracts 10 x the 10.84 which averages out to 108. so the division by 10, and the remainder from the division by 10 can give accurate results.

    And we can do all this without using slow, bloated, floating point code, available here:

    <A Href="http://www.melabs.com/resources/fp.htm">Using Microchip's <b>Floating</b> Point routines with PicBasic Pro Compiler</a>

  3. #3
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Ok, that was pretty bassackwards. If anyone followed all that, I don't know whether to feel sorry for you, or commend you. Sorry to wast all that space. Wish I could edit, or even better delete...

    Here is the "simple" way.....

    Known equation from the data sheet:
    Vout = Vs* (0.00369*P + 0.04)

    using 1/271 = .00369, and dividing both sides by .00369

    Vout * 271 = VS * (P + (.04/.00369))
    or Vout * 271 = VS * (P + 10.84)

    Vout * 271 / VS = P +10.84

    (Vout * 271 / VS) - 10.84 = P

    (Vout * (271 / 1023)) - 10.84 = P

    (Vout * .2649) - 10.84 = P

    But, again, the program that Alain pointed to in one of the above posts is useful to find a fraction to represent .00369.

    Hopefully this is much cleaner to follow. It still yields the same result, just possibly less excedrin.

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Since you arrived at the same conclusion.

    Does it really matter how you got there?

    Factoring polynomials is more of an Art.
    There's almost always more than one way to get there.

    Nice work Walter!
    <br>
    DT

  5. #5
    Join Date
    Mar 2011
    Location
    Bangkok Thailand
    Posts
    16


    Did you find this post helpful? Yes | No

    Default Same problem i think. My negative Amp sensor behave nuts.

    Hello
    Thanks for all the good tips and trick here it has helped me a lot. I think this is the right tread to put this.

    I am making an analogue Amp Instrument with a Servo with an arrow that shows the +10 to -10 Amp. Its for an old style motorbike.

    I have an Allegro Current Sensor, represented by Pot ASC714. The ASC714 Zero IN current give an output of 2.5V. I want the span to be between +10 Amps to -10 Amps. Each Amp out of the sensor =66mV. The output is:
    +10A=3.16V=255
    0A=2.5V=127
    -10A=1.84V=0

    I use only 8bit A/D and a PIC16F887.
    I have set Vref+ to 3.16V and Vref- to 1.84V
    At first sight it look that its working fine. But after some jogging with the pot it sometimes count up instead of down and vice versa. I think it has something with the Div32 to do and that i overflow it but cant figure out how to correct this.

    Thankful for all solutions
    Ole Thronborg


    '************************************************* ***************
    '* Name : Amp887.BAS *
    '* Date : 17/3/2011 *
    '* Notes : Read ASC714, Out LCD +-10A and move Servo *
    '* Show value in bit and Amp on LCD display
    '* For PIC16F887 *
    '* For standard servo settings 1-2mS set LOW_servo con 100 *
    '* and HIGH_servo CON 200 *
    '************************************************* ***************
    'Define OSC and ADC
    DEFINE OSC 4 ' Set internal Oschillator to 4Mhz
    DEFINE ADC_BITS 8 ' Set number of bits in result
    DEFINE ADC_CLOCK 2 ' Set clock source (3=rc)
    DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS

    ' Define LCD pins
    Define LCD_DREG PORTD 'LCD data port
    Define LCD_DBIT 0 'LCD data starting bit 0 or 4
    Define LCD_RSREG PORTD 'LCD register select port
    Define LCD_RSBIT 4 'LCD register select bit
    Define LCD_EREG PORTD 'LCD enable port
    Define LCD_EBIT 5 'LCD enable bit

    TRISA = %00001101 ' RA0 = A/D input
    ADCON1.7 = 0 ' RA.1 = +Vref, Set PORTA analog and left justify result
    ADCON1.5=1 ' Set Vref- to pin 5
    ADCON1.4=1 ' Set Vref+ to pin 4
    PORTb.6 =0 ' Prepare RB0 for high-going pulseout

    ANSEL = %00001101 ' Set PORTA.2 analog, rest digital
    'ANSELH = %00000000

    ' Variables
    outpuls VAR byte ' Variable for the calculated puls out in mS
    POT_POS VAR BYTE ' Pot position CC=0, CCW=255
    ampdum VAR Word ' A dummy to canculate big values
    amp var word ' Calculating Ampere
    LEFT CON 90 ' Left Stop position 0.70mS
    RIGHT CON 163 ' Right Stop position 2.1mS

    'Pause 700 ' Wait for LCD to start

    MainLoop: ' The Loop start here!
    ADCIN 0,POT_POS ' Read A/D channel 0 to variable SVO_POS
    IF POT_POS>127 then ' Check if positive
    ampdum=(7874* (POT_POS-128)) ' Then calculate the dummy
    amp= div32 100 ' Let amp get the dummy value
    else
    ampdum=(7874 * (127-POT_POS)) ' Now it must be negative Amp so calculate
    amp= div32 100 ' Let amp get the dummy value
    endif ' End of test if Amp negative

    'Output to LCD
    Lcdout $fe, 1, "POT_POS= ", #POT_POS ' Display POT Valu between 0-255 on line 1
    IF POT_POS>=127 then ' Check if + Ampere Else goto - Ampere
    LCDOut $fe,$C0, "AMP= ",DEC (amp/1000),".", DEC1 amp' Display pulswith in mS on line 2
    else
    LCDOut $fe,$C0, "AMP= -",DEC (amp/1000),".", DEC1 amp' Display pulswith in mS on line 2
    endif

    'outpuls =100+(POT_POS *100/255) 'Calculate the outpuls in mS
    'IF outpuls < LEFT THEN outpuls = LEFT ' Stop if puls shorter than constant LEFT
    'IF outpuls > RIGHT THEN outpuls = RIGHT ' Stop if puls shorter than constant RIGHT
    ' Span 100 gives 100+100 = 200=2mS 150 is center
    'PULSOUT portb.6 ,outpuls ' Move servo on pin
    'PAUSE 20 ' Constant 20mS pulses(low) between outpuls
    'GOTO MainLoop ' Forever
    End


    Name:  alegro.jpg
Views: 4094
Size:  188.3 KB

Similar Threads

  1. Getting out of floating point
    By jcb344 in forum General
    Replies: 3
    Last Post: - 5th August 2008, 22:18
  2. floating point numbers
    By n qwerty in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 8th April 2008, 05:18
  3. Floating Point Display Problem (serial string out)
    By Cash Olsen in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 20th December 2007, 03:03
  4. Microchip Floating Point Routines
    By Joe Rocci in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 12th October 2006, 20:51
  5. DIV32 instead of floating point routines?
    By Tomasm in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 22nd April 2004, 08:50

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts