SPI COMMUNICATION INTRODUCTION:SPI stands for Serial Peripheral Interface, used for moving data simply and quickly from one device to another. It is the popular embedded serial communication that is widely supported by many of chip manufacture. It is considered as one of the fastest serial data transfer interface for the embedded system. Most of time, it is used to communicate data to and from the micro-microcontroller. SPI is frequently used when few I/O lines are available.


SPI is a synchronous protocol which allows transmission of data from a master device to one or more slave devices and from slave devices to master device over short distances at high speeds (MHz). Data is exchanged between these devices. SPI bus interface connection is incorporated into many devices like ADC, DAC and EEPROM.SPI is implemented in the PIC microcontrollers by a hardware module called the Synchronous Serial Port or the Master Synchronous Serial Port. This module is built into many PIC microcontrollers. It allows serial communication between two or more devices at a high speed and is reasonably easy to implement.spi communication

SPI communication BUS PINS

SPI was originally started by Motorola Corp, but now this standard is adopted my many semi-conductor chip companies.

It consists of total 4 pins:

  1. SDO: Serial data output from master and Input to slave [MOSI]
  2. SDI: Serial data input to master and output from slave [MISO]
  3. SCLK: Serial clock output from master [SCK] Input to slave [SCK]
  4. CE: Chip Enable or SS (Slave Select)

Only two pins SDI (Data in) and SDO (Data out) are for data transfer. This reduction of data pins reduces the package size and power consumption. SPI bus has the SCLK (serial clock) pin to synchronize the data transfer between two chips.It is generated by the master device and controls when data is sent and when it is read.The last pin of SPI bus is CE (Chip Enable) which is used to initiate or terminate the data transfer.When the CE signal goes low at a slave device, only that slave is accessed by SPI and will listen for SPI clock and data signals. These four pins make the SPI a 4-wire interface. In 3-wire interface we have SCLK, CE and only one single pin for data transfer.

SPI  communication DATA TRANSFER

In connecting a device with an SPI bus to a microcontroller, we use the MCU as a master device and the SPI device acts as slave. The clock signal is provided by the master to provide synchronization. MCU generate the SCLK which is fed to the SCLK pin of the SPI device. The SPI protocol uses SCLK to synchronize the transfer of one bit at a time and the MSB (Most significant bit) goes in first.

During transfer:

  • CE must be high, CE=1.
  • Address and data is transferred between MCU and slave in group of 8 bits.
  • Address byte is immediately followed by Data byte.
  • For Write: A7 bit of address byte is always 1
  • For Read: A7 bit of address byte is always 0


There are two modes of operation for SPI devices.

  1. Single byte write
  2. Multiple byte write

 Single Byte Write of SPI communication:

Following are the steps to send data in single byte mode for SPI devices.spi single byte writing

  1. Make CE=1 to begin writing.
  2. The 8-bit address is shifted one bit at a time with each edge of clock.
  • When all the 8 bits of address are sent, the SPI device will expects to receive the data for the particular address location.
  1. The 8bit data is shifted with one bit a time with each edge of clock.
  2. Make CE=0, which indicates the end of write cycle.

Multiple Burst Write of SPI communication

This mode loads consecutive locations. We just provide the address for first location, followed by the data for that location. From then on consecutive bytes are written to that consecutive memory location. SPI device internally increments the address location as long as CE is high.spi multi byte writing

Following are the steps to send data in single byte mode for SPI devices.

  1. Make CE=1 to begin writing.
  2. The 8-bit address of first location is provided and is shifted one bit at a time with each edge of clock.
  • The 8bit data for the first location is provided and is shifted with one bit a time with each edge of clock.
  1. From then on we just provide the consecutive bytes of data to be put in consecutive memory locations.
  2. CE must stay high to indicate that it is burst mode multi-byte write operation.
  3. Make CE=0, which indicates the end of write cycle.

SPI communication Multi-Byte writing (Burst mode)


MSSP (Master synchronous serial port) module inside the Pic MCU supports the SPI protocol. Three registers are associated with SPI of the MSSP module, which are:

SSPBUF         (synchronous serial port buffer Register)
SSPCON1       (synchronous serial port control Register)
SSPSTAT        (synchronous serial port status Register)

To transfer a byte of data, it is placed in SSPBUF. This register holds the byte received. It will then transfer the contents to the SSPSR. SSPSR is the synchronous shift register for the SPI module. It shifts data in and out of the device.

The SSPCON1 register controls the SPI mode operation.


SSPCON1:     SSP Control Register 1

SSPEN bit (Synchronous Serial Port Enable) in the SSPCON1 register must be set “High” to allow the use of Pins of pic MCU for SPI protocol.

We can also set SPI master mode using SSPM 3:0

spi frequency selection bits in pic microcontroller

SSPSTAT register


Sample bit:

  • SMP=1, input data sampled at the end of data output time
  • SMP=0, input data sampled at the middle of data output time

SMP must be cleared when SPI is used in slave mode

Buffer Full status bit:

  • BF=1, receive complete, SSPBUF is full
  • BF=0, receive not complete, SSPBUF is empty

Clock Edge select bit:

  • CKE=1, Transmit occurs on transition from active to idle clock state
  • CKE=0, Transmit occurs on transition from idle to active clock state


circuit digram of SPI communication interfacing with PIC microcontroller

The main devices used in the circuit are the two 18F452 (one acts as master and one as slave) and  LEDs. The 4 SPI control lines connect the two PICs together.spi communication with pic microcontroller circuit diagram

  • SCLK: RC3 (master)to RC3 (slave)
  • MOSI: RC5 (master) to RC4 (slave)
  • MISO: RC4 (master) to RC5 (slave)
  • SS:RA0 (master) to RA5 (slave)

7 LEDs will be used to display the data transferred to the SPI slave Pic MCU from the SPI master Pic MCU. The LEDs are connected to PORTB (RB0-RB6) of the slave Pic.

Code SPI communication with pic microcontroller

There will be two codes for this example. One for the SPI master and one for SPI slave.MPLAB code is given below for both devices.The master is sending commands and slave is receiving and displaying it on the LED output port. LEDs act like a binary counter, counting down to zero.

SPI communication Master Code:

void main(void){

unsigned x = 0xFF;

unsigned mask = 0x80;

TRISA = 0x00;

TRISC = 0x00;

PORTA = 0x03;



        PORTAbits.RA0 = 0;                 //Slave Select


        PORTAbits.RA0 = 1;                 //Slave Select


if(x == 0)

            x = 0xFF;





SPI communication Slave Code:

void main(void){

unsignedchar x;

ADCON1 =0b00000110;  //PORTA All Digital


TRISB =0x00;

PORTA =0x00;

PORTB =0x00;

TRISCbits.TRISC3 =1;                       //SCLK

TRISCbits.TRISC4 =1;                       //MOSI




        x =getcSPI();//ReadSPI();

        PORTB =(x>>1);





  1. Really helpful to understand for the beginners . Good work bro keep it up .
    Can you help me? I need to make a protocol for micro controller communication over power lines.

  2. Good explanation. I am confused about which PIC pin should I connect to SDI, SCLK and CS pins, if I want to use SPI available internally.

  3. I am interfacing 2 gb SD Card with PIC16F887, so is it possible to code for it without using FAT ….??? Kindly tell..


Leave a Comment