Shop OBEX P1 Docs P2 Docs Learn Events
help with calculating numbers larger than 65536 — Parallax Forums

help with calculating numbers larger than 65536

tolarndantolarndan Posts: 1
edited 2005-03-21 18:04 in BASIC Stamp
I work in the construction industry, and I was looking to adapt and use a BS2p40 for use on a peice of machinery that we use.· I have the BS2p40 rigged up with a hall effect sensor that is reading off of a planetary gearing system that drives the machine (for distance measurement), a 25 key keypad, and an lcd.· I am using the first bank of the stamps memory for an initialization program, the second bank for a program to detect the counts coming off of the hall effect sensor and also keystrokes from the keypad, and the third bank to run a program·for the math calculations·necessary to provide the machine's operator with·the information that he needs.· I am also using the scratch pad to transfer information between programs.· I am new to the basic stamps and I am having trouble working with numbers larger than 65536.· I need the Stamp to run a routine that will calculate a yield value·using this equation·
[size=+0][/size]
[size=+0](((TONVAL - STNVAL)*2000) /(((FCounter - SSNVAL) * WIDVAL )/9)) = Yield.[/size]

TONVAL -·is a user entered value that can be any number from 1 to 32,000.
STNVAL -·is a user entered value that can be any number from 1 to 32,000.
FCounter -·is the distance measurement coming from the hall effect sensor and can be any number from 1 to 52800.
SSNVAL - is a user entered value that can be any number from 1 to 52800.
WIDVAL - is a user entered value that can be any number from 1 to 12.


I tried to use the example coding on·tracy's website (www.emesystems.com) but I just did not understand what I was doing wrong.· Here is the code that I have so far.· Please excuse how rough it is. Any help would be appreciated.

'DMIMATH.BSP

' {$STAMP BS2p}
' {$PBASIC 2.5}


SPVAL1· VAR·· Word
SPVAL2· VAR·· Word
SPVAL3· VAR·· Word
SPVAL4· VAR·· Word
n1·· VAR Word· ' most significant word of numerator
n0·· VAR Word· ' least significant word of numerator
dv·· VAR Word· ' divisor 1<=dv<=32768
x1·· VAR Word· ' most significant word of quotient
x0·· VAR Word· ' least significant word of quotient
z··· VAR Word· ' temporary variable for long division
······················ ' also remainder from division at end
ix·· VAR Nib·· ' index for long division

[size=+0]
····· A CON·· 7281·· ' A=65536/dv, integer division
····· B CON·· 7·· ' B=65536//dv, the remainder from division A

[size=+0]'(((TONVAL - STNVAL)*2000) /(((FCounter - SSNVAL) * WIDVAL )/9)) = Yield.[/size]

····· GET 2, Word SPVAL1 ' 2 is the location of the user entered TONVAL
····· GET 0, Word SPVAL2 ' 0 is the location of the user entered STNVAL

······'(TONVAL - STNVAL)
····· SPVAL3 = SPVAL1 - SPVAL2 'Current Tonnage
·····
····· PUT 8, Word SPVAL3

····· GET 6, Word SPVAL1 ' 6 is the·location for· the user entered SSNVAL
····· GET 51, Word SPVAL2 ' 51 is the location for the FCounter value

······'(FCounter - SSNVAL)
····· SPVAL3 = SPVAL2 - SPVAL1 'Current·Distance
······[size=+0]
····· PUT 12, Word SPVAL3[/size]

····· GET 12, Word SPVAL1
····· GET 4, SPVAL2 ' 4 is the location of user entered WIDVAL
·······
······'(((FCounter - SSNVAL) * WIDVAL )
····· SPVAL3 = SPVAL1 ** SPVAL2 'High Value of Square Footage

····· PUT 14, Word SPVAL3 'High Value of Square Footage
···
······'(((FCounter - SSNVAL) * WIDVAL )
····· SPVAL3 = SPVAL1 * SPVAL2 'Low Value of Square Footage

····· PUT 16, Word SPVAL3 'Low Value of Square Footage

···

[size=+0]··· 'q1=z1/dv
··· 'q0=z1//dv
··· 'rm=q0*B//dv+(z0//dv)
··· 'q0=(q0*A)+(q0*B/dv)+(z0/dv)+(rm/dv)
··· 'rm=rm//dv
[/size]


··· GET 14, Word SPVAL1 'High Value of Square Footage

···· 'q1=z1/dv
[size=+0]··· SPVAL2 = SPVAL1 / 9[/size]
[size=+0][size=+0]····'q0=z1//dv
··· SPVAL3 = SPVAL1 // 9[/size][/size]

··· PUT 17, Word SPVAL2 'High Value of Square Yards
··· PUT 19, Word SPVAL3 'Low Value of Square Yards

··· GET 16, Word SPVAL2 'Low Value of Square Footage

·····'rm=q0*B//dv+(z0//dv)
··· SPVAL1 = ((SPVAL3 * B)//9) + (SPVAL2 // 9)

··· PUT 21, Word SPVAL1

··· GET 19, Word SPVAL1· 'Low Value of Square Yards
··· GET 16, Word SPVAL2· 'Low Value of Square Footage
··· GET 21, Word SPVAL3··'Remainder

···· 'q0=(q0*A)+(q0*B/dv)+(z0/dv)+(rm/dv)
[size=+0]··· SPVAL4 = (SPVAL1 * A) + ((SPVAL1 * B)/9) + (SPVAL2 / 9) + (SPVAL3 / 9)
[/size][/size]
··· PUT 23, Word SPVAL4· 'Square Yards Value


··· GET 8, Word SPVAL1 'Current Tonnage
··· SPVAL2 = SPVAL1 ** 2000
··· SPVAL3 = SPVAL1 * 2000

··· PUT 25, Word SPVAL2 'High Value for Pounds
··· PUT 27, Word SPVAL3 'Low Value for Pounds

··· GET 25, Word n1
··· GET 27, Word n0
··· GET 23, Word dv

········· ' ---*format conversion*---
· n1=n1<<1+n0.BIT15·· ' convert numerator to 16:15 format
· n0.BIT15=0
···· ' ----*division*
· x1=n1/dv·········· ' high word
· x0=0·············· ' initialize for long division
· z=n1
· FOR ix=14 TO 0···· ' long division, 15 bits
···· z=z//dv<<1
···· x0.BIT0(ix)=z/dv
· NEXT
· x0=x0+(n0/dv)···· ' result plus contribution from low word
· ' now find the remainder
· z=(z//dv)+(n0//dv)· ' combined remainder
· x0=(z/dv)+x0······· ' may have to add one to quotient
· x1=x1+x0.BIT15········ ' add possible carry· 16:15
· x0.BIT15=0······ '
· z=z//dv········· 'final remainder
· ' ----*end of division*
···· x0.BIT15=x1.BIT0·· ' convert quotient to standard binary format
· x1=x1>>1
· ' ---*end or format re conversion*---

· PUT 29, Word x1 'My Yield Value
·
Sign In or Register to comment.