Monday, August 24, 2015

ARTICLE #3 - Arduino SPI communication using 74HC595 8-bit shift register


In this tutorial we are going to start learning about a great protocol used for implementing communication between microcontrollers and their peripheral devices on a serial bus. This protocol is generally used for its high speed data transfer rate than asynchronous protocol communications such as CAN and UART. I guess the question that comes directly to your mind is: what is an SPI protocol ? 


SPI protocol :



Serial peripheral interface, or SPI, is a synchronous serial communication protocol used by microcontrollers to communicate with one or more peripheral devices. The main difference between the SPI protocol and the old asynchronous communication protocol (ex: CAN bus) is that it has a clock signal that synchronizes the communication between devices. SPI protocol is used as a standard communication for  a variety of peripheral devices such as sensors, EEPROM memory, SD cards, etc.


Pin description :




In SPI communications, there is only one device that generates the clock speed which is usually a microcontroller (the Arduino board in this tutorial), it is called a Master. The other devices have to set up the same clock speed and they are called Slaves.



SPI bus: single master and single slave



  • MOSI : Master Out Slave In is the line used to send data from Arduino to the peripheral device.
  • MISO : Master In Slave Out is the line used to send from the peripheral devices to Arduino.
  • SCK : This line is used to transmit the clock signal generated by the Master device (Arduino) to the Slave device’s clock to synchronize data transmission.



When multiple peripheral devices are used, we add another line to select the device which is :



CS : Chip Select is the line that selects the slave device that the master wants to talk to. All CS lines are set to high (logic 1) which disconnects the slave devices from SPI bus. Before sending data, Arduino sets the desired  CS line to low to activate the peripheral device that it wants to communicate with. 






SPI bus: master and three independent slaves




For further information about the SPI protocol, you can visit these links to see more detailed illustrations :

Serial Peripheral Interface — Wikipédia



SPI protocol implementation :



     - SPI library :



It is a standard library which is included by default with the Arduino IDE, it allows you to use multiple functions used for establishing communication. You can add it by going to Sketch -> Import library -> SPI on your Arduino software, or by just adding this line to the beginning of your program :


 #include <SPI.h>    




Now that you included the SPI library into your program, you have to set up some functions to initiate communication :


    - Hardware connexions :




For arduino Uno board, you have to connect the pins as follows :



MOSI : pin 11
MISO : pin 12
SCK : pin 13
/Cs : You can use any digital pin and set it to output.




Example : using SPI with 74HC595 shift registers :



In this example, we are going to set up an SPI communication between the Arduino microcontroller and the 74HC595 via its serial data input DS. If you are not introduced to use shift registers you can check my previous article to see what it is and how it works.



This is a simple program that shows how to implement SPI communication, initialize it in the setup function and then use it to send data to the shift register’s DS input, the data sent is an 8-bit binary counter that increments every time the Chip Select goes low.



 //includes the functions necessary for SPI communication
 #include <SPI.h> 


 const int CS_pin = 10; //Chip Select


 void setup() {

       pinMode(CS_pin, OUTPUT); //CS as output
       SPI.begin(); //starts the SPI communication
       SPI.setBitOrder(MSBFIRST); //data sent MSB first

 }

 void binary_counter(int cnt) {
        
       digitalWrite(CS_pin, LOW); //Chip select low to select the slave device
       SPI.transfer(cnt); //data transfer
       digitalWrite(CS_pin, HIGH); //deselect the device by turning it to high
       cnt++; //counter incrementation
       delay(200);

 }

 void loop() { 
      
      int counter = 0;
      binary_counter( counter); //calls the binary_counter function

 }


No comments:

Post a Comment