Mcp3202 spi adc
VonSzarvas
Posts: 3,451
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. 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.
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
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. 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
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
Post Edited (JonnyMac) : 11/15/2007 4:05:52 PM GMT
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...
I might solder up a test version if it is sure the code is correct.... Thus eliminating the breadboard....
Double hmm.
I think this should lead me to think its a program issue rather than a hardware issue?
Not sure..
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
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.
In the schematic VDD and VSS are shown unconnected. I assume this was simply done for convenience.
- Sparks
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.
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.