Shop OBEX P1 Docs P2 Docs Learn Events
MCP3208 only reading 1/2 of supplied voltage — Parallax Forums

MCP3208 only reading 1/2 of supplied voltage

BenjBenj Posts: 66
edited 2014-01-16 23:28 in BASIC Stamp
I am breadboarding an MCP3208 ADC on a BS2P24/40 demo board and am having a tough time figuring out why the inputs are only reading half of the voltage I'm putting on the input channels.

MCP3208 pin out:
1 (Channel 0) - +5v
2 (Channel 1) - +5v
9 (DGND) - vss
10 (CS) - BS pin 0
11 (DIN) - BS pin 3
12 (DOUT) - BS pin 2
13 (CLK) - BS pin 1
14 (AGND) - vss
15 (VREF) - vdd (+5)
16 (VDD) - vdd (+5)

Code:
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' -----[ I/O Definitions ]-------------------------------------------------

CS              PIN     0                         ' Chip Select 
Clock           PIN     1                         ' Clock       
DataOut         PIN     2                         ' DOUT
DataIn          PIN     3                         ' DIN

' -----[ Constants ]-------------------------------------------------------

Cnts2Mv         CON     $0139                     ' x 1.22 (To Millivolts)
Start_bit       CON     1
SE_0            CON     %1000
SE_1            CON     %1001
' -----[ Variables ]-------------------------------------------------------

result0         VAR     Word                      ' Conversion Result CH0
result1         VAR     Word                      ' Conversion Result CH1
mVolts0         VAR     Word                      ' Result0 --> mVolts
mVolts1         VAR     Word                      ' Result1 --> mVolts

' -----[ Init Setup ]------------------------------------------------------

DEBUG CLS, "ADC CH 0 : ", CR, "Volts    :", CR,
           "ADC CH 1 : ", CR, "Volts    :"

' -----[ Program Code ]----------------------------------------------------

DO
  LOW CS                                          ' Enable ADC
  SHIFTOUT DataIn, Clock, MSBFIRST, [Start_bit\1, SE_0\4]     ' Select CH0, Single-Ended
  SHIFTIN DataOut, Clock, MSBPOST, [result0\12]   ' Read ADC
  HIGH CS                                         ' Disable ADC
  mVolts0 = result0 */ Cnts2Mv                    ' Convert To Millivolts

  LOW CS                                          ' Enable ADC
  SHIFTOUT DataIn, Clock, MSBFIRST, [Start_bit\1, SE_1\4]     ' Select CH1, Single-Ended
  SHIFTIN DataOut, Clock, MSBPOST, [result1\12]   ' Read ADC
  HIGH CS                                         ' Disable ADC
  mVolts1 = result1 */ Cnts2Mv                    ' Convert To Millivolts

  DEBUG HOME, CRSRXY, 11, 0, DEC result0, CLREOL, ' Displays voltages & digital value
              CRSRXY, 11, 1, DEC mVolts0 DIG 3,   ' for both channels
                       ".", DEC3 mVolts0,
              CRSRXY, 11, 2, DEC result1, CLREOL,
              CRSRXY, 11, 3, DEC mVolts1 DIG 3,
                       ".", DEC3 mVolts1
  PAUSE 100
LOOP

When I apply +5v to channel 0 and/or channel 1, it only reads 2.502v (2047 in 12-bit) on the debug screen. The +5vdc on channels 0 and 1 is coming from VDD on the demo board (so the same as the supply for the chip, but that shouldn't matter?) This is originally code from a MCP3202, which didn't work until I altered the SHIFTOUT string. Is there something else in the code that isn't compatible with the 3208? Anyone see anything?

Benj

Comments

  • pmrobertpmrobert Posts: 677
    edited 2014-01-16 07:09
    I think you may need to use \13 instead of \12 - being off by 1/2 means you're doing an inadvertent divide-by-2 due to not pulling the entire set of result bits in. Here is code that works perfectly on MCP3208. It's PropBasic for the propeller but is close enough to PBasic to where you should be able to comprehend it. There are also some other differences such as toggling CS and my leaving the clock low after each conversion. I don't have the datasheet in front of me and don't specifically remember why I did it the way I did but seem to recall that it was important if multiple MCPs were used which they were in this project - 3 of them.
                    '*************************************
    		'*           ADC Section             *
    		'*************************************
    		' CHAN 0
    		chan = 0
    		adc_cmd = chan + %11000
    		high spi_cs0
    		low spi_cs0
    		shiftout spi_tx, spi_clk, msbfirst, adc_cmd\5, so_speed
    		shiftin spi_rx, spi_clk, msbpost, adc_cnt\13, so_speed
    		low spi_clk
    		wrlong ADC_0_H,adc_cnt 'multiply by 1.22 to get mv based on 5 volt vRef
    
    		' CHAN 1
    		chan = 1
    		adc_cmd = chan + %11000
    		high spi_cs0
     		low spi_cs0
    		shiftout spi_tx, spi_clk, msbfirst, adc_cmd\5, so_speed
    		shiftin spi_rx, spi_clk, msbpost, adc_cnt\13, so_speed
    		low spi_clk
    		wrlong ADC_1_H,adc_cnt 'multiply by 1.22 to get mv based on 5 volt vRef
    
  • BenjBenj Posts: 66
    edited 2014-01-16 08:10
    Yes, changing it to \13 worked. Thank you!
  • davejamesdavejames Posts: 4,047
    edited 2014-01-16 23:28
    Benj wrote: »
    Yes, changing it to \13 worked. Thank you!

    So Benj - that's good news! Care to mark your thread as "solved"?
Sign In or Register to comment.