Shop OBEX P1 Docs P2 Docs Learn Events
0831 help with code — Parallax Forums

0831 help with code

HermieHermie Posts: 36
edited 2009-06-29 23:47 in BASIC Stamp
I have a bs2sx connected to an adc0831 and using a voltage divider to transform voltage from a car battery down to 0-5v. Everything works great, as my lcd correctly displays 0-5 as it should. However, now that·I have converted to voltage, I can't figure out the code to convert the 0-5 volt reading back to 0-14 or 15 so the display will show actual voltage of the battery. Here's the code:

CLR CON 1 'lcd1620
I CON 254 'lcd1620
N9600 CON $40F0 'lcd1620
'
[noparse][[/noparse] Declarations ]
adcBits VAR Byte
v VAR Byte
r VAR Byte
v2 VAR Byte
v3 VAR Byte
'
[noparse][[/noparse] Initialization ]
CS PIN 0
CLK PIN 1
DataOutput PIN 2
DEBUG CLS 'Start display.
'
[noparse][[/noparse] Main Routine ]
DO
GOSUB ADC_Data
GOSUB Calc_Volts
'GOSUB Display
GOSUB lcd
LOOP
'
[noparse][[/noparse] Subroutines ]
ADC_Data:
LOW CLK
LOW CS
PULSOUT CLK, 210
SHIFTIN DataOutput,CLK,MSBPOST,[noparse][[/noparse]adcBits\8]
HIGH CS
RETURN
Calc_Volts:
v = 5 * adcBits / 255
r = 5 * adcBits // 255
v2 = 100 * r / 255
v3 = 100 * r // 255 '· new line
v3 = 10 * v3 / 255 '· new line
IF (v3 >= 5) THEN v2 = v2 + 1 '· new line
IF (v2 >= 100) THEN '· new line
v = v + 1 '· new line
v2 = 0
ENDIF
RETURN
Display:
'DEBUG HOME
'DEBUG "8-bit binary value: ", BIN8 adcBits
'DEBUG CR, CR, "Decimal value: ", DEC3 adcBits
'DEBUG CR, CR, "DVM Reading: " '· new line
'DEBUG DEC1 v, ".", DEC2 v2, " Volts" '· new line

RETURN
lcd:
SEROUT 6,n9600,[noparse][[/noparse]I,CLR] ' Clear the LCD screen.
PAUSE 1
SEROUT 6,n9600,[noparse][[/noparse]"Battery:"] ' Print the label.
SEROUT 6,n9600,[noparse][[/noparse]DEC1 v, ".", DEC2 v2, "··· "]
RETURN


I'm sure there's an easy answer, and I swore to figure this out myself, but I'm to the point of accepting a little help.

Thanks...Ron

Comments

  • FranklinFranklin Posts: 4,747
    edited 2009-06-27 03:12
    (0 to 5) * 3 = (0 to 15)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • HermieHermie Posts: 36
    edited 2009-06-27 03:43
    Wished it was that easy, I tried that already, but the voltage was off a little. I did the math and the conversion ratio is 3.33, not an even 3, thus my problem.
  • JonathanJonathan Posts: 1,023
    edited 2009-06-27 13:37
    Multiply the original value by 10, then divide by 33.

    adcresult = (adcresult*10) / 33

    See if that works for ya.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • HermieHermie Posts: 36
    edited 2009-06-27 17:28
    Jonathan
    It appears code is easier in theory than in reality. Your math makes total sense, but I've tried it, and can't get it to work. Problem is the code breaks down the variable because of the remainder then puts it back together "v" and "v2. I've put the math to every variable possible with no luck. Gee....if only the stamp would calculate decimals!
  • HermieHermie Posts: 36
    edited 2009-06-28 18:44
    If anyone else could help with this, I'm really feeling stupid right now! [noparse]:)[/noparse]
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-06-28 19:51
    Look at chapter 3 and 4 and it explanes how to do what you want to do

    ·Go here >>>> ADC0831

    Here is what part of· what Chapter 3 says

    ·Calculate Voltage

    Now that we know the decimal equivalent of the ADC0831’s binary output, we can do a
    few calculations to get the measured voltage. To find out what voltage the decimal
    number corresponds to, we need to calculate where in the voltage range the number falls.
    Here is an effective way to think about the problem.

    ( Where they have·5 Volts you are going to ·use 15 volts for the scale·)


    • We know that the voltage is on a 0 to 5 volt scale, and we know that the
    · ADC0831’s output is on a scale from 0 to 255.

    • In other words, the measured voltage is to 5 as the A/D output is to 255.
    This translates to fractions as:


    Voltage = Decimal A/ D Output
    ____________________________

    ······ 5·············· 255

    We can re-arrange this equality to calculate the voltage:

    ····················· 5× (Decimal A/D Output)
    Voltage=···_________________________________

    ·································· 255

    So, now we know to multiply by 5 and divide by 255 for a 5 volt scale with 256 levels.
    We can calculate the voltage from Figure 3-6 where the ADC0831’s output is 10100101

    = 165. The measured voltage is:
    = 3.24 Volts rounded to two decimal places.

    ·················· 5 ×165
    ·················· _______
    ·Voltage· =·················
    ····················· 255

    To calculate and display this voltage using the BASIC Stamp, we’ll add some code to
    both the Calc_Volts and Display subroutines. First, the voltage equation needs to be
    expressed in PBASIC code. Here is an example of some code that could reasonably be
    expected to work.

    v = 5 * adcBits / 255· ( So you are going to use· v = 15 adcBits / 255 )

    This PBASIC calculation looks like it will give us the output we want, but it won’t. It’s
    instructive to try it this way and see what happens. Modify the Calc_Volts and Display
    subroutines in Program Listing 3.1 as follows:

    Calc_Volts:
    v = 5 * adcBits / 255 '· ( So you are going to use· v = 15 adcBits / 255 )


    ·
    Resolution

    The BASIC Stamp is now programmed to accurately calculate the voltage associated
    with the ADC0831's binary output, and the calculation is accurate to the hundredth’s
    decimal place. Although sources of calculation error have been eliminated, there is
    another source of error that caused by the resolution limitation of the A/D converter.
    The A/D converter chip we are using is capable of 256 binary values. This means that
    amount of the voltage range covered between each of these discrete values. Since the first
    value is zero, there are 255 voltage steps. The step size is given by:


    ·( Where they have·5 Volts you are going to ·use 15 volts for the scale·)

    ······································ 5 Volts
    ················ Step Size =· _________· = 0.0196 Volts/step 0.02 Volts/step
    ·····································
    ····································· 255 steps

    With this in mind, each time you adjust the pot, the converter comes close to
    approximating the analog value, but it's not exact because of the resolution constraints.
    So, there is still some uncertainty at the hundredth’s decimal place. In some applications,
    the uncertainty is stated along with the measurement. Assuming the ADC0831 rounds at
    the half way point, we can use this convention to read the voltage from Figure 3-9 as
    "3.24 volts plus or minus 0.01 volts."
    ··I hope this helps


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 6/28/2009 9:06:33 PM GMT
  • HermieHermie Posts: 36
    edited 2009-06-28 22:01
    Sam,

    thank you so much, it works great. I shall name my first child after you!

    You Rock!
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-06-29 23:47
    Hermine

    I one thing I would like you to do for me is when you get your code to work right is to Post it· and share it

    Thanks

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
Sign In or Register to comment.