numbers more than 65535
Archiver
Posts: 46,084
You can take advantage of this fact:
x0=x
x=x+1
if x>x0 then no_over
y=y+1
no_over:
The trick is that $FFFE+1 = $FFFF and $FFFF>$FFFE. However, $FFFF+1 = 0 and
0 is not > $FFFF. So therefore there was an overflow.
So now y:x is your total 32-bit count (assuming x and y and x0 are 16-bits).
You can find more details about this and other tricks in my book
Microcontroller Projects with Basic Stamps
(www.al-williams.com/awce/sbook.htm). Also Tracy Allen's site has lots of
math tricks (www.emesystems.com I think). I'd be remiss if I didn't point
out that the PAK-I and PAK-II let you do full floating point math easily,
too.
Regards,
Al Williams
AWC
* PAK Sale -- almost over! http://www.al-williams.com/awce
>
Original Message
> From: Mohamed REFKY [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=iftH9fYAukx08pBivX9MSpa8kqyPNOt7o3s-BpTFgnKNhrLcVOGC68_CO_4mm52NmCdMIoxbhYwQ]refky@h...[/url
> Sent: Thursday, July 27, 2000 1:54 PM
> To: basicstamps@egroups.com
> Subject: [noparse][[/noparse]basicstamps] numbers more than 65535
>
>
> Hell,
> How can I count up to a number that is more than 65535.I may need
> to count
> up to 99999 or more.
>
> Thank You
> Mohamed Refky
>
> ________________________________________________________________________
> Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
>
>
>
x0=x
x=x+1
if x>x0 then no_over
y=y+1
no_over:
The trick is that $FFFE+1 = $FFFF and $FFFF>$FFFE. However, $FFFF+1 = 0 and
0 is not > $FFFF. So therefore there was an overflow.
So now y:x is your total 32-bit count (assuming x and y and x0 are 16-bits).
You can find more details about this and other tricks in my book
Microcontroller Projects with Basic Stamps
(www.al-williams.com/awce/sbook.htm). Also Tracy Allen's site has lots of
math tricks (www.emesystems.com I think). I'd be remiss if I didn't point
out that the PAK-I and PAK-II let you do full floating point math easily,
too.
Regards,
Al Williams
AWC
* PAK Sale -- almost over! http://www.al-williams.com/awce
>
Original Message
> From: Mohamed REFKY [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=iftH9fYAukx08pBivX9MSpa8kqyPNOt7o3s-BpTFgnKNhrLcVOGC68_CO_4mm52NmCdMIoxbhYwQ]refky@h...[/url
> Sent: Thursday, July 27, 2000 1:54 PM
> To: basicstamps@egroups.com
> Subject: [noparse][[/noparse]basicstamps] numbers more than 65535
>
>
> Hell,
> How can I count up to a number that is more than 65535.I may need
> to count
> up to 99999 or more.
>
> Thank You
> Mohamed Refky
>
> ________________________________________________________________________
> Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
>
>
>
Comments
' make a low word and a high word
' and add one to the high word every time
' this can count up to 2^32
xlow var word
xhigh var word
xlow=65525 ' demo starting value
incrementcount:
xlow=xlow+1
xhigh= xhigh + (xlow max 1) -1
debug hex4 xhigh,hex4 xlow,cr
goto incrementcount
'it might be more convenient to roll over at 9999
'if you need an easy decimal result
' count up to 99999999
xlow var word
xhigh var word
xlow=9990 ' demo starting value
incrementcount:
xlow=xlow+1 //10000 ' roll over at 9999
xhigh= xhigh + (xlow max 1) -1
debug dec4 xhigh,dec4 xlow,cr
goto incrementcount
regards,
Tracy Allen
Electronically Monitored Ecosystems
http://www.emesystems.com/BS2math2.htm#Method 2 <-- how to convert
double prec binary to decimal
how can it then be divided by a value that will NEVER be more than 1 word
in size? I realize that floating point routines or the PAK-1/2 will do
the job but just wondered if someone has worked out a method that gives
results in integer arithmetic - whole number and remainder. This would be
useful in devices that collect data and compute averages.
Larry Nielsen
Tracy Allen wrote:
> Here are a couple of methods:
>
> ' make a low word and a high word
> ' and add one to the high word every time
> ' this can count up to 2^32
> xlow var word
> xhigh var word
> xlow=65525 ' demo starting value
> incrementcount:
> xlow=xlow+1
> xhigh= xhigh + (xlow max 1) -1
> debug hex4 xhigh,hex4 xlow,cr
> goto incrementcount
>
> 'it might be more convenient to roll over at 9999
> 'if you need an easy decimal result
> ' count up to 99999999
> xlow var word
> xhigh var word
> xlow=9990 ' demo starting value
> incrementcount:
> xlow=xlow+1 //10000 ' roll over at 9999
> xhigh= xhigh + (xlow max 1) -1
> debug dec4 xhigh,dec4 xlow,cr
> goto incrementcount
>
> regards,
> Tracy Allen
> Electronically Monitored Ecosystems
> http://www.emesystems.com/BS2math2.htm#Method 2 <-- how to convert
> double prec binary to decimal
>
>
What is 'file//10000' ?
TIA,
Ray McArthur
> incrementcount:
> xlow=xlow+1 [url=][/url] ' roll over at 9999
> Once a number exists in 2 words - either $FFFFFFFF
> or decimal 99999999,how can it then be divided
> by a value that will NEVER be more than 1 word
> in size? I realize that floating point routines
> or the PAK-1/2 will do the job but just wondered
> if someone has worked out a method that gives
> results in integer arithmetic - whole number and
> remainder. This would be useful in devices that
> collect data and compute averages.
http://www.emesystems.com/BS2math2.htm
regards,
Tracy Allen
Electronically Monitored Ecosystems
>TIA,
>Ray McArthur
>> incrementcount:
>> xlow=xlow+1 [url=][/url] ' roll over at 9999
The word "file:" should not be in there. It wasn't in the original I sent
or in the echo I got back from egroups. My guess is that your email or
html interpreter stuck it in there for you when it saw the "//". The //
operator returns the remainder of (xlow+1) after division by 10000. So it
counts up to 9999 and then rolls over to zero.
I see I got a formula for the high word backwards in my previous post: it
should read
xhigh= xhigh + 1 - (xlow max 1) ' correct, increment
'NOT xhigh= xhigh + (xlow max 1) -1 <--- this is decrement
-- Tracy Allen
Electronically Monitored Ecosystems
http://www.emesystems.com
How can I count up to a number that is more than 65535.I may need to count
up to 99999 or more.
Thank You
Mohamed Refky
________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
Mohamed REFKY wrote:
>
> How to display that decimal number exists in 2 words using MAX7219,since it
> may needs faster subroutine than that of the AppKit? .Also how to do
> computations with such a number or I have to use PAk1 ?
>
> >'it might be more convenient to roll over at 9999
> >'if you need an easy decimal result
> >' count up to 99999999
> >xlow var word
> >xhigh var word
> >xlow=9990 ' demo starting value
> >incrementcount:
> > xlow=xlow+1 //10000 ' roll over at 9999
> > xhigh= xhigh + 1-(xlow max 1) debug dec4 xhigh,dec4 xlow,cr
> >goto incrementcount
> >
> > regards,
> > Tracy Allen
> > Electronically Monitored Ecosystems
> > http://www.emesystems.com/BS2math2.htm#Method 2 <-- how to convert
> >double prec binary to decimal
> >
> >
> >
> >
> >
> >
>
> ________________________________________________________________________
> Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
may needs faster subroutine than that of the AppKit? .Also how to do
computations with such a number or I have to use PAk1 ?
>'it might be more convenient to roll over at 9999
>'if you need an easy decimal result
>' count up to 99999999
>xlow var word
>xhigh var word
>xlow=9990 ' demo starting value
>incrementcount:
> xlow=xlow+1 //10000 ' roll over at 9999
> xhigh= xhigh + 1-(xlow max 1) debug dec4 xhigh,dec4 xlow,cr
>goto incrementcount
>
> regards,
> Tracy Allen
> Electronically Monitored Ecosystems
> http://www.emesystems.com/BS2math2.htm#Method 2 <-- how to convert
>double prec binary to decimal
>
>
>
>
>
>
________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
> How to display that decimal number exists in 2 words using MAX7219,
> since it may needs faster subroutine than that of the AppKit? .Also
> how to do computations with such a number or I have to use PAk1?
Leaving aside the issue of computations for the moment...
and counting lots and lots of artichokes:
' BS2
' count up to 99999999
' roll over from low word to high word at 9999
xlow var word
xhigh var word
xlow=9990 ' demo starting value
incrementcount:
xlow=xlow+1 //10000 ' roll over at 9999
xhigh= xhigh +1-(xlow max 1)
' debug dec4 xhigh,dec4 xlow,cr
offset=4 ' to display higer 4 digits
dispVal = xhigh '<--send to the MAX7219
gosub MaxDisplay4
offset=0 ' to display lower 4 digits
dispVal = xlow
gosub MaxDisplay4
goto incrementcount
You could potentially fill up the 8 digits of the MAX7219, count up to
99999999 artichokes--wow! The routine above now calls a Maxdisplay4
routine to put the data on the MAX7219, instead of on the debug screen.
Below is a modified version of the Maxdisplay routine that appears in the
Parallax app note. You said you need it to run faster. I honed is so it
only uses SHIFTOUT once to send both the register address and the data as
one 16 bit quantity, instead of separate SHIFTOUTs. I also took out the
the leading zero blanking, which takes up a lot of time in that routine.
But it is still going to be "slow" to display 8 digits. It could be
tightened up a lot, so that it only updates digits when they change.
' all other variables & initialization
' as in Parallax app note for MAX7219
maxCode var word ' define a new word
regAdr var maxCode.byte1 ' alias for register address
Bcode var maxCode.byte0 ' alias for B-code
offset var nib ' offset on display
MaxDisplay4: ' to display 4 digits on Max7219
for index = 4 to 1 ' Work from digit 8 down to digit 1.
regAdr = index+offset
Bcode = dispVal dig (index-1) ' Get decimal digit (0-4) of dispVal.
shiftout DATA_n,CLK,msbfirst,[noparse][[/noparse]maxCode\16] ' Send address & digit.
pulsout Load,5 ' And load the display.
next ' Repeat for all 4 digits.
return ' Done? Return.
Returning to the issue of "calulations" you brought up. What sort of
calculations do you mean? Most calculations are easier if you keep the
numbers in binary format. I do have a routine on my web page that kicks
out decimal digits of a double precision (32 bit binary) number. I don't
know how it would compare in speed to a PAK-I.
regards,
Tracy Allen
Electronically Monitored Ecosystems
http://www.emesystems.com/BS2math2.htm#Method 2 <-- how to convert