Shop OBEX P1 Docs P2 Docs Learn Events
MCP3208 Code — Parallax Forums

MCP3208 Code

Steve AndruesSteve Andrues Posts: 29
edited 2009-08-27 02:12 in BASIC Stamp
Does anyone have any sample code for talking with the MCP3208 12-bit ADC?· I tried adapting the code from the 8-bit part, ADC8031·(Chapter 3: Basic Analog to Digital Conversion), but all I get are zeros.· The reference voltage is 4.096 volts (from an LM4040) to keep the math simple with a 12-bit ADC.· The data sheet seemed straight forward; Chip Select Low, Output four bits of configuration data, Input 12 bits of ADC data, then Chip Select High.· What am I missing?

Comments

  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2009-08-19 06:05
    Steve,

    Look at the attached datasheet on page #16 .. there is an additional clock after the configuration data and a null bit before the 12 data bits. That however shouldn't cause all Zero's unless the CS is not properly going high, and the MCP3208 thinks that the data has completed. From the datasheet: "After completing the data transfer, if further clocks are applied with CS low, the A/D converter will output LSB first data, followed by zeros indefinitely".

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Don MDon M Posts: 1,647
    edited 2009-08-19 17:27
    Steve,

    Just so happens I have recently been working with the MCP3202 (2 channel device) and I too would experience all 0's from one channel but not the other. I connected my Logic analyzer to monitor the timing and I discovered that the Data Out from the A/D chip would act strangely as compared to what the data sheet showed. After further reading I discovered that a 1uF bypass capacitor is recommended at the A/D chip from Vdd to ground. So if you have not already tried that you may see a difference. Look at page 23 section 6.2 of the data sheet.

    Don

    Added attachment- Logic analyzer shot of working device

    Post Edited (Don M) : 8/19/2009 5:40:16 PM GMT
    1449 x 454 - 405K
  • Steve AndruesSteve Andrues Posts: 29
    edited 2009-08-21 02:11
    Thanks Don,

    I added the cap but no help.· After playing with DIRS & OUTS the output was all 1s for a few seconds then goes to all 0s.· Following is the code, I hope that someone can see something amiss.



    Clk···· ·········· PIN·· 0··· 'Clock pin for·MCP3208
    ADCdataIn····· PIN·· 8··· 'Configures ADC
    ADCdataOut··· PIN·· 9··· 'Results of ADC Conversion
    ADC_CS·········PIN· 10··· 'ADC Chip Select

    Voltage········· VAR·· Word

    SE_0············ CON·· %1000···· 'Configuration nibble - Single Ended Channel 0

    OUTS = %0000001000000001····
    DIRS =· %0000001000000001

    '
    [noparse][[/noparse]Main Routine]

    DO
    ··· GOSUB ADC_Data
    ··· GOSUB Calc_Volts
    ··· GOSUB Display
    LOOP

    END
    '
    [noparse][[/noparse] End Program ]

    '
    [noparse][[/noparse] Subroutines ]

    ADC_Data:
    ·· LOW ADC_CS······························································ 'Chip Select low
    ·· SHIFTOUT ADCdataIn, Clk, MSBFIRST, [noparse][[/noparse]SE_0\4]················ 'Output configuration·nibble to single-ended on·channel 0
    ·· SHIFTIN ADCdataOut, Clk, MSBPOST, [noparse][[/noparse]Voltage\12]············ 'get data
    ·· HIGH ADC_CS······························································ 'de-select chip
    RETURN


    Calc_Volts:·············· 'saved 'till the conversion gets fixed
    RETURN

    Display:
    ·· DEBUG HOME
    ·· DEBUG "12-bit binary Value:·· ", BIN12 voltage·················· 'print binary value of·Voltage
    ·· DEBUG CR, CR, "Decimal value:··· ", DEC4 voltage·············· 'print numeric value of Voltage
    RETURN
  • phil kennyphil kenny Posts: 233
    edited 2009-08-21 03:49
    I see two problems:

    1) You need to send the START bit with the SHIFTOUT command

    2) You need to have 13 clocks for the SHIFTIN command.

    Change those lines to read
    Start_bit  CON  1
    SHIFTOUT ADCdataIn,Clk,MSBFIRST, [noparse][[/noparse]Start_bit\1, SE_0\4]   ' Send start bit and select channel
    SHIFTIN ADCdataOut, Clk,MSBPOST,[noparse][[/noparse]Voltage\13]  ' Get data bits.
    
    



    From the MCP3204 data sheet:

    "The first clock received with CS low and DIN high will constitute a start bit.

    The SGL/DIFF bit follows the start bit and will determine if the conversion will be done
    using single-ended or differential input mode.

    The next three bits (D0, D1 and D2) are used to select the input channel configuration.

    Once the D0 bit is input, one more clock is required to complete the sample and hold period
    (DIN is a “don’t care” for this clock). On the falling edge of the next clock, the device will output a
    low null bit. The next 12 clocks will output the result of the conversion with MSB first."

    phil

    Post Edited (phil kenny) : 8/21/2009 5:43:50 PM GMT
  • Steve AndruesSteve Andrues Posts: 29
    edited 2009-08-27 02:12
    Success!!· After rebuilding the project and finding one wire not making contact, everything works fine!· The results make a 12 bit version of the Chapter #3 Basic Analog to Digital Conversion, which was 8-bit; this was the objective.

    Thank you to everyone who helped me with input.

    If you want to reproduce my results:
    1) The voltage reference was an LM4040 (made by both Texas Instruments and National Semiconductor) with a 4.096 volt output.· It is being fed through a 4.7k ohm resistor.
    2) Use a .1 uF capacitor on the power input as well as the ADC inputs

    Here is the working code:


    '
    [noparse][[/noparse] I/O Definitions ]

    Clk····· ············ PIN·· 0··· 'Clock pin for MCP3208 pin 13
    ADCdataIn···· · PIN·· 1··· 'Configures ADC - Din pin 11
    ADCdataOut··· PIN·· 2··· 'Results of ADC Conversion - Dout pin 12
    ADC_CS········· PIN·· 3··· 'ADC Chip Select - pin 10

    'Define ADC Variables

    Start_Bit·········· CON·· 1
    Voltage············ VAR·· Word

    'Define ADC Constants

    SE_0····· CON·· %1000···· 'Single Ended Channel 0
    SE_1····· CON·· %1001···· 'Single Ended Channel 1
    SE_2····· CON·· %1010···· 'Single Ended Channel 2
    SE_3····· CON·· %1011···· 'Single Ended Channel 3
    SE_4····· CON·· %1100···· 'Single Ended Channel 4
    SE_5····· CON·· %1101···· 'Single Ended Channel 5
    SE_6····· CON·· %1110···· 'Single Ended Channel 6
    SE_7····· CON·· %1111···· 'Single Ended Channel 7
    Diff_0···· CON·· %0000···· 'Differential CH0=IN+, CH1=IN-
    Diff_1···· CON·· %0001···· 'Differential CH0=IN-, CH1=IN+
    Diff_2···· CON·· %0010···· 'Differential CH2=IN+, CH3=IN-
    Diff_3···· CON·· %0011···· 'Differential CH2=IN-, CH3=IN+
    Diff_4···· CON·· %0100···· 'Differential CH4=IN+, CH5=IN-
    Diff_5···· CON·· %0101···· 'Differential CH4=IN-, CH5=IN+
    Diff_6···· CON·· %0110···· 'Differential CH6=IN+, CH7=IN-
    Diff_7···· CON·· %0111···· 'Differential CH6=IN-, CH7=IN+


    '
    [noparse][[/noparse]Main Routine]

    DEBUG CLS························· ····· 'Start Display

    DO······························· ················ 'Loop Forever
    ··· GOSUB ADC_Data·················· 'Make Conversion
    ··· GOSUB Display············ ··········· 'Show Results
    LOOP
    END
    '
    [noparse][[/noparse] End Program ]


    '
    [noparse][[/noparse] Subroutines ]
    ADC_Data:
    ·· LOW ADC_CS············································································ 'Select ADC
    ·· SHIFTOUT ADCdataIn, Clk, MSBFIRST, [noparse][[/noparse]Start_Bit\1, SE_1\4]· 'Shift 1 start conversion bit
    ········································································································· 'and ADC configuration nibble
    ·· SHIFTIN ADCdataOut, Clk, MSBPOST, [noparse][[/noparse]Voltage\13]················· 'get data
    ·· HIGH ADC_CS············································································· 'De-select ADC
    RETURN

    Display:
    ·· DEBUG HOME
    ·· DEBUG "12-bit binary Value: ", BIN13 voltage
    ·· DEBUG CR, CR, "Decimal value:······ ", DEC4 voltage
    ·· DEBUG CR, CR, "DVM Reading:········ ", DEC1 Voltage/1000, ".", DEC3 Voltage//1000
    RETURN


    ·
Sign In or Register to comment.