PDA

View Full Version : SPI Interfacing



toofastdave
- 4th June 2007, 01:35
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

savvyguy
- 5th June 2007, 21:07
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

F1CHF
- 14th June 2007, 10:06
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

savvyguy
- 15th June 2007, 01:37
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

charudatt
- 15th June 2007, 06:25
Hey Mike,

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


regards
Charudatt

mongater
- 17th November 2007, 19:12
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..

mister_e
- 17th November 2007, 21:57
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.

isaac
- 18th November 2007, 11:28
see this tread
http://list.picbasic.com/forum/messages/4011/4733.html?

Isaac

Normnet
- 18th November 2007, 12:15
See SPI clk speed adjustment (http://www.picbasic.org/forum/showthread.php?p=32821#post32821) post #13.
First run program with Shiftin and Shiftout.

Norm