SPI - The basics


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    653

    Default

    Quote Originally Posted by Bruce View Post
    SHIFTOUT is a software SPI communications routine. If you use it, don't enable the PICs hardware SPI. If you do, then the hardware SPI takes control of these pins, and SHIFTOUT won't work.
    Thanks for the heads up.

    Ok....struggling here! (being completely new to SPI!)

    I'd assumed the reason that SPI hw being available on the PIC was to assist in ease of comms with SPI devices....but now youre saying that h/w should not be used when shiftout is used? So (n00b hat on again!).... Why use shiftout wrt SPI then?

    How does shiftout 'marry' up with the SPI master clock (when using h/w presumably there's tight correlation between the SPI buffers being read out at the right time etc)

    If not using shiftout (as in my above code), how would getting an 11 bit control stream out the SDO pin (the least 3 significant bits of an upper byte + a complete lower byte) be approached in hw?

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405

    Default

    It's pretty much the same for any built-in hardware peripheral.

    Once you enable a peripheral, it takes control of the pins. With PBP SHIFTIN & SHIFTOUT you can use most any pin. With the hardware peripheral you're stuck using whatever pins are used for the hardware peripheral.

    Melabs has a few examples for using hardware SPI here http://melabs.com/resources/samples-pbp-general.htm but I would stick with SHIFTOUT & SHIFTIN unless you just want to learn how the hardware works.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    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 12:11.

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

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

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

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

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