Shop OBEX P1 Docs P2 Docs Learn Events
Scaling? — Parallax Forums

Scaling?

Mike15Mike15 Posts: 109
edited 2006-04-11 12:57 in Learn with BlocklyProp
·I am using an ADC to read an accelerometer and I'm trying to scale the input.

P75······· CON $00C0 ' 0.75
P50······· CON $0080 ' 0.50
P25······· CON $0040 ' 0.25
SCLK······ CON 6
CS········ CON 7
SerData_in CON 8
adc······· VAR Word
sValue···· VAR Word
main:
GOSUB convert
GOSUB print
GOTO main

print:
DEBUG HOME
DEBUG "Raw value... ", DEC adc, " ", CR
DEBUG "Filtered.... ", DEC sValue, " "
sValue = adc
RETURN

convert:
LOW CS
SHIFTIN Serdata_in,SCLK,MSBPOST,[noparse][[/noparse]adc\12]
HIGH CS
sValue = (adc */ P25) + (sValue */ P75)
adc = adc */ (?)
sValue = sValue */ (?)
RETURN

I know this my ADC has 4096 counts each count = 1mV and my accelerometer out puts 27 mV per G and has an at rest reading of 2.5V or 2500 counts.

My question is how can I scale this so I get an at rest reading and +/- G's?

·

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-04-10 22:01
    [noparse][[/noparse]edit] PJ has a better solution -- here's my version that gets around the PBASIC limitation of no division of negative values:

    · IF counts.BIT15 THEN
    ··· gForce = 0 - ((ABS(counts) - 2500) / 27)
    · ELSE
    ··· gForce = (counts - 2500) / 27
    · ENDIF

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax

    Post Edited (Jon Williams (Parallax)) : 4/10/2006 11:22:01 PM GMT
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-04-10 22:45
    0G = 2500 counts

    +G [noparse][[/noparse]if counts > 2500] =·counts - 2500 / 27

    -G [noparse][[/noparse]if counts < 2500] = 2500 - counts / -27

    ???



    Post Edit --
    Jon Williams said...
    ...no division of negative values
    D'Oh!

    Post Edited (PJ Allen) : 4/11/2006 1:07:50 AM GMT
  • Mike15Mike15 Posts: 109
    edited 2006-04-11 05:45
    instead of using 2500 would it be better to take a few at rest samples and average together?
    gForce = 0 - ((ABS(counts) - atrest) / 27)

    Is it possable to average numbers?
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-04-11 12:57
    Mike15 said...
    Is it possible to average numbers?
    Yes.· You could, say,·stash 3 readings into 3 variables, add those up, and divide by 3.
    But, remember, with acceleration, time is a big deal.· So, mind what you are averaging.

    Post Edited (PJ Allen) : 4/11/2006 1:48:08 PM GMT
Sign In or Register to comment.