SPI - The basics


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    653

    Default

    Thank (as ever) Bruce....I managed to get the hardware working (while testing I was sending a data byte of zeros to the digipot ....which ahem, being zeros don't show up on the PIC's SDO pin...doh, lol. Also, I was clocking data out of the pic on the wrong edge of the clock, which meant the least significant of the data byte was going awol from the digipot's perspective)

    So, while I'm what's the pros and cons of s/w SPI vs hw SPI?

    From the top of my head (and some assumptions)...

    sw SPI ...
    pros - flexibility of pin assignments, bit centric.
    cons - slower? can't use interrupts in the same program (this is an assumption on my part as I know serout/debug get corrupted when interrupts are used in the same program)

    hw SPI ...

    pros - can use interrupts in the same program?, faster?
    cons - tied to using the allocated SPI pins. byte centric?
    Last edited by HankMcSpank; - 9th September 2010 at 13:11.

  2. #2
    Join Date
    Feb 2008
    Location
    Michigan, USA
    Posts
    231

    Default Re: SPI - The basics

    Something seems weird about this interface.

    The datasheets that I have looked at seem to say that the hardware SPI is only capable of communicating in 8 bit bytes. So even if you were able to write a program that handled the HWSPI, you would be locked into devices that used 8 bits.
    I seems that so many of the desired devices that use SPI require more than that.

    The above D/A is one example. An SCA61T inclinometer that I'm playing with is using 11 bit data.

    I'm going to read the inclinometer and output to the D/A so it looks like a software SPI is the ONLY solution. I intent to try a variation on Bruce's multi-bit write above to do similar with the read so we'll see how that goes.

    Sure seems odd that is the case if I understand it correctly.

    Bo

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default Re: SPI - The basics

    It's just a matter of playing with it. If you need to send 11 Bits with the MSSP, then you just need to send 2 byte (2x8 bits) BUT mask/set the extra bits with 1 or 0.

    Have a look at MCP3204/3208 section 6
    http://ww1.microchip.com/downloads/e...doc/21298c.pdf

    If you really need speed, you want to use the MSSP port, case not, SHIFTIN/SHIFTOUT handle it.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Feb 2008
    Location
    Michigan, USA
    Posts
    231

    Default Re: SPI - The basics

    Thanks Steve!

    I appreciate that you took the time to give me the reference. Very helpful.

    Bo

  5. #5
    Join Date
    Feb 2008
    Location
    Michigan, USA
    Posts
    231

    Default Re: SPI - The basics

    Interesting, that explanation doesn't show up in either the MCP4821 or MCP4921 datasheets.

    Thanks again for the heads up

    Bo

  6. #6
    Join Date
    Feb 2008
    Location
    Michigan, USA
    Posts
    231

    Default Re: SPI - The basics

    After studying this for a few nights, the fog is beginning to clear a bit, but not completely.
    On the above mentioned inclinometer (sca61t, pages 11 &12), you send a command and then concurrently it sends back a reply with the data. I think I can see how to send the command, and extend the transmission to 19 clocks so that it can fit in the response by leaving CS low with the [var\19] in SEROUT. What I don't see how to grab those reply bits using SEROUT and SERIN. Each one would execute in order and not catch the response as both share the clock.
    Code:
    I VAR WORD
    D VAR WORD 
    CS_I      VAR           PORTC.2  ' chip select Inclinomteter
    CS_D      VAR           PORTC.3  ' chip select D/A
    SCK       VAR           PORTC.5 ' clock
    SDI       VAR           PORTC.4 ' data OUT
    SDO       VAR           PORTC.7 ' data IN
    IncIn     var           word 
    HIGH CS_I   
    HIGH CS_D
    LOW SCK
    I = %0001000000000000     'send RDAX & fill to get 11 bits of data 
    D = %0001111111111111        '2x, active, max output for test
    
    '*** Inclinometer Commands ***********
    'MEAS         00000000           'Measure (Exit self test)
    'RWTR         00001000           'R/W Temp     
    'RDSR         00001010           'Read Status register
    'RLOAD        00001011           'relaod NV to o/p
    'STX          00001110           'activate self test
    'RDAX         00010000           'read acceleration
    
    Main:
         GOSUB IncRdg
         GOSUB DACout
    goto main
    '**** Subs *********************************
    ;---- Sub to read the inclinometer ------------------
    IncRdg:
      LOW CS_i                       ' enable writes to Inclinometer
      SHIFTOUT SDI,SCK,0,[I\19]      'sends command to READ
    ' SHIFTIN SDO,SDK,0,[IncIn\11]  ' I don't see this working
      HIGH CS_I 
      PAUSE 5
      RETURN
    
    ''---- Sub to output to the D/A converter ----- 
    'DACout:
    '  LOW CS_D                       ' enable writes to D/A converter
    '  SHIFTOUT SDI,SCK,0,[D\16]
    '  HIGH CS_D 
    '  PAUSE 5
    '  return
    Will test further, but I suspect that I might have to resort to the hardware SPI.

    Any thoughts?
    Bo

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624

    Default Re: SPI - The basics

    Hi,
    I just took a quick look at this but as far as I can see the commands are 8 bits and the response from the device is 11 bits starting at the 9th clock pulse. So, if reading the acceleration you first SHIFTOUT the 8 bit RDAX command, then you SHIFTIN 11 bits for a total of 19bits.

Members who have read this thread : 1

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