MCP3301 A/D converter SPI reading


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938

    Default MCP3301 A/D converter SPI reading

    Hi there,

    I'm trying to read a 13 bits MCP3301 A/D converter who's output value should go from 0 to 8191.

    Unfortunately, even if I can read something, it looks to be only the "upper half" of the expected full value.

    Name:  2024-02-01 22_10_51-Serial Communicator.jpg
Views: 1117
Size:  67.5 KB




    Could I have a hardware (wiring) issue or is there something in my code that would be wrong?
    Code:
    ' ====== DESCRIPTION ===============================================================================
    ' MCP3301: SPI 13Bits ADC
    
    ' ====== FUSES =====================================================================================
    ' PIC 16F690 Fuses - Internal oscillator (activate OSCCON register)
    
    '@ __config _FCMEN_OFF &_IESO_OFF &_CPD_OFF &_WDT_OFF &_INTRC_OSC_NOCLKOUT &_BOR_OFF &_CP_OFF &_PWRTE_OFF &_MCLRE_OFF
    
    #CONFIG
        __config _FCMEN_OFF &_IESO_OFF &_CPD_OFF &_WDT_OFF &_INTRC_OSC_NOCLKOUT &_BOR_OFF &_CP_OFF &_PWRTE_OFF &_MCLRE_OFF
    #ENDCONFIG
    
    @ ERRORLEVEL -306
    @ ERRORLEVEL -202
    
    ' ====== REGISTERS =================================================================================
    '             76543210
    OPTION_REG = %10000000 'PORT A&B Pull-Ups disabled (see WPUA & WPUB)
    OSCCON     = %01100000 'Internal RC set to 4MHZ
    ANSEL      = %00010100 'Select analog inputs Channels 0 to 7 (AN2 + AN4 selected)
    ANSELH     = %00000000 'Select analog inputs Channels 8 to 11
    ADCON0     = %10000000 'A/D Module (Bit5:2 select channels 0:11)
    ADCON1     = %00000000 'A/D control register
    CM1CON0    = %00000000 'Comparator1 Module is OFF
    CM2CON0    = %00000000 'Comparator2 Module is OFF
    INTCON     = %00000000 'INTerrupts CONtrol (TMR0 OFF)
    TRISA      = %00000100 'Set Input/Output (0 to 5)
    PORTA      = %00000000 'Ports High/Low (0 to 5)
    TRISB      = %00000000 'Set Input/Output (4 to 7)
    PORTB      = %00000000 'Ports High/Low (4 to 7)
    TRISC      = %00000001 'Set Input/Output (0 to 7)
    PORTC      = %00000000 'Ports High/Low (0 to 7)
    
    ' ====== DEFINES ===================================================================================
    DEFINE OSC 4
    DEFINE NO_CLRWDT 1   'Don't waste cycles clearing WDT  
    DEFINE HSER_CLOERR 1 'Automatic clear overrun error 
    DEFINE SHIFT_PAUSEUS 100
    
    ' ====== VARIABLES =================================================================================
    SD          var PORTB.4 'SPI Data pin
    CS          var PORTB.5 'SPI Chip Select
    SCK         var PORTB.6 'SPI Clock pin
    Serial_Out  VAR PORTB.7
    Mode        Var WORD
    MCP3301     Var WORD
    
    ' ====== INITIALIZE VARIABLES ======================================================================
    CS          = 1
    Mode        = 84 'serial com
    MCP3301     = 0
    
    ' ====== PROGRAM ===================================================================================
    MAIN:
    
        CS = 0                          'initiate 3301 comm
        SHIFTIN SD,SCK,1,[MCP3301\13]   'shift 13 bits of data in, lowest bit first
        CS = 1                          'stop 3301 comm
        
        Serout2 Serial_Out,Mode,["MCP3301: ", BIN13 MCP3301," - ", DEC4 MCP3301,13,10]
        
        PAUSE 1000
    
        GOTO MAIN
    END
    Attached Images Attached Images
    Last edited by flotulopex; - 1st February 2024 at 21:18.
    Roger

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,643


    Did you find this post helpful? Yes | No

    Default Re: MCP3301 A/D converter SPI reading

    according to the data sheet the data read is 12 data bits + sign bit
    Warning I'm not a teacher

  3. #3
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default MCP3301 A/D converter SPI reading

    Really?

    Name:  2024-02-01 22_43_50-Clipboard.jpg
Views: 1009
Size:  54.1 KB

    I don't get it
    Roger

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,643


    Did you find this post helpful? Yes | No

    Default Re: MCP3301 A/D converter SPI reading

    i'm looking at the data sheet you posted, that's what i see

    Name:  r2.jpg
Views: 1053
Size:  202.2 KB

    Name:  r1.jpg
Views: 1070
Size:  263.9 KB
    Warning I'm not a teacher

  5. #5
    Join Date
    Aug 2011
    Posts
    453


    Did you find this post helpful? Yes | No

    Default Re: MCP3301 A/D converter SPI reading

    Keep reading. It's a 13-bit differential converter.

    From the datasheet section 5.1:
    and the device uses the collected charge to produce a serial 13-bit binary two’s complement output code.
    So that's 12 bits of data + sign. See Section 6.1.

  6. #6
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default Re: MCP3301 A/D converter SPI reading

    Okay then, didn't see that one 😵

    So I'll get a 12 bits "value" and the last bit will indicate either "+" or "-", right?
    Roger

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,643


    Did you find this post helpful? Yes | No

    Default Re: MCP3301 A/D converter SPI reading

    the last bit will indicate either "+" or "-", right?
    if by last bit you mean the msb then yes
    not forgetting its a 13-bit binary two’s complement number
    Warning I'm not a teacher

  8. #8
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default MCP3301 A/D converter SPI reading

    not forgetting its a 13-bit binary two’s complement number
    ...and that means what exactly please?
    Roger

Similar Threads

  1. Reading DS1620 (SPI Temperature) from 18F46k22 Micro at 16MHz
    By JimAvanti in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 13th June 2015, 00:24
  2. Need to use SPI but I'm already using the SPI port
    By Christopher4187 in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 30th December 2012, 09:52
  3. SPI on a pic without hardware SPI?
    By modifyit in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 26th April 2006, 13:29
  4. Isolated A/D converter, SPI I/F
    By srob in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 3rd November 2004, 07:18
  5. help D/A converter
    By matelda in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 8th September 2004, 17:25

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts