Shop OBEX P1 Docs P2 Docs Learn Events
Trying to read voltage with MCP3002 but all pins read source voltage — Parallax Forums

Trying to read voltage with MCP3002 but all pins read source voltage

PeteStarksPeteStarks Posts: 7
edited 2013-06-23 16:54 in BASIC Stamp
After successfully getting my LM34 working with my ADC0831 thanks to help from the forum I have moved onto the 2 channel MCP3002. I attempted to get it working with a simple proof of concept by reading voltage off the first channel. I hooked everything up as I saw it on the datasheet with a reference and source voltage of 5volts as supplied by BS2 BOE.

After first loading the code and running it, I had a debug screen which showed me 5volts. Seeing as I was measuring 5volts, I assumed it was working fine. However, I quickly found out that it read EVERY value as 5volts. After unhooking my voltage sources from the channels and getting my meter I saw both channels and the Vref pin are all putting out ~5volts when disconnected. I believe it is caused by a problem with the initialization code and testing with a second MCP3002 gets me the same readings on my meter.

I was able to find examples for the MCP3008 and the MCP3208, but only really able to find examples for hooking the MCP3002 to a Raspberry Pi which was confusing at best for me. I found one bit of code which I used for initialization process that had no real documentation and could very well be bad.

Input?




' ==============================================================================
'
'
' {$STAMP BS2}
'
'
' Notes: This code was pieced together from bits found on google and Stamp Works experiment #27 found at
' URL: http://www.parallax.com/dl/docs/books/sw/exp/sw27.pdf
' The scavenged code from google was mostly for the initialization process(Which I assume is incorrect).
' I used the Stamp Works code to read and display the result as a voltage for  a proof of concept.  I realize
' I'm pulling less data that the ADC is capable of, but I believed it simplified things for testing.
'
'
'
' ------------------------------------------------------------------------------
' I/O Definitions
' ------------------------------------------------------------------------------
  A2DdataO   CON   6  ' A/D data OUTPUT line
  A2DdataI   CON   3  ' A/D data INPUT line for selecting channels.
  A2Dclock   CON   1  ' A/D clock
  A2Dcs      CON   2  ' A/D chip select (low true)


' ------------------------------------------------------------------------------
' Variables
' ------------------------------------------------------------------------------
  result        VAR   Byte ' result of conversion
  a2dchan       VAR   Nib  ' A2D channel selector
  mVolts        VAR   Word ' convert to millivolts




' ------------------------------------------------------------------------------
' Program Code
' ------------------------------------------------------------------------------
Main:


  a2dchan = 0                              ' Sets channels to 0 ?


  GOSUB InitADC


  GOSUB ReadData


  GOSUB PrintData


  PAUSE 1024


GOTO Main






' ------------------------------------------------------------------------------
' Subroutines
' ------------------------------------------------------------------------------


' Prepare the ADC for data acquisision
'=-=-=-=-=-=-=
InitADC:
'=-=-=-=-=-=-=
 HIGH A2Dcs                                       ' Pull CS high to ensure it's reset
 PULSOUT 1,A2Dclock                               ' Use clock pulse to advance communication


 ' Start bit
 LOW A2Dcs                                        ' Pull CS (Chip Select) pin low to initiate communication
 SHIFTOUT A2DdataI, A2Dclock, 0, [1\1]            ' Send '1' to DIN and a CLK pulse for start bit


 ' SGL/DIFF bit
 ' 0: 1 chan differential mode
 ' 1: 2 chan singled ended mode
 SHIFTOUT A2DdataI, A2Dclock, 0, [1\1]


 ' ODD/SIGN bit
 ' 0: for chan 0
 ' 1: FOR chan 1
 SHIFTOUT A2DdataI, A2Dclock, 0, [a2dchan\1]


 ' MSBF bit (Most Significant Bit Flag)
 ' 0: MSB data followed by LSB data
 ' 1: just output MSB data
 ' Although testing seems to indicate these bit settings are reversed
 SHIFTOUT A2DdataI, A2Dclock, 0, [0\1]
 'LOW DIN
 'PULSOUT 1,CLK    ' Use clock pulse to advance communication


 ' Null Bit
 ' SHIFTIN DOUT, CLK, 1, [DigBit\1] ' Actually grab the null bit
 ' Send a clock pulse to activate the null bit, but don't
 ' bother grabbing it
 PULSOUT 1,A2Dclock
 'DEBUG BIN1 DigBit, "-"


RETURN










' Get 9 OF 10 data bits using SHIFTIN
'=-=-=-=-=-=-=
ReadData:
'=-=-=-=-=-=-=
 result = 0
 SHIFTIN A2DdataO, A2Dclock, 0, [result\9]
 HIGH A2Dcs                     ' Done with that reading. Pull CS pin high
RETURN


'=-=-=-=-=-=-=
 PrintData:
'=-=-=-=-=-=-=
    mVolts = result */ $139C                       ' x 19.6 (mv / unit)


    DEBUG HOME
    DEBUG "ADC..... ", DEC result, " ", CR
    DEBUG "volts... ", DEC mVolts DIG 3, ".", DEC3 mVolts
    PAUSE 100                                      ' delay between readings
  RETURN




LM37_MCP3002-FORUM.BS2

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-06-21 19:25
    I wrote some code for the MCP3202 back when we first got it. Have you tried the code found on the product page?

    http://www.parallax.com/Portals/0/Downloads/docs/prod/compshop/MCP3202Demo.zip
  • PeteStarksPeteStarks Posts: 7
    edited 2013-06-21 19:42
    Hah... and the shame sets in!

    I have been so used to digging in the deepest, darkest holes of the internet for code, I completely over looked what was right in front of me. Let me give this a shot and see what happens....

    UPDATE:
    The code ran fine and is WORLDS simpler than that beast I was messing with. However, I am still reading 5volts on both channels even when they are disconnected from a power source. Any more ideas? Or could I have managed to somehow smoke these 2 chips?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-06-22 14:26
    PeteStarks wrote: »
    I am still reading 5volts on both channels even when they are disconnected from a power source.

    This sounds like a wiring issue.

    Could you take a picture of your setup? It might help someone identify the problem.
  • PeteStarksPeteStarks Posts: 7
    edited 2013-06-23 08:33
    It looks like you were right. I took everything apart to shorten wires and make a better picture to post. After getting everything back hooked up, I tired the code again only to find it working! I assume it was a bad connection, because some holes on my breadboard seemed to be a little worn and loose from age. I am sure I also fell victim to working on something for a bunch more hours than I should have been without a break :)

    To sum everything up:
    1) Check the connections 600 or so times to be EXTRA sure!
    2) Use the code Chris wrote as a starting point (http://www.parallax.com/Portals/0/Do...CP3202Demo.zip)
  • GenetixGenetix Posts: 1,749
    edited 2013-06-23 16:54
    PeteStarks wrote: »
    It looks like you were right. I took everything apart to shorten wires and make a better picture to post. After getting everything back hooked up, I tired the code again only to find it working! I assume it was a bad connection, because some holes on my breadboard seemed to be a little worn and loose from age. I am sure I also fell victim to working on something for a bunch more hours than I should have been without a break :)

    To sum everything up:
    1) Check the connections 600 or so times to be EXTRA sure!
    2) Use the code Chris wrote as a starting point (http://www.parallax.com/Portals/0/Do...CP3202Demo.zip)

    It's called the "smoke" test. You pray it doesn't smoke when you turn on the power.
Sign In or Register to comment.