Shop OBEX P1 Docs P2 Docs Learn Events
Stamp Math - Tracy Allen help — Parallax Forums

Stamp Math - Tracy Allen help

PorterPorter Posts: 9
edited 2006-02-21 19:05 in BASIC Stamp
Tracy I pulled this off an earler post dealing with Stamp integer math.

***
The LTC1298 uses the power supplyy as its reference, so 5 volts is divided into 4096 steps of 1.2207 volts. When you see a reading of 1580 steps, that means the input voltage is,
V = 1580 * 1.2207 = 1.929

On the Stamp, you do that with the */ operator

milllivolts = AD */ 3125
DEBUG DEC millivolts/10, ".", DEC1 millivolts
***

I'm testing a battery output with·a variable voltage·between 16.5 down to 10.5 volts through a voltge divider. (3.9k + 1.0K both 1%).· This give me an output that is feed to a·ltc1298 (like the above post)·only I'm referencing a 4.1 persision voltage regulator so I should get about 1mv per count but it's not quite that good.

Long story short help me understand how you got the number 3125 in the above set of equation and how the */ operator works.

Thanks in advance····

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-02-20 20:26
    */ multiplies by units of 1/256. To use it simply multiply your fractional value by 256 and then use this as the value for */. If you wanted to multiply by Pi, for example:

    3.14159 x 256 = 804 (remember the Stamp only uses integers)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-02-21 17:16
    As Jon explained, its fractional units of 1/256. I like that way of thinking about it better than the explanation you sometimes see using HEX integer/fraction notation. Here is why.

    Here is the fractional approximation as you might write it in 6th grade math class: 3125/256 = 12207/1000 = 1.2207 * 10.

    Actually, 3125/256 is 12.20703125, but that's close enough, don't you think? The Stamp works this internally by multiplying by 3125 and then shifting the result right by 8 bits (which is the same as dividing by 256). Computers are very fast at shifting bits, whereas long division in general is quite time consuming.

    If you feed a known voltage into your circuit, you will get a value from the LTC1298. Say you feed in known 10.00 volts, and the ADC count output read by the Stamp happens to be 2041 counts (out of 4096).

    You want to display the number 10.00. Here is the math: 1000 = multiplier/256 * 2041. Solve for multiplier. multiplier = 1000 * 256 / 2041 = 125.43, which you round off to 125, or to 1254 or 12543.

    Then from the ADC, 2041 */ 125 = 996 close, but not too great, or 2041 */ 1254 = 9997. Or 2041 */ 12543 = 10001.

    You see how the precision improves as you use more digits? The last one gives you plenty to work with to round off very accurately.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-02-21 19:05
    Oh, sorry, I mis-spoke on the highest accuracy way of doing it. 2041 */ 12543 = 100001, not 10001. Oops, that larger number won't fit in 16 bits, and the Stamp ends up dropping the most significant bit when it crams the number into 16 bits. So instead of 100001, you end up with 34465 (which is 100001 // 65536) One thing you have to watch out for in using */ is that the product of the two numbers can never exceed 2^24 power, which leads to the above problem.

    Maybe you don't need the precision. 9997 rounds off to 10.0 volts:
    result = result */ 1254    ' =9997 out for 2041 in
    result = result + 5 / 10   '  rounding off, 1000 out for 9997 in
    DEBUG DEC result/100, ".", DEC2 result  ' display 10.00
    




    There _are_ a couple of ways to get a little more precision, if you need it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 2/21/2006 7:35:20 PM GMT
Sign In or Register to comment.