interger only math
Archiver
Posts: 46,084
Hi Hudson,
The method you are using will work, and it is always good to think
hard about things!.
The Stamp does have a couple of built in operators "*/" and "**" that
are most helpful in dealing with fractions.
If the ADC puts out counts of 0 to 255 in response to an input of 0
to 5 volts, or in response to an input of 4.96 volts, you are looking
at the conversion:
mymvolts = mycounts * 5000/256
or
mymvolts = mycounts * 4960/256
That is the way you would do it on a calculator, not on the Stamp,
but notice that it starts with millivolts already so we are starting
with larger integers. And notice that the denominator is 256, not
255, because there are 256 possible values, when you count zero as a
value. Small difference, but it matters.
The way to do it in Stampese PBASIC is:
mymvolts = mycounts */ 5000
or
mymvolts = mycounts */ 4960
Notice that the "256" has disappeared from the formula. So, you see,
the answer to the question in the Basic analog and digital is really
quite simple.
Really, if you want to go on with this, you should understand how it
works for the Stamp and many other microcontrollers.
Division by 256 is implied in the */ operator. When the Stamp PBASIC
interpreter sees the "*/", it multiplies mycounts*5000 or
mycounts*4960 internally and then divides the result by 256. (shift
right 8 bits).
Suppose you tried to make the Stamp do that using the obvious brute
force equation,
mymvolts = mycounts * 5000/256
And suppose mycounts=250. Then the Stamp would attempt to do
250*5000 = 1250000, but since the Stamp can only count up to 65535
(16 bits) it will gives back a bogus answer of 4816 (which is the
remainder of 1250000//65536).
The */ operator does not have that limitation, because internally for
the first step the Stamp uses a 24 bit calculation that can go up to
15932415, and still give an answer that can go up to 65535.
I hope that helps. You might find the math section on my web site
useful, with more explanation, maybe more confusion!...
http://www.emesystems.com/BS2index.htm
By the way, your email messages are coming through with a June date
that puts them in the "future" in my Eudora mailbox. I wonder if the
date settings on your computer are correct?
-- regards,
Tracy Allen
electronically monitored ecosystems
mailto:tracy@e...
http://www.emesystems.com
>Ok well a while ago I posted something about the analog to digital
>experiments from parallax... I'm not the best at math, guess what? I'm
>still stuck! But I have thought about this very hard. I think I will have
>to do a different bit of code if I'm working with a whole number or a
>fraction... For example if its 4 volts I will have to do something
>different then if its 4.5 volts. Here is the code it does to calculate
>the voltage:
>
>CALC_VOLTS: 'subroutine named CALC_VOLTS
>v = 5*adcbits/255
>R = 5*adcbits//255
>v2 = 100*R/255
>v3 = 100*R//255
>v3 = 10*v3/255
>if v3 < 5 then skip_out 'if v3 is less then 5 go to the skip_out label
>v = v + 1
>v2 = 0
>skip_out: 'label named skip_out
>return
>
>Alright now I figured out this:
>
>5 * b / 255 = a
>
>and
>
>a * 255 / 5 = b
>
>I did this so I could figure out the binary value for whatever fraction
>or non fraction I'm working with. I did the second formula so I can find
>what the value for 5-1 is here is what I found:
>
>255 = 5
>204 = 4
>153 = 3
>102 = 2
>51 = 1
>and of course 0 = 0
>
>Now what I was thinking of doing is this suppose whats before the decimal
>point is A and whats after it is B (sense you can't store a fraction in
>one variable) so we have a.b right? I thought maybe thing would adjust
>the calculation for the whole numbers:
>
>A = A - 1
>B = 96
>
>I thought that this would adjust it because I have to adjust it so that
>the total voltage is 4.96 and not exactly 5. I found that it works with 5
>only the others are slightly off. OK so I guess this isn't how I would do
>it. I'm not understanding why it only works for 5... Does anyone have any
>ideas? I'm gonna start reading about integer only math now... I'm so
>stuck its not even funny.
The method you are using will work, and it is always good to think
hard about things!.
The Stamp does have a couple of built in operators "*/" and "**" that
are most helpful in dealing with fractions.
If the ADC puts out counts of 0 to 255 in response to an input of 0
to 5 volts, or in response to an input of 4.96 volts, you are looking
at the conversion:
mymvolts = mycounts * 5000/256
or
mymvolts = mycounts * 4960/256
That is the way you would do it on a calculator, not on the Stamp,
but notice that it starts with millivolts already so we are starting
with larger integers. And notice that the denominator is 256, not
255, because there are 256 possible values, when you count zero as a
value. Small difference, but it matters.
The way to do it in Stampese PBASIC is:
mymvolts = mycounts */ 5000
or
mymvolts = mycounts */ 4960
Notice that the "256" has disappeared from the formula. So, you see,
the answer to the question in the Basic analog and digital is really
quite simple.
Really, if you want to go on with this, you should understand how it
works for the Stamp and many other microcontrollers.
Division by 256 is implied in the */ operator. When the Stamp PBASIC
interpreter sees the "*/", it multiplies mycounts*5000 or
mycounts*4960 internally and then divides the result by 256. (shift
right 8 bits).
Suppose you tried to make the Stamp do that using the obvious brute
force equation,
mymvolts = mycounts * 5000/256
And suppose mycounts=250. Then the Stamp would attempt to do
250*5000 = 1250000, but since the Stamp can only count up to 65535
(16 bits) it will gives back a bogus answer of 4816 (which is the
remainder of 1250000//65536).
The */ operator does not have that limitation, because internally for
the first step the Stamp uses a 24 bit calculation that can go up to
15932415, and still give an answer that can go up to 65535.
I hope that helps. You might find the math section on my web site
useful, with more explanation, maybe more confusion!...
http://www.emesystems.com/BS2index.htm
By the way, your email messages are coming through with a June date
that puts them in the "future" in my Eudora mailbox. I wonder if the
date settings on your computer are correct?
-- regards,
Tracy Allen
electronically monitored ecosystems
mailto:tracy@e...
http://www.emesystems.com
>Ok well a while ago I posted something about the analog to digital
>experiments from parallax... I'm not the best at math, guess what? I'm
>still stuck! But I have thought about this very hard. I think I will have
>to do a different bit of code if I'm working with a whole number or a
>fraction... For example if its 4 volts I will have to do something
>different then if its 4.5 volts. Here is the code it does to calculate
>the voltage:
>
>CALC_VOLTS: 'subroutine named CALC_VOLTS
>v = 5*adcbits/255
>R = 5*adcbits//255
>v2 = 100*R/255
>v3 = 100*R//255
>v3 = 10*v3/255
>if v3 < 5 then skip_out 'if v3 is less then 5 go to the skip_out label
>v = v + 1
>v2 = 0
>skip_out: 'label named skip_out
>return
>
>Alright now I figured out this:
>
>5 * b / 255 = a
>
>and
>
>a * 255 / 5 = b
>
>I did this so I could figure out the binary value for whatever fraction
>or non fraction I'm working with. I did the second formula so I can find
>what the value for 5-1 is here is what I found:
>
>255 = 5
>204 = 4
>153 = 3
>102 = 2
>51 = 1
>and of course 0 = 0
>
>Now what I was thinking of doing is this suppose whats before the decimal
>point is A and whats after it is B (sense you can't store a fraction in
>one variable) so we have a.b right? I thought maybe thing would adjust
>the calculation for the whole numbers:
>
>A = A - 1
>B = 96
>
>I thought that this would adjust it because I have to adjust it so that
>the total voltage is 4.96 and not exactly 5. I found that it works with 5
>only the others are slightly off. OK so I guess this isn't how I would do
>it. I'm not understanding why it only works for 5... Does anyone have any
>ideas? I'm gonna start reading about integer only math now... I'm so
>stuck its not even funny.
Comments
experiments from parallax... I'm not the best at math, guess what? I'm
still stuck! But I have thought about this very hard. I think I will have
to do a different bit of code if I'm working with a whole number or a
fraction... For example if its 4 volts I will have to do something
different then if its 4.5 volts. Here is the code it does to calculate
the voltage:
CALC_VOLTS: 'subroutine named CALC_VOLTS
v = 5*adcbits/255
R = 5*adcbits//255
v2 = 100*R/255
v3 = 100*R//255
v3 = 10*v3/255
if v3 < 5 then skip_out 'if v3 is less then 5 go to the skip_out label
v = v + 1
v2 = 0
skip_out: 'label named skip_out
return
Alright now I figured out this:
5 * b / 255 = a
and
a * 255 / 5 = b
I did this so I could figure out the binary value for whatever fraction
or non fraction I'm working with. I did the second formula so I can find
what the value for 5-1 is here is what I found:
255 = 5
204 = 4
153 = 3
102 = 2
51 = 1
and of course 0 = 0
Now what I was thinking of doing is this suppose whats before the decimal
point is A and whats after it is B (sense you can't store a fraction in
one variable) so we have a.b right? I thought maybe thing would adjust
the calculation for the whole numbers:
A = A - 1
B = 96
I thought that this would adjust it because I have to adjust it so that
the total voltage is 4.96 and not exactly 5. I found that it works with 5
only the others are slightly off. OK so I guess this isn't how I would do
it. I'm not understanding why it only works for 5... Does anyone have any
ideas? I'm gonna start reading about integer only math now... I'm so
stuck its not even funny.
[noparse][[/noparse]Non-text portions of this message have been removed]
my girl friend who went to UC DAVIS has like some math skills...
she was helping me figure out how to deal with the fraction ones (not
whole numbers) and she basically figured out what your site has for doing
this. Like for one thing I dropped out of high school... so I'm not the
best in math... I started going to college when I was 17 for electronics
(I'm an A student). Anyway I'm really starting to understand its all
about this "scaling" business really... we figured out (I even helped!)
how to do it for fractions to. I have a TI-36x calculator I want to buy a
nice graphing one like she has. What kind of calculator do you use? What
would you recommend for electronics?
Your site did help because I was really seing the pattern... its all
about scaling, anyway thanks a lot good job for helping out the community
and putting that wonderful documentation on the internet! I was thinking
how my applied mathimatics class in electronics (I got an A!) should have
a small section about this you know? I don't know why they didn't at
least go over it but then again my other class mates are NULL and took
many weeks to understand vector math.