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


Closed Thread
Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425

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

    I made my first project with a SPI interface (18F4550) recently but I'm never used it before this project. I need to add another component (MCP4911) but it uses SPI too. Can you make other pins SPI I/O's? The datasheet says it uses I2C but I've never used that before. Is it similar to SPI?

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

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

    Hi,
    The idea with SPI is that you can have multiple devices on the bus and then select which of them you want to communicate with by pulling that specific device chip select line low (it's always active low as far as I know).

    SPI and I2C are not compatible with each other and SPI devices and can not share bus with I2C devices. If you're using the MSSP module in the PIC (which can do SPI or I2C but not both at the same time) for the SPI chip then you're left with the software I2C commands - which can use "any" pins you like. Some PICs have two MSSP modules so you could set one up for SPI and the other for I2C.

    With that said, the datasheet for the MCP4911 seems pretty clear that it IS using SPI and not I2C, where did you get that?

    /Henrik.

  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

    With that said, the datasheet for the MCP4911 seems pretty clear that it IS using SPI and not I2C, where did you get that?
    I think I confused myself. I was looking at the MCP4011 before the MCP4911 and the MCP4011 states it uses I2C. I assumed all of the MCP identifiers use SPI but apparently that's not the case....

    SPI and I2C are not compatible with each other and SPI devices and can not share bus with I2C devices. If you're using the MSSP module in the PIC (which can do SPI or I2C but not both at the same time) for the SPI chip then you're left with the software I2C commands - which can use "any" pins you like. Some PICs have two MSSP modules so you could set one up for SPI and the other for I2C.
    So can you have all of the devices that use SPI have common SDO, CLK, SDI, etc and then each SPI module have discrete chip select lines?

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


    Did you find this post helpful? Yes | No

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

    I think the only thing MCP means is Microchip (TM).
    So can you have all of the devices that use SPI have common SDO, CLK, SDI, etc and then each SPI module have discrete chip select lines?
    Yes I believe so. It's important that each of the slaves put their respective data out pin (ie. MISO or SDO) in high impedence mode when their select line goes inactive. The DAC in question (MCP4911)doesn't even have a data output pin so there's nothing to worry about there. What's the other chip you're using?

    /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 using the MCP2515. I'd like to use the DAC as it will work perfect for my project. Being able to use multiple SPI devices on the same PIC is a nice feature. If using interrupts, are there other issues to consider with using the CS lines?

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


    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.

  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

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

  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

    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

  9. #9
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    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.

  10. #10
    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

  11. #11
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    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.

  12. #12
    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.

  13. #13
    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

  14. #14
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

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

    Hi,
    I'm glad you've got it working. I don't understand the reference to "an extra bit" but perhaps it doesn't matter now that it works. I see that you have 17 bits in your SHIFTOUT command but you're actually only sending 16.

    For reference, this is the datasheet I'm looking at. Section 5.2 explains how the write command works, ie 4 config bits and 12 databits for a total of 16bits. The transfer from the input shift register to the actual device will occur only if 16bits has been shifted in, you can't do less or it will be ignored.

    If LDAC is low the output voltage will be updated at the rising edge of CS. If LDAC is high at the rising edge of CS the output will be updated at the falling edge of LDAC. This allows multiple DACs to be connected to the SPI bus and writen one after the other and then, with their LDAC pins connected together, their outputs gets updated simultanously instead of one by one as they are written to by the SPI bus.

    On the next page, register 5.2 shows the write command register for the MCP4911 and what each of the config bits do. Again, no "extra bit" as far as I can see.

    You can't just write the 10 databits to the device, you must transfer all 16 bits including the config bits - even if they don't actually change. If you don't transfer 16bits the transfer will be ignored as explained in section 5.2.

    Here's something you could do to make it more clear what's happening.
    Code:
    OutWord VAR WORD
    
    WRITE_DAC CON 0
    Vref_BUF CON 16384
    Vref_UnBuf CON 0
    GAIN_1x CON 8192
    GAIN_2x CON 0
    VOut_ON CON 4096
    VOut_OFF CON 0
    
    OutWord = WRITE_DAC + VRef_UnBuf + GAIN_1x + VOut_ON + 456   ' Where 456 is your analog value.
    
    SHIFTOUT SDI, CLK, 1, [OutWord\16]
    The above would then shiftout the value 12744, which in binary is 0011000111001000.

    /Henrik.

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, 11: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, 12:41
  3. Port A and Pot...
    By bearpawz in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 19th November 2004, 17:19
  4. Duplicating port input to port output
    By lwindridge in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 26th April 2004, 21:43
  5. Lcd And Port A
    By vicce in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 21st February 2004, 09:22

Members who have read this thread : 2

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