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

  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

    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

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    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.

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

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    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.

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

  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

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

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    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 : 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