You are currently viewing Basic SPI programming in Embedded C
SPI Communication in embedded c

Basic SPI programming in Embedded C

Spread the love

Intro:

We know that SPI stands for the Serial Peripheral Interface. SPI developed by Motorola in the mid-1980.
It is a one of the serial communication protocol for connect low-speed devices.
It is commonly used for communication with flash memory, ram disk, sensor, real-time clock (RTC), analog-to-digital converters, and more.
SPI is a full-duplex synchronous serial communication, which means that data can be simultaneously transmitted from both directions.
The main advantage of the SPI is to transfer the data without any interruption.

SPI Interface:

Basically SPI protocol uses the four wires for the communication purpose.

SPI Interface
  • SCL Serial clock
  • MISO Master In Slave Out
  • MOSI Master Out Slave In
  • SS Slave Select

(Note : For Communication SS Must be ‘0’ OR Low Similar For Stop Communication SS Must be ‘1’ OR High)

Different operating modes

In SPI Communication Protocol number of Slave select increase with n number of slave so we use another method Daisy Chain Method

Daisy Chain Method

Daisy Chain Method

There four operating modes in this method depending on Rising and Falling Edge of clock pulse

Operation Mode Clock pulseRising EdgeFalling Edge
Mode 000
Mode 101
Mode 210
Mode 311
Operation mode in SPI

Sample C code for SPI Communication in Embedded

Master Slave Arduino communication:

//Master Arduino Code:

//SPI MASTER

//SPI COMMUNICATION BETWEEN TWO ARDUINO 

//CIRCUIT DIGEST


#include<SPI.h>                             //Library for SPI 

#define LED 7           

#define ipbutton 2

int buttonvalue;

int x;

void setup (void)


{

  Serial.begin(115200);                   //Starts Serial Communication at Baud Rate 115200 

  

  pinMode(ipbutton,INPUT);                //Sets pin 2 as input 

  pinMode(LED,OUTPUT);                    //Sets pin 7 as Output

  

  SPI.begin();                            //Begins the SPI commnuication

  SPI.setClockDivider(SPI_CLOCK_DIV8);    //Sets clock for SPI communication at 8 (16/8=2Mhz)

  digitalWrite(SS,HIGH);                  // Setting SlaveSelect as HIGH (So master doesnt connnect with slave)

}


void loop(void)

{

  byte Mastersend,Mastereceive;          


  buttonvalue = digitalRead(ipbutton);   //Reads the status of the pin 2


  if(buttonvalue == HIGH)                //Logic for Setting x value (To be sent to slave) depending upon input from pin 2

  {

    x = 1;

  }

  else

  {

    x = 0;

  }

  

  digitalWrite(SS, LOW);                  //Starts communication with Slave connected to master

  

  Mastersend = x;                            

  Mastereceive=SPI.transfer(Mastersend); //Send the mastersend value to slave also receives value from slave

  

  if(Mastereceive == 1)                   //Logic for setting the LED output depending upon value received from slave

  {

    digitalWrite(LED,HIGH);              //Sets pin 7 HIGH

    Serial.println("Master LED ON");

  }

  else

  {

   digitalWrite(LED,LOW);               //Sets pin 7 LOW

   Serial.println("Master LED OFF");

  }

  delay(1000);

}

------------------------------------
//Slave Arduino Code:

//SPI SLAVE (ARDUINO)

//SPI COMMUNICATION BETWEEN TWO ARDUINO 

//CIRCUIT DIGEST

//Pramoth.T


#include<SPI.h>

#define LEDpin 7

#define buttonpin 2

volatile boolean received;

volatile byte Slavereceived,Slavesend;

int buttonvalue;

int x;

void setup()


{

  Serial.begin(115200);

  

  pinMode(buttonpin,INPUT);               // Setting pin 2 as INPUT

  pinMode(LEDpin,OUTPUT);                 // Setting pin 7 as OUTPUT

  pinMode(MISO,OUTPUT);                   //Sets MISO as OUTPUT (Have to Send data to Master IN 


  SPCR |= _BV(SPE);                       //Turn on SPI in Slave Mode

  received = false;


  SPI.attachInterrupt();                  //Interuupt ON is set for SPI commnucation

  

}


ISR (SPI_STC_vect)                        //Inerrrput routine function 

{

  Slavereceived = SPDR;         // Value received from master if store in variable slavereceived

  received = true;                        //Sets received as True 

}


void loop()

{ if(received)                            //Logic to SET LED ON OR OFF depending upon the value recerived from master

   {

      if (Slavereceived==1) 

      {

        digitalWrite(LEDpin,HIGH);         //Sets pin 7 as HIGH LED ON

        Serial.println("Slave LED ON");

      }else

      {

        digitalWrite(LEDpin,LOW);          //Sets pin 7 as LOW LED OFF

        Serial.println("Slave LED OFF");

      }

      

      buttonvalue = digitalRead(buttonpin);  // Reads the status of the pin 2

      

      if (buttonvalue == HIGH)               //Logic to set the value of x to send to master

      {

        x=1;

        

      }else

      {

        x=0;

      }

      

  Slavesend=x;                             

  SPDR = Slavesend;                           //Sends the x value to master via SPDR 

  delay(1000);

}

}

Conclusion

In this article we saw basics of SPI Communication with different operating modes

sachin Pagar

I am Mr. Sachin pagar Embedded software engineer, the founder of Sach Educational Support(Pythonslearning), a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

Leave a Reply