Shop OBEX P1 Docs P2 Docs Learn Events
Mcp3202 spi adc — Parallax Forums

Mcp3202 spi adc

VonSzarvasVonSzarvas Posts: 3,451
edited 2007-11-16 10:56 in General Discussion
Hello all,

I am experimenting with the MCP3202 2 Channel ADC, and have reached a point where I would like to ask some help.

My device is connected thus:
CS - RB.0
CLK - RB.1
DO - RB.2
DI - RB.3

For testing, I have connected CH0 and CH1 to ground or 2v (wire link selector).

Using the below code, I always get X=255, Y=255 output. Even when CH0 is grounded.

I have spent hours searching through the datasheets and limited resources I could find with Google. mad.gif Some partial ASM code (which is foreign to me) seems to exist, or code for BS/2 etc.. and mostly with other SPI devices, but not specifically this device.

I wonder if some magic "between the lines" has been neglected from the datasheets.....

Thanks for looking. I hope this can become a useful reference.
Max.



DEVICE          SX28, OSCXT2, TURBO, STACKX, OPTIONX
FREQ              4_000_000

' -------------------------------------------------------------------------
' IO Pins, variables and sub/func definitions
' -------------------------------------------------------------------------
CS              PIN RB.0 INPUT PULLUP
Clk             PIN RB.1 INPUT PULLUP
Dout           PIN RB.2 INPUT PULLUP
Din             PIN RB.3 INPUT PULLUP
Sout            PIN RC.7 OUTPUT

ADresult      VAR      Word

GET_ADC     SUB      0               ' get value from ADC

' -------------------------------------------------------------------------

Start:

    HIGH CS                 ' make CS HIGH, no ADC
    LOW Clk                    ' make clock 0-1-0


DO
        GET_ADC                 ' move ADC value to ADresult Word
      
        TX_STR "X="
        TX_DEC3 ADresult_MSB
        TX_BYTE $2c ' Comma
        TX_STR "Y="
        TX_DEC3 ADresult_LSB
        LFCR
        PAUSE 2000     ' wait 2 seconds

LOOP
END

SUB GET_ADC
  
    LOW CS            ' activate MCP3202
    
    SHIFTOUT Dout, Clk, MSBFIRST, %00000001 ' Start bit
    SHIFTOUT Dout, Clk, MSBFIRST, %10100000 ' - Channel 0 (%11100000 - Channel 1)
    
        SHIFTIN Din, Clk, MSBPOST, ADresult_MSB/4     ' shift in the data
    SHIFTIN Din, Clk, MSBPOST, ADresult_LSB
    
    HIGH CS            ' deactivate MCP3202
    

ENDSUB





Note: I have left out the "standard" Parallax/Bean/Williams subs for TX_STR etc... which are referenced by this code to display the values on a PC via serial port on RC.7

Comments

  • terrymrterrymr Posts: 19
    edited 2007-11-15 02:30
    I think the command sequence you want to be shifting out is %00001101 (including the start bit) ... leading zeros are ignored by the ADC.
  • terrymrterrymr Posts: 19
    edited 2007-11-15 02:36
    Also the first bit you receive from the device is a null bit followed by the 12 data bits.
  • terrymrterrymr Posts: 19
    edited 2007-11-15 03:01
    Can you verify you have Din and Dout connected correctly ... Output from the SX goes to Din on the ADC and vice versa - it's easy to get your directions confused.
  • VonSzarvasVonSzarvas Posts: 3,451
    edited 2007-11-15 12:03
    Thank you for the suggestions. I have updated the source file to include your points, but still I get just &FFFF back from the ADC.

    I have attached a schematic and full source code.

    Perhaps I need to look at the connections on the sx-tech next, maybe something is getting old. I already tried changing the mcp3202 chip just in-case.

    However, I do wonder if the startup command is still a problem here....

    All ideas welcome!

    Max.



    MCP3202 Datasheet (Table on p.15 shows protocol).
    www.ortodoxism.ro/datasheets2/1/03pqicp27xfk33o2psak6seyqwwy.pdf

    Post Edited (Maxwin) : 11/15/2007 12:43:52 PM GMT
    653 x 370 - 20K
  • JonnyMacJonnyMac Posts: 9,107
    edited 2007-11-15 13:57
    The MCP3202 docs look exactly like the LTC1298 docs -- give the attached program a try; it's a conversion from a working LTC1298 program.

    Post Edited (JonnyMac) : 11/15/2007 4:05:52 PM GMT
  • VonSzarvasVonSzarvas Posts: 3,451
    edited 2007-11-15 14:50
    Thank you JonnyMac.

    To let you know, your code snippet has a small error:
    FUNC RD_3202 returns the ADC value, but in the code you do not consume the returned value! Instead calling it like a SUB.

    Anyhow,
    Sadly still I get just zeros (or FF's if I pull the input pin high).

    I have disabled the TX part for now, and am debugging with the SX key to see if anything appears there.

    However, it would seem that the device (mcp3202) needs some slightly different config instruction. If anyone has a connection at Microchip... please please ask!!

    The other thought was with my sx Tech board.. how long do they last?? (I mean the breadboard). How many Tech boards would an avid parallaxer get through each year? Mind you, I have metered the pins and they seem ok... sad.gif

    I might solder up a test version if it is sure the code is correct.... Thus eliminating the breadboard....
  • VonSzarvasVonSzarvas Posts: 3,451
    edited 2007-11-15 15:10
    Hmm, in debug the returned Word value displays as 4_095 for both channels. 1 channel goes to ground, the other to approx. 2v

    Double hmm.
  • VonSzarvasVonSzarvas Posts: 3,451
    edited 2007-11-15 16:02
    I do not have an LTC1298 available, but just out of curiosity I tried the ADC0831 in the same circuit. With minor PIN changes it works fine. Shame its only 8 bit and 1 channel!

    I think this should lead me to think its a program issue rather than a hardware issue?

    Not sure.. blush.gif
  • JonnyMacJonnyMac Posts: 9,107
    edited 2007-11-15 16:25
    The problem is either your connections or you have a bad device. Based on the quality of your code, I'm going to assume it's the connections (sorry, but your program was a bit messy). I fixed the error in my function call (my original code was create before SX/B could return Words and adc was a global -- the first posting had adjusted everything but this).

    Just to prove that my code does in fact work, I hooked up an LTC1298 (Ch0 to a pot, Ch1 grounded) and the attached screen cap shows the results.

    Post Edited (JonnyMac) : 11/15/2007 4:33:52 PM GMT
    333 x 285 - 55K
  • VonSzarvasVonSzarvas Posts: 3,451
    edited 2007-11-15 17:08
    I don't quite understand how the quality of my code can relate to having bad connections over a bad device ????

    No offence, really! I appreciate all your help - just I don't quite understand the statement! I don't disagree my code can be messy though as it's a WIP. (Or actually a DIP debug-in-progress)

    Luckily I have a few more MCP3202's, so I will work through trying different ones. I do feel assured that the problem is not code now, so if that doesn't work I will build something on veroboard and order a new SX-Tech board as an early Christmas present!

    Thanks again, Max.
  • JonnyMacJonnyMac Posts: 9,107
    edited 2007-11-15 17:43
    Well, WIP code, WIP connections... often this leads to problems; I've done it myself more times than I can count. I tested the program using a PDB, so you really should be able to get it to work on your SX-Tech board. Just make sure your connections are correct and secure; sometimes the sockets used that connect to the SX can be a little loose so I put a bit of a kink into the wire before pushing it into the socket.
  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2007-11-15 22:54
    I hope you will not be insulted. Are all the pins of the MCP3202 connected properly?

    In the schematic VDD and VSS are shown unconnected. I assume this was simply done for convenience.

    - Sparks
  • VonSzarvasVonSzarvas Posts: 3,451
    edited 2007-11-15 23:33
    Jon.. I understand your logic now... he he he... Must try harder! tongue.gif

    Sparks.. yes the power pins are all connected. Thanks for checking.


    I have some news... I built a vero board SX-28 / MCP3202 using almost the same code and connections as from the SX-Tech board. The only connections difference was that I used 2 extra SX pins pulled high/low for vdd/vss supply to the 3202, which made the layout neater.

    And yes it works fine. Very good resolution and stability, especially considering the lack of filter caps and ground plane. This is what I wanted to see, and it has proven to me well worth implementing on projects requiring greater than 255 step resolution - which the SX can handle very well itself using ANALOGIN and a careful pcb layout.


    So it would seem the problem all along MAY have been the SX-Tech, or at least Microchip being fussy. As I said, I had metered out the connections and tried wiggling cables etc.. etc.. Not a peak. And I just tried the Tech again - still not happening.. Oh well, I will order a new Tech board tomorrow and see if that solves it. Curiosity must be satisfied!


    Thanks again JonnyMac. I really appreciate your time building a circuit to help me eliminate the code problem and focus attention towards hardware. Now, get back to the book!!!

    Best regards - Max.
    1280 x 960 - 75K
  • JonnyMacJonnyMac Posts: 9,107
    edited 2007-11-16 00:11
    Glad you got it working -- I will put a note in my LTC1298 code that it also works with the MCP3202.
  • VonSzarvasVonSzarvas Posts: 3,451
    edited 2007-11-16 10:56
    Hello all,

    Just to close of this thread with a solution.

    With a fresh head this morning I followed the tip from JonnyMac to put a slight kink at the end of cables. So I "kinked" everything going into the breadboard, and also extended the legs of the MCP3202 chip, and now the circuit definitely runs on my old SX-Tech.

    Having also made the vero-board version, I can see the SX-Tech design is less stable with its results, so I guess the breadboard introduces noises that we won't get on a well laid-out pcb. But, it is very good for testing - of course!

    For reference I attach my final schematic, which is working with JonnyMac's SX/B code sample shown above. The code also includes provision to output the ADC readings to a serial port.


    Max.

    smile.gif
    832 x 560 - 32K
Sign In or Register to comment.