{"id":2563,"date":"2019-02-12T18:19:52","date_gmt":"2019-02-12T16:19:52","guid":{"rendered":"https:\/\/deepbluembedded.com\/?p=2563"},"modified":"2023-08-17T23:52:50","modified_gmt":"2023-08-17T20:52:50","slug":"spi-tutorial-with-pic-microcontrollers","status":"publish","type":"post","link":"https:\/\/deepbluembedded.com\/spi-tutorial-with-pic-microcontrollers\/","title":{"rendered":"SPI Tutorial With PIC Microcontrollers | Serial Peripheral Interface"},"content":{"rendered":"\n\n\n\n\n
\"Previous<\/a><\/td>\nPrevious Tutorial<\/strong><\/a><\/td>\nTutorial 21<\/span><\/strong><\/span><\/td>\nNext Tutorial<\/strong><\/td>\n\"Next<\/td>\n<\/tr>\n
<\/td>\nSPI Tutorial | Serial Peripheral Interface<\/strong><\/td>\n<\/td>\n<\/tr>\n
<\/td>\nIntermediate Level\u00a0\u2605\u2605\u2606\u2606\u2606<\/strong><\/span><\/td>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

 <\/p>\n

In this tutorial, we’ll be discussing the SPI (serial peripheral interface) bus. How it works and how to establish serial communication between 2 PIC microcontrollers via SPI. This is going to be an extensively detailed guide for the ins and outs of the SPI communication in general, and programming PIC MCUs to be SPI master\/slave in particular on MPLAB IDE and XC8 Compiler. So, it’s essentially a long-read and hopefully helpful. And let’s get started!<\/span><\/p>\n

[toc]<\/p>\n


\n

\u00a0 \u00a0Required Components\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Qty.<\/strong><\/span><\/td>\nComponent Name<\/strong><\/span><\/td>\nBuy<\/strong> On Amazon.com<\/span><\/td>\n<\/tr>\n
2<\/td>\nPIC16F877A<\/span><\/td>\nAdd<\/a><\/td>\n<\/tr>\n
2<\/td>\nBreadBoard<\/span><\/td>\nAdd<\/a><\/td>\n<\/tr>\n
8<\/td>\nLED<\/span><\/td>\nAdd<\/a>\u00a0 \u00a0 Add<\/a><\/td>\n<\/tr>\n
1<\/td>\nResistors Kit<\/span><\/td>\nAdd<\/a>\u00a0 \u00a0 Add<\/a><\/td>\n<\/tr>\n
1<\/td>\nCapacitors Kit<\/span><\/td>\nAdd<\/a>\u00a0 \u00a0 Add<\/a><\/td>\n<\/tr>\n
1<\/td>\nJumper Wires Pack<\/span><\/td>\nAdd<\/a>\u00a0 \u00a0 Add<\/a><\/td>\n<\/tr>\n
1<\/td>\nLM7805 Voltage Regulator (5v)<\/span><\/td>\nAdd<\/a><\/td>\n<\/tr>\n
1<\/td>\nCrystal Oscillator<\/span><\/td>\nAdd<\/a><\/td>\n<\/tr>\n
1<\/td>\nPICkit2 or 3 Programmer<\/span><\/td>\nAdd<\/a><\/td>\n<\/tr>\n
2<\/td>\n9v Battery or DC Power Supply<\/span><\/td>\nAdd<\/a>\u00a0 \u00a0 Add<\/a>\u00a0 \u00a0 Add<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

The Prototyping Board Setup<\/a><\/strong><\/p>\n

\"Prototyping<\/a><\/p>\n


\n

\u00a0 \u00a0Serial Communication Introduction (Review)\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n

 <\/p>\n

[<\/strong> Disclaimer: This section has been discussed earlier in Tutorial-18 For UART. If you’ve already read it, just skip to the next one. ]<\/strong><\/p>\n

In Embedded Systems, Telecommunication, and Data Transmission applications, Serial Communication<\/strong> is known to be the process of sending data one bit at a time (bit-by-bit) sequentially, over the serial bus. It takes a complete clock cycle in order to transfer each bit from an end to the other. Conversely, parallel communication is known to be the process of sending several\u00a0bits, even bytes, as a whole only in a single clock cycle. However,\u00a0even if you transfer fewer data per cycle with a serial transmission, you can do it at much higher frequencies which results in higher net transfer rates than of the parallel communication.<\/span><\/p>\n

\u00a0The Fundamental Concepts\u00a0<\/span><\/h4>\n

There do exist many serial communication protocols, each of which is working in a different way. However, it turns out to be some similarity that they do share in common. Frankly speaking, serial communication protocols have Shift Registers<\/strong> somehow\/somewhere in their\u00a0hardware implementation as the working-horse (core). Shift registers are used to shift out the data to be transmitted bit-by-bit each clock cycle. So, let’s dig deeper into a shift register in order to build up a complete understanding of what is actually happening at the Register(low)-Level.<\/span><\/p>\n

Shift registers are basically some D-Flip-Flops serially connected while sharing the same clock line. Here is a graphical animation that demonstrates\u00a0how does a shift register work internally.<\/span><\/p>\n

\"Shift<\/p>\n

 <\/p>\n

As you might have noticed, the data input (0’s and 1’s) is being shifted from the input pin to the output end at bit-0. It takes only 1-clock to transfer a single bit, which means it takes 8-clocks for a single byte transfer. For the same of simplicity, we’ll represent the shift register as a single block instead of a series of D-Flip-Flops as shown above.<\/span><\/p>\n

 <\/p>\n

Here is an animation for an 8-Bit shift register with a serial input & serial output data lines.<\/span><\/p>\n

\"Shift<\/p>\n

 <\/p>\n

Well, now you should know how shift registers are actually working. Frankly speaking, serial communication is as simple as connecting a couple of shift registers together! Connecting the data output of a shift register to be the data input of the other shift register enables us of sending digital data serially from an end to another!<\/span><\/p>\n

 <\/p>\n

In the following animation, I’m connecting a couple of 4-Bit shift registers. One at the transmitter device and the other at the receiver device. The serial bus consists of a couple of wires (data, and clock). Each clock, a bit is sent from the transmitter TX pin and received by the receiver’s RX pin.<\/span><\/p>\n

\"Serial<\/p>\n

As you might have noticed, it takes 4-clocks to send the 4-Bit data from the transmitter to the receiver. This is simply the short answer to the “How Serial Communication Works?”. However, it’s not everything you should know about serial communication or at least to the level of being able to implement a simple serial data transfer process.<\/span><\/p>\n

In fact, there are some other options and configurations for each serial communication protocols. Which includes, various data transmission rates, error detection, and correction mechanisms, and much more that adds to the overall complexity associated with each protocol. We’ll be working with the UART protocol in this tutorial, which has a decent steep learning curve.<\/span><\/p>\n

\u00a0Serial VS Parallel Communication\u00a0<\/span><\/h4>\n

As we’ve stated earlier, serial communication is the process of sending data each bit at a time (bit-by-bit). And conversely, parallel communication is the process of sending multiple bits, even bytes, as a whole in a single clock cycle.<\/span><\/p>\n

A very basic implementation, that you can create on your own, for the parallel data transfer is shown down below. As I’m connecting a couple of 4-Bit registers, via the 5-wire parallel bus (4-data pins + 1-clock). Here is an animation that shows you how parallel data transfer is done.<\/span><\/p>\n

\"Parallel<\/p>\n

As you might have noticed, it takes only 1-clock to transfer the data from a transmitter device to the receiver! Frankly speaking, we can theoretically transfer any number of bits (or bytes) using parallel communication protocols in a single clock cycle at a time.\u00a0<\/span><\/p>\n

Here is a brief comparison between serial communication and parallel communication protocols.<\/span><\/p>\n\n\n\n\n\n\n\n\n\n
Feature<\/strong><\/span><\/td>\nSerial Communication<\/span><\/strong><\/td>\nParallel Communication<\/span><\/strong><\/td>\n<\/tr>\n
Relative Speed<\/strong><\/span><\/td>\nFaster<\/span><\/td>\nSlower<\/span><\/td>\n<\/tr>\n
Distance Range<\/strong><\/span><\/td>\nMuch Farther<\/span><\/td>\nShorter<\/span><\/td>\n<\/tr>\n
Transfer Method<\/strong><\/span><\/td>\nOne bit is transmitted at a time<\/span><\/td>\nBytes are transmitted in parallel, One byte or more at a time<\/span><\/td>\n<\/tr>\n
Applications<\/strong><\/span><\/td>\nComputer small peripherals, modules interfacing, and sensors measuring & sending simple data frames<\/span><\/td>\nShort distance High-Speed communication such as Computer printers, etc\u00a0<\/span><\/td>\n<\/tr>\n
Wires Inside<\/strong><\/span><\/td>\nFew wires, all data bits pass only through the same data line<\/span><\/td>\nMultiple, more wires, each bit has a dedicated wire. So as to be transmitted all at once<\/span><\/td>\n<\/tr>\n
Cables Pictures<\/strong><\/span><\/td>\n\"Ethernet\"USB \"Serial<\/td>\n\"Parallel \"Parallel \"Parallel<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u00a0Applications Of Serial Communication Protocols\u00a0<\/span><\/h4>\n

There are countless situations in which we do use one or more of the serial communication protocols. Here, I’ll try to categorize the main purposes for which we use serial communication instead of listing down all possible applications that may be serving the same purposes. Hence, any application will, hopefully, possibly fall under the umbrella of the following purposes.<\/span><\/p>\n