SPI - Serial Peripheral Interface - on the PIC

From Mech
Jump to: navigation, search

Contents

Overview

SPI or Serial Peripheral Interface is a communication method that was once used to connect devices such as printers, cameras, scanners, etc. to a desktop computer. SPI runs as a master slave set-up and can run in full duplex mode, meaning that signals from the master to the slave and vis versa can be transferred at the same time. SPI involves four lines, and is therefore often termed the “four wire” serial bus. The four lines are:

SCLK — Serial Clock (output from master)

MOSI/SIMO — Master Output, Slave Input (output from master)

MISO/SOMI — Master Input, Slave Output (output from slave)

SS — Slave Select(active low; output from master)

SPI communication is done in bytes or words - a start signal, after which a given amount of data bits are transmitted serially, followed by a stop signal.

PIC Controller

Using SPI with the PIC and the CCS compiler is quite straightforward. Some versions of the PIC offer hardware support for SPI on reserved pins. However, the compiler also includes software SPI support, which allows the use of any of the communication pins on the PIC.

To set up SPI, all we need to do is include one line in the header of our source file:

#use spi(DO = <data out>, DI = <data_in>, CLK = <serial_clock>, ENABLE = <enable_pin>, BITS = <8|16>, etc. )


This includes code that implements software SPI communication on the pins defined in DO, DI, CLK and ENABLE. The use of the other options is device and application specific and a list of them is provided with the compiler documentation.

To actually send a word, one simply needs to use the spi_xfer() function:

spi_xfer(0x1337);


which, in our example, will send the hex word 0x1337 to the SPI slave.

Examples

Communication to the AD9833 is done through SPI.

References

Personal tools