Shop OBEX P1 Docs P2 Docs Learn Events
Need help in finding average ,voltage reading using ADC 831 with basic stamp code — Parallax Forums

Need help in finding average ,voltage reading using ADC 831 with basic stamp code

mogu93mogu93 Posts: 22
edited 2013-10-20 07:25 in BASIC Stamp
This is my code ,where I take 4 reading ,and average reading .I need helping in reading average reading of the voltages using a adc also shift in the reading and looping it . I don't know if my code is correct . THANKS! :DD ' {$STAMP BS2}
' {$PBASIC 2.5}
'
[ Program Description ]
'
' This program demonstrates reading a variable voltage with an ADC0831
' analog-to-digital converter chip. This program uses a Vref input of
' 5.000 volts (Vdd) for a bit resolution of 19.6 millivolts.
'
[ I/O Definitions ]
CS PIN 0 ' chip select (ADC0831.1)
Clock PIN 1 ' clock (ADC0831.7)
DataIn PIN 2 ' data (ADC0831.6)
'
[ Constants ]
Cnts2Mv CON $139C ' x 19.6 (to millivolts)
'
[ Variables ]
result VAR Byte ' result of conversion
mVolts VAR Word ' millivolts
mVolts1 VAR Word ' millivolts
mVolts2 VAR Word ' millivolts
mVolts3 VAR Word ' millivolts
mVolts4 VAR Word
mVolts5 VAR Word
mVolts9 VAR Word




'
[ Initialization ]
Reset:
DEBUG CLS, ' create report screen
"ADC.... ", CR,
"volts... "
'
[ Program Code ]
Main:

DO

GOSUB Read_0831 ' read the ADC
mVolts1 = result */ Cnts2Mv ' convert to millivolts
DEBUG HOME, ' report
CRSRXY, 9, 0, DEC result, CLREOL,
CRSRXY, 9, 1, DEC mVolts1 DIG 3,
".", DEC3 mVolts1
PAUSE 1000

GOSUB Read_0831 ' read the ADC
mVolts2 = result */ Cnts2Mv ' convert to millivolts
DEBUG HOME, ' report
CRSRXY, 9, 0, DEC result, CLREOL,
CRSRXY, 9, 1, DEC mVolts2 DIG 3,
".", DEC3 mVolts2
PAUSE 1000

GOSUB Read_0831 ' read the ADC
mVolts3 = result */ Cnts2Mv ' convert to millivolts
DEBUG HOME, ' report
CRSRXY, 9, 0, DEC result, CLREOL,
CRSRXY, 9, 1, DEC mVolts3 DIG 3,
".", DEC3 mVolts3
PAUSE 1000

GOSUB Read_0831 ' read the ADC
mVolts4 = result */ Cnts2Mv ' convert to millivolts
DEBUG HOME, ' report
CRSRXY, 9, 0, DEC result, CLREOL,
CRSRXY, 9, 1, DEC mVolts4 DIG 3,
".", DEC3 mVolts4
PAUSE 1000






GOSUB Read_0831 ' read the ADC
mVolts = (mVolts1+mVolts2+mVolts3+mVolts4)/4' convert to millivolts
DEBUG HOME, ' report
CRSRXY, 9, 0, DEC result, CLREOL,
CRSRXY, 9, 1, DEC mVolts DIG 3,
".", DEC3 mVolts
PAUSE 1000


GOSUB Read_0831 ' read the ADC
mVolts5 = result */ Cnts2Mv ' convert to millivolts




GOSUB Read_0831 ' read the ADC
mVolts1=mVolts2
mVolts2=mVolts3
mVolts3=mVolts4
mVolts9 = (mVolts5+mVolts1+mVolts2+mVolts3)/4' convert to millivolts
DEBUG HOME, ' report
CRSRXY, 9, 0, DEC result, CLREOL,
CRSRXY, 9, 1, DEC mVolts9 DIG 3,
".", DEC3 mVolts9
PAUSE 3000


LOOP



'
[ Subroutines ]
Read_0831:
LOW CS ' enable ADC
SHIFTIN DataIn, Clock, MSBPOST, [result\9] ' read ADC
HIGH CS ' disable ADC
RETURN

Comments

  • Francis160791Francis160791 Posts: 28
    edited 2013-10-18 02:03
    this seem so hard......
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2013-10-18 07:02
    Francis160791,

    Your code looks good, the only thing I really did was to change how you dealt with consecutive ADC reads towards the bottom ....
    "Bump the oldest ADC value out in preparation of inserting the new value"


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' -----[ Program Description ]---------------------------------------------
    '
    ' This program demonstrates reading a variable voltage with an ADC0831
    ' analog-to-digital converter chip. This program uses a Vref input of
    ' 5.000 volts (Vdd) for a bit resolution of 19.6 millivolts.
    ' -----[ I/O Definitions ]-------------------------------------------------
    CS PIN 0 ' chip select (ADC0831.1)
    Clock PIN 1 ' clock (ADC0831.7)
    DataIn PIN 2 ' data (ADC0831.6)
    ' -----[ Constants ]-------------------------------------------------------
    Cnts2Mv CON $139C ' x 19.6 (to millivolts)
    ' -----[ Variables ]-------------------------------------------------------
    result VAR Byte ' result of conversion
    mVolts VAR Word ' millivolts
    mVolts1 VAR Word ' millivolts
    mVolts2 VAR Word ' millivolts
    mVolts3 VAR Word ' millivolts
    mVolts4 VAR Word
    
    
    
    ' -----[ Initialization ]--------------------------------------------------
    Reset:
    DEBUG CLS, ' create report screen
    "ADC.... ", CR,
    "volts... "
    ' -----[ Program Code ]----------------------------------------------------
    Main:
    '-------------------------------------------------------------------------
    '--------------------------------+------------+---------------------------
    '--------------------------------¦ Initialize ¦---------------------------
    '--------------------------------+------------+---------------------------
    '-------------------------------------------------------------------------
    ' Read the first 3 consecutive ADC values here
    
    GOSUB Read_0831 ' read the ADC
    mVolts1 = result */ Cnts2Mv ' convert to millivolts
    DEBUG HOME, ' report
    CRSRXY, 9, 0, DEC result, CLREOL,
    CRSRXY, 9, 1, DEC mVolts1 DIG 3,
    ".", DEC3 mVolts1
    PAUSE 1000
    
    GOSUB Read_0831 ' read the ADC
    mVolts2 = result */ Cnts2Mv ' convert to millivolts
    DEBUG HOME, ' report
    CRSRXY, 9, 0, DEC result, CLREOL,
    CRSRXY, 9, 1, DEC mVolts2 DIG 3,
    ".", DEC3 mVolts2
    PAUSE 1000
    
    GOSUB Read_0831 ' read the ADC
    mVolts3 = result */ Cnts2Mv ' convert to millivolts
    DEBUG HOME, ' report
    CRSRXY, 9, 0, DEC result, CLREOL,
    CRSRXY, 9, 1, DEC mVolts3 DIG 3,
    ".", DEC3 mVolts3
    PAUSE 1000
    
    '-------------------------------------------------------------------------
    '--------------------------------+-----------+----------------------------
    '--------------------------------¦ Main Loop ¦----------------------------
    '--------------------------------+-----------+----------------------------
    '-------------------------------------------------------------------------
    'Perpetually read the 4th ADC value here in this loop
    
    DO
    
       GOSUB Read_0831 ' read the ADC
       mVolts4 = result */ Cnts2Mv ' convert to millivolts
    
    '-------------------------------------------------------------------------
    '--------------------------------+---------+------------------------------
    '--------------------------------¦ Average ¦------------------------------
    '--------------------------------+---------+------------------------------
    '-------------------------------------------------------------------------
    'Average the 4 ADC results together and display
    
       mVolts = (mVolts1+mVolts2+mVolts3+mVolts4)/4' convert to millivolts
       DEBUG HOME, ' report
       CRSRXY, 9, 0, DEC result, CLREOL,
       CRSRXY, 9, 1, DEC mVolts DIG 3,
       ".", DEC3 mVolts
       PAUSE 3000
    
    '-------------------------------------------------------------------------
    '------------------------+-------------------------------+----------------
    '------------------------¦ Bump the oldest ADC value out ¦----------------
    '------------------------+-------------------------------+----------------
    '-------------------------------------------------------------------------
       mVolts1=mVolts2
       mVolts2=mVolts3
       mVolts3=mVolts4
    
    LOOP
    
    
    Read_0831:
    '-------------------------------------------------------------------------
    '--------------------------------+-----------+----------------------------
    '--------------------------------¦ Read_0831 ¦----------------------------
    '--------------------------------+-----------+----------------------------
    '-------------------------------------------------------------------------
    LOW CS ' enable ADC
    SHIFTIN DataIn, Clock, MSBPOST, [result\9] ' read ADC
    HIGH CS ' disable ADC
    RETURN
    
  • mogu93mogu93 Posts: 22
    edited 2013-10-18 20:10
    thanks for your help ! :smile::tongue::lol:
  • Francis160791Francis160791 Posts: 28
    edited 2013-10-20 07:25
    but how is the code work man ?
Sign In or Register to comment.