SPI Interfacing


Closed Thread
Results 1 to 9 of 9

Thread: SPI Interfacing

  1. #1
    Join Date
    Jun 2006
    Posts
    15

    Default SPI Interfacing

    Hello all,
    Just writing to find out if there is any collection of information on SPI commands out there. I am using a PIC16F88 to interface with an ISD1700 Winbond Chipcorder and since SPI is standard with the F88 I thought that there would be a supported SPI command or some sort of Include file... Defines or even an SPI page in the manual but I didn't see anything that supported SPI in the PBP or Proton manuals... I did find some info on a few websites out there on the Web but I would have thought it was covered somewhere in one of the manuals?? It would be nice to see a list of commands that would be useful to initialize the module in the PIC and use it to communicate... or am I overlooking something? I know that you can use ShiftIN and ShiftOUT for SPI but it would be nice to use SPI_??? Is this even possible in PBP or Proton?

    Thanks!
    Dave

  2. #2
    Join Date
    May 2007
    Posts
    2

    Smile SPI methods

    I also looked for some examples last week but couldn't find any. I am using PIC 16F884. This has MSSP so life is actually very easy. I have extracted some snippets of code from my application so you can see how it works. The deeply indented code is not runnable but intended to demonstrate how the subroutines are called. I hope this helps. It runs my apps fine but I can't guarantee that it is the most efficient.

    I would be glad to explain if you need to contact me.


    'SPI setup
    pot_cs var PORTC.0 ' chip select
    a2d_cs var PORTC.1 'chip select
    amp_cs var PORTC.2 'chip select

    SSPCON.5=1 'enable SSP
    SSPCON.4=1 'Clock idles high bit name CKP
    SSPCON.3=0 ' bit 3 to 0 indicate clock speed. bit 0 set means clock = OSC/16 = 1.25 MHz
    SSPCON.2=0
    SSPCON.1=0
    SSPCON.0=1
    SSPSTAT.7=1 ' SPI Master mode
    SSPSTAT.6=0 ' data transmitted on rising edge of SCK
    TRISC.3 = 0
    TRISC.5 = 0

    vcount var word

    OUTPUT pot_cs
    OUTPUT amp_cs
    OUTPUT a2d_cs

    ' CODE TO CONTROL SPI POTENTIOMETER MicroChip MCP41050
    valuebyte = 128 ' VALUE OF POT 0 TO 255
    commandbyte = 17 ' bit 4 and 5 = command bits, bit 0 or 1 = channel select bit - 16 = write data, 1 = select CH0
    pot_cs = 0
    gosub sendspi
    pot_cs = 1


    'CODE TO CONTROL SPI AMPLIFIER MicroChip MCP6S21/2/6/8
    valuebyte = 0 'set gain INDEX
    commandbyte = 64 ' bit 5, 6 and 7 = command bits, bit 0 = register select - 0 = gain register, 1 = channel register
    amp_cs = 0
    gosub sendspi
    amp_cs = 1
    valuebyte = 0 ' select channel from input MUX
    commandbyte = 65 ' bit 5, 6 and 7 = command bits, bit 0 = register select - 0 = gain register, 1 = channel register
    amp_cs = 0
    gosub sendspi
    amp_cs = 1


    ' read SPI ADC MicroChip MCP3208
    valuebyte = 0
    commandbyte = 4 + 2 + channelnumber >> 2 ' SET BIT 2 HI FOR START BIT - bit 1 = single ended - bit 0 channelnumber may be 0 to 7
    secondbyte = channelnumber << 6 'D1,D0 shifted to top bits
    a2D_cs = 0
    gosub txrxspi
    a2D_cs = 1



    end

    sendspi:
    SSPBUF = commandbyte
    PAUSEUS 10
    SSPBUF = valuebyte
    PAUSEUS 7
    return

    txrxspi:
    SSPBUF = commandbyte
    PAUSEUS 10
    SSPBUF = secondbyte
    while SSPSTAT.0 = 0
    'spin
    WEND
    vcount = (SSPBUF & 15) << 8
    SSPBUF = valuebyte
    while SSPSTAT.0 = 0
    'spin
    WEND
    vcount = SSPBUF + vcount
    return

    end

  3. #3
    Join Date
    Jan 2005
    Location
    France
    Posts
    97

    Default SPI with a 16F88

    Hello,
    I have the same project for a friend who use SPI devices ..
    as far as I understand the 16F88 has a built in SPI option
    see datasheet copy
    10.2 SPI Mode
    This section contains register definitions and
    operational characteristics of the SPI module.
    SPI mode allows 8 bits of data to be synchronously
    transmitted and received simultaneously. To
    accomplish communication, typically three pins are
    used:
    • Serial Data Out (SDO) RB2/SDO/RX/DT
    • Serial Data In (SDI) RB1/SDI/SDA
    • Serial Clock (SCK) RB4/SCK/SCL
    Additionally, a fourth pin may be used when in a Slave
    mode of operation:
    • Slave Select (SS) RB5/SS/TX/CK
    When initializing the SPI, several options need to be
    specified. This is done by programming the appropriate
    control bits in the SSPCON register (SSPCON<5:0>)
    and the SSPSTAT register (SSPSTAT<7:6>). These
    control bits allow the following to be specified:
    • Master mode (SCK is the clock output)
    • Slave mode (SCK is the clock input)
    • Clock Polarity (IDLE state of SCK)
    • Clock Edge (output data on rising/falling
    edge of SCK)
    • Clock Rate (Master mode only)
    • Slave Select mode (Slave mode only)

    Question : I didn't see any Serial command in your example ?

    about the speed ? where to set it ?
    like all command .. just play with the SSP register bits ?

    may I use it like it is to "try something" mostly send commands
    to my friend's unit ..


    thanks in advance for your help

    Francois

  4. #4
    Join Date
    May 2007
    Posts
    2

    Smile SPI Interfacing

    Hi Francois,

    I will try to answer your questions.

    >>I didn't see any Serial command in your example ?

    There are three examples of communicating with SPI devices. Each begins with a comment describing the device they apply to. After setting the value of two or three Bytes (valuebyte, commandbyte, and secondbyte), a GOSUB is called to move the data. The subroutines are: "sendspi" used to send commands to the digital pot and the amplifier and and "txrxspi" to command an ADC to convert and read back the data. The result of the data read back is put into the variable vcount. I know this does not look like much code but it is all you have to do. Simple, isn't it!?!

    >>about the speed ? where to set it ?

    Bits 3 - 0 of the SSPCON register select transmission speed. It is a function of the MCPU clock. In my instance, I use a 20 MHz oscillator and define at the top of my code using the define: DEFINE OSC 20.

    >>may I use it like it is to "try something" mostly send commands to my friend's unit

    Feel free to use this any way you wish. Note that the Byte values developed are for specific devices so you will have to adapt them to do your task and create a loop for them to run in.

    I hope I have explained adequately.

    Cheers,
    Mike

  5. #5
    Join Date
    Sep 2003
    Location
    INDIA
    Posts
    161

    Default Thanks Mike

    Hey Mike,

    Thanks for that explaination. Surely a lot of people will gain from it.


    regards
    Charudatt

  6. #6
    Join Date
    Nov 2007
    Posts
    4

    Default SPI to 7seg.

    I've tried to drive a 2 digits 8" tall 7segment using 74HC595 shift register with no luck ...any code snippets or reference? btw I'm using F88..

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

    Default

    Hi and welcome on the forum,
    did you tried with Shiftin/Shiftout or with the MSSP module?

    please, when you post a question, give us all details you have such as your code, schematic, compiler version, device programmer, etc etc.
    Steve

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

  8. #8
    Join Date
    May 2004
    Location
    brighton
    Posts
    149

  9. #9
    Join Date
    Oct 2004
    Posts
    440

    Default

    See SPI clk speed adjustment post #13.
    First run program with Shiftin and Shiftout.

    Norm

Similar Threads

  1. Using SPI with External Interrupts
    By shaiqbashir in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 29th June 2008, 05:08
  2. 16-bit SPI problem
    By shaiqbashir in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 19th June 2008, 16:42
  3. SPI configuration PIC versus Atmel
    By Pedro Santos in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 10th July 2007, 20:17
  4. How to configure SPI in PICBASIC PRO?
    By moogle in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 15th April 2007, 19:31
  5. interfacing SPI with 2 pins?
    By modifyit in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 1st December 2005, 23:04

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