Using Serial Peripheral Interface (SPI) with Microchip PIC18 Families Microcontroller

Serial Peripheral Interface SPI

The Serial Peripheral Interface (SPI) is one of the popular embedded serial communications widely supported by many of today’s chip manufacture and it considered as one of the fastest serial data transfer interface for the embedded system. Because of its special in/out register configuration, the SPI master device could transfer its data and at the same time it receive a data from the SPI slave device with the clock speed as high as 10 MHz. Beside its superior data transfer speed; SPI also use a very simple data transfer protocol compared to the other serial data transfer methods. When the SPI master device want to send the data to the SPI slave device then the SPI master will just simply shifting its own data through a special 8-bits register and at the same time the SPI master will receive the data from the SPI slave into the same register as shown on this following picture:Serial Peripheral Interface SPI

With this circular shift register connection between the SPI master and the SPI slave devices, the complete data transfer from both devices will be accomplished in just 8 clock cycles. This means the SPI devices only need about 0.8 us to complete transfer the 8-bit data if we use 10 MHz clock. One of the drawbacks using the SPI especially when we use multiple SPI slave device is the SPI slave could not initiate sending its own data to the SPI master device, all the data transfer initiation is always come from the SPI master. The SPI master device has to poll each of the SPI slave devices to know whether the SPI slave device has a data to be sent to the SPI master device or not.

Polling the entire SPI slave devices will eventually consumed the SPI master resources when the SPI slave devices to be polled increase, therefore some of the SPI slave device is equipped with the interrupt pin to notify the SPI master device that it has a data to be read. You could read more about how SPI work in my previous posted blog Using Serial Peripheral Interface (SPI) Master and Slave with Atmel AVR Microcontroller.

The PIC18F14K22 Microcontroller

On this tutorial I will use the Microchip PIC18F14K22 microcontroller, this microcontroller is one of my favorite 8-bit 20-pins PIC18 microcontroller families members as it is equipped with sophisticated advanced peripheral inside such as ADC, USART, ECCP (Enhanced Capture/Compare/PWM), SPI, I2C and the SR Latch (555 Timer) module for capacitive sensing. With 16K bytes flash ram and equipped with the build in circuit debug, this 8-bit 20-pins microcontroller is a perfect choice for serious embedded application or just for hobbyist’s project.Schematic Using Serial Peripheral Interface SPI

The PIC18F14K22 microcontroller SPI peripheral support both master and slave mode but on this tutorial we will only exposing the PIC18F14K22 SPI master mode where on the first part we will expand the PIC18F14K22 microcontroller I/O by using the SPI I/O expansion chip and the second part we will turn the PIC18F14K22 microcontroller into a very useful SPI device testing tools that could be used to test and debug most of the SPI device chip available today. Both of these projects will give a good understanding and experience of how the PIC18F14K22 microcontroller SPI master peripheral works.

Now let’s list down all the necessary hardware and software needed to accomplished these projects:

  • Resistors: 330 Ohm (8) and 10K (1)
  • LEDS: 3 mm Blue LED (8) and 3 mm Red LED (1)
  • One momentary push button
  • One Breadboard and some breadboard’s jumper cables
  • PICJazz 20-PIN learning board with Microchip PIC18F14K22 microcontroller from ermicro
  • Microchip PICKit3 programmer (used in this project); you could also use the Microchip PICKit2 programmer.
  • Microchip MPLAB IDE version 8.47 and Microchip C18 Compiler version 3.30
  • Microchip Reference Document: PIC18F14K22 datasheet, MCP23S17 datasheet, and MCP42xxx datasheet

Read more: Using Serial Peripheral Interface (SPI) with Microchip PIC18 Families Microcontroller

Leave a Comment

Your email address will not be published. Required fields are marked *