Need to use SPI but I'm already using the SPI port


Closed Thread
Results 1 to 14 of 14

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,627


    Did you find this post helpful? Yes | No

    Default Re: Need to use SPI but I'm already using the SPI port

    Hi,
    In the case of MCP2515 and MCP4911 there really can't be any conflicts since only one of them actually puts data out on the MISO line.

    With SPI it's always the master (the PIC in this case) that initiates and "drives" the communication with the slave devices, the slave devices can not start transfering on their own. The INT\ pin on the MCP2515 - for example - is used to let the master device know that the slave needs attention. If the master is in the middle of a "converstation" with another device it's up to the master to decide if it should answer the slaves request for attention or wait (and possibly risk missing something important).

    Again, with SPI there's one master (the PIC in this case) and, at least, one slave. The master provides the clock and only the slave whos CS\ line is active (low) will listen to data being put out by the master on the SDI/MOSI line and only the slave whos CS\ line is active will (or at least should) put data out on the SDO/MISO line. A SPI slave device can never "send" anything on its own since the clock to actually shift data out on the SDO/MISO line is generated by the master.

    /Henrik.

  2. #2
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: Need to use SPI but I'm already using the SPI port

    Great information, Henrik. Thanks for the detailed explanation. More questions to follow soon.

  3. #3
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: Need to use SPI but I'm already using the SPI port

    Roadblock #1.

    I got a hold of a MCP4911E. If I understand the datasheet correctly, this chip has literally one command that is a word sized variable. I'm using the internal oscillator on a 16F690. What am I doing wrong?
    Code:
    ' ================================================================
      CS VAR PORTC.7 
     CLK VAR PORTC.1
      SDI VAR PORTC.0
      LDAC VAR PORTC.2
     
      
    '                      
    ' ================================================================
     
      
     X       VAR WORD   'GENERAL TIMER
     
    '                           PROGRAM INIT
    ' ================================================================
       
    '                            MAIN LOOP
    ' ================================================================
    CS=1
    X=0
    MAIN:
    TOGGLE PORTB.4
    
    GOSUB SEND_VOLTS
    
        
    PAUSE 100
    GOTO MAIN
    
    '                             
    ' ================================================================    
    
    SEND_VOLTS:
    TOGGLE PORTB.7
    TOGGLE PORTB.5
    TOGGLE PORTB.6
    TOGGLE PORTB.7
        LOW CS               
        LOW LDAC
        PAUSE 100
        SHIFTOUT SDI,CLK,0, [%011100111110]    
        HIGH CS             
        'HIGH LDAC
    RETURN

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,627


    Did you find this post helpful? Yes | No

    Default Re: Need to use SPI but I'm already using the SPI port

    Hi,
    Never used it and don't have access to one to try with.
    BUT the datasheet says that all write operations are 16 bits, 4 configuration bits followed by 12 databits where the 2 least significant bits doesn't matter for the 10-bit MCP4911. You're only sending 12 bits.
    You're using MODE 0, which accordning to the manual shifts data out LSB first while the MCP4911 expects MSB first. Doesn't really matter as long as you remember to put the bits in the correct order before sending them. SHIFTOUT does 8 bits by default, if you want anything else (up to 32) you need to specify that.

    Also, make sure you don't have any analog functions (comparator, ADC) mutliplexed onto the pins you're using. They are usually, but not always, turned on by default - always double check if you haven't already.

    I'd probably try:
    Code:
    LOW CS
    SHIFTOUT SDI, CLK, 1, [%0011010101010100\16]
    HIGH CS
    LOW LDAC
    PAUSEUS 100
    HIGH LDAC
    /Henrik.

  5. #5
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: Need to use SPI but I'm already using the SPI port

    I'm not getting anything. This is the entire program. Do I need to configure the SSPSTAT register or one that I missed?

    Code:
     include "modedefs.bas"
    '                REGISTERS AND PINOUT ( 1 = IN; 0 = OUT )
      OPTION_REG = %10000000 'PORT A&B Pull-Ups disabled (look WPUA & WPUB)
       ANSEL      = %00000000 'Disable analog inputs Channels 0 to 7
       ANSELH     = %00000000 'Disable analog inputs Channels 8 to 11
       WPUB       = %00000000 'Disable weak pull-ups
       ADCON0     = %00000000 'A/D Module is OFF
       CM1CON0    = %00000000 'Comparator1 Module is OFF
       CM2CON0    = %00000000 'Comparator2 Module is OFF
       INTCON     = %00000000 'INTerrupts CONtrol
       TRISA      = %00000000 '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      = %00000000 'Set Input/Output (0 to 7)
       PORTC      = %00000000 'Ports High/Low (0 to 7)
     
    '                         ALIAS & MODIFIERS
    ' ================================================================
      CS VAR PORTC.7 
     CLK VAR PORTC.1
      SDI VAR PORTC.0
      LDAC VAR PORTC.2
      
    '                        VARIABLES & COSTANTS
    ' ================================================================
     X       VAR WORD   'GENERAL TIMER
     
    '                           
    ' ================================================================
       
    '                            MAIN LOOP
    ' ================================================================
    CS=1
    X=0
    HIGH LDAC
    MAIN:
    TOGGLE PORTB.4 
    GOSUB SEND_DATA    
    PAUSE 1000
    GOTO MAIN
    '                              SUB - ROTINES
    ' ================================================================    
    
    SEND_DATA:
    TOGGLE PORTB.7
    TOGGLE PORTB.5
    TOGGLE PORTB.6
    TOGGLE PORTB.7
    LOW CS                 
    SHIFTOUT SDI, CLK, 1,[%1001111111111111/16] 
    high cs
    LOW LDAC
    PAUSE 10
    HIGH LDAC                       
    RETURN

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,627


    Did you find this post helpful? Yes | No

    Default Re: Need to use SPI but I'm already using the SPI port

    Hi,
    SHIFTIN/SHIFTOUT are bit-banged commands and does NOT use the SSP/MSSP module so you don't need to set that up. There are no "high level" PBP commands to do SPI (or I2C) with the SSP/MSSP module so you'll need to "drive it" manually - which isn't that hard. But again, since you're using SHIFTOUT it doesn't matter.

    Right now you're sending MSB first - which is great. But the very first bit (Bit 15) you're sending is '1' which, if you look at the datasheet, means 'Ignore this command'. So, if everything else is alright, it should do exactly what you're seeing - nothing.

    From the datasheet:
    bit 15
    0 = Write to DAC register
    1 = Ignore this command


    bit 14 BUF: VREF Input Buffer Control bit
    1 = Buffered
    0 = Unbuffered

    bit 13 GA: Output Gain Selection bit
    1 = 1x (VOUT = VREF * D/4096)
    0 = 2x (VOUT = 2 * VREF * D/4096)

    bit 12 SHDN: Output Shutdown Control bit
    1 = Active mode operation. VOUT is available.
    0 = Shutdown the device. Analog output is not available. VOUT pin is connected to 500 ktypical)

    bit 11-0
    D110: DAC Input Data bits. Bit x is ignored.
    Do you have a scope or logic analyzer you can use to verify that the pins are doing what they should?

    /Henrik.

  7. #7
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: Need to use SPI but I'm already using the SPI port

    You located the problem Henrik! Unless I missed it, I didn't see where the datasheet indicates you must send an extra bit before sending data. I'm guessing this is something I just should have known? Maybe I'm just not understanding it, I don't know.

    Anyhow, it seems to work well, I like it. I know there's a different way to do it but I've tried a few different things and I can't get it to work like I want it. I'll be using this in the "off" position or on with a continually adjusting voltage.

    If I need it off, I can just send this command: SHIFTOUT sdi,clk, 1, [%00110000001101100\16]

    When I send a specific voltage, I would send the above command with the SHDN bit set to zero. Now, when I want to change just the 10 bit number, how can I do it? The first five bits will have two numbers for my application, which are %00110 or %00111. Obviously the 10 bit number changes but isn't it possible to send something like SHIFTOUT sdi,clk, 1, [SET, VOLTAGE] where "set" is %00111 and "voltage" is %000001101100.

  8. #8
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: Need to use SPI but I'm already using the SPI port

    Awesome! I found the answer I was looking for. It was located in this post: http://www.picbasic.co.uk/forum/show...6874#post16874

Similar Threads

  1. 18F2520 - problem configuring fuse for port B as digital port.
    By hwhisperer in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th October 2010, 12:41
  2. LCD R/S works on Port A but not Port B
    By TDonBass in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 10th February 2009, 13:41
  3. Port A and Pot...
    By bearpawz in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 19th November 2004, 18:19
  4. Duplicating port input to port output
    By lwindridge in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 26th April 2004, 22:43
  5. Lcd And Port A
    By vicce in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 21st February 2004, 10:22

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