SlideShare a Scribd company logo
SPI (Serial Peripheral Interface)
INTRODUCTION!
Serial communication is the best and efficient way of transmitting one byte of data between devices
wired or wirelessly. Many newbies may have this doubt about why learning so much about serial
communication if we can simply transfer one bit analog data from one device to the other by
connecting wires simply? You can do that but what if your other device is far away, here you would
require wireless communication. Now only serial data can be transmitted wirelessly, moreover you
can send 8-bits of analog data serially. Thus it’s more efficient & time saving.
Serial Peripheral Interface (SPI) is a type of serial communication between microcontrollers.
Serial communication is also possible via USART Module for communication b/w a microcontroller
and PC. Another alternative of SPI is TWI (I2C) but SPI is faster, reliable and less power consuming.
SPI also has an advantage when there are multiple masters and multiple slaves all interconnected,
SPI communication is the only way to communicate selectively between particular masters and
particular slaves.
What is SPI?
The Serial Peripheral Interface (SPI) is a high-speed synchronous data transfer between a
microcontroller and peripheral devices or between several AVR devices. The ATmega16 SPI
includes Full-duplex (incoming & outgoing data exchange both at the same time), Three-wire
Synchronous Data Transfer through MOSI, MISO & SCK pins. It has Seven Programmable Bit Rates
for data transfer. It is used for Master to Slave Data transfer through these three pins.
How does SPI work?
Through SPI Data is always shifted from Master to Slave on the Master Out – Slave In, MOSI, line,
and from Slave to Master on the Master In – Slave Out, MISO, line.
The SPI is synchronous data transfer protocol, so clock pulse is needed to synchronize both master
and slave device. The clock pulse is generated from master side. The SCK pin of master provides
clock pulse to slave device.
The SS pin is also included in SPI system. To make any device as master, the SS pin must be set as
high. If it is configured as an output pin, then it made high using the software. If the SS is considered
as input pin, it should make high externally. In slave mode SS is always an input pin, which should be
connected to ground (to make it slave device).
The system consists of two Shift Registers, and a Master clock generator. The SPI Master initiates
the communication cycle when pulling low the Slave Select SS pin of the desired Slave. Master and
Slave prepare the data to be sent in their respective Shift Registers, and the Master generates the
required clock pulses on the SCK line to interchange data. After each data packet being transferred,
the Master will synchronize the Slave by pulling high the Slave Select, SS, line.
Following table explains the functionality of the pins involved in SPI interface:
MOSI (Master Output Slave
Input)
An output data transfer pin for a Master device while is an input
pin for a Slave device
MISO (Master Input Slave
Output)
An output data transfer pin for a Slave device while is an input pin
for a Master device
SCK (Slave Clock) On this pin Master generates the required clock pulses to
interchange data
SS’ (Slave Select) When set as input, the SS’ pin should be given as HIGH (Vcc) on
as Master device, and a LOW (Grounded) on a Slave device
Registers of SPI System:
The SPI system consists of three registers which are described below:
 SPI Control Register – SPCR
Bit: 7 6 5 4 3 2 1 0
SPIE SPE DORD MSTR CPOL CPHA SPR1 SPR0
o SPIE (SPI Interrupt Enable): This bit if set causes the SPI interrupt to be executed if SPIF bit
in the SPSR Register is set and if the global interrupt enable bit in SREG is set.
o SPE (SPI Enable): When the SPE bit is written to one, the SPI system is enabled. This bit
must be set to enable any SPI operations.
o DORD (Data Order): For DORD=1, LSB will be transmitted first.
For DORD=0, MSB will be transmitted first.
{Hardly matters, whether you transmit MSB/LSB first because finally you would get the same result.}
o MSTR (Master/Slave Select): For MSTR=1, to select device as master.
For MSTR=0, to select device as slave.
o The CPHA and CPOL control bits collectively determine SCK phase and polarity with respect
to serial data.
o SPR [1:0] (SPI clock Rate): This SPR [1:0] and SPI2X bit of SPSR register decides frequency
of SCK. The combinations of these three bits to select SCK frequency are shown in following
table:
Relationship between SCK and the Oscillator Frequency
SPI2X SPR1 SPR0 SCK Frequency
0 0 0 fosc/4
0 0 1 fosc/16
0 1 0 fosc/64
0 1 1 fosc/128
1 0 0 fosc/2
1 0 1 fosc/8
1 1 0 fosc/32
1 1 1 fosc/64
When the SPI is configured as Slave, the SPI is only guaranteed to work at fosc/4 or lower.
 SPI Status Register – SPSR
Bit: 7 6 5 4 3 2 1 0
SPIF WCOL - - - - - SPI2X
o SPIF (SPI Interrupt Flag): This bit becomes set automatically after completion of serial data
transfer. The SPIF bit is cleared by first reading the SPI Status Register with SPIF set, then
accessing the SPI Data Register (SPDR).
o WCOL (Write Collision Flag): This bit is set if SPDR is written during data transfer.
o SPI2X (Double SPI Bit): By setting 1 to this bit SCK frequency becomes double.
 SPI Data Register – SPDR:
Bit: 7 6 5 4 3 2 1 0
MSB LSB
The SPI Data Register is an 8-bit read/write buffer register used for data transfer between the
Register File and the SPI Shift Register. Writing to the register initiates data transmission.
Reading the register causes the Shift Register Receive buffer to be read.
Master transfers one bit from its SPDR to slave device in every clock cycle. It means to send
one byte data (8-bits), 8 clock pulses are needed. Here is a pictorial representation of how
a single bit is transmitted in MOSI & MISO lines.
SPI Master-Slave Interconnection for Data Transfer
For DORD = 0
The Slave Select (SS’) Pin
SS’ (means SS complemented) works in active low configuration, which means to use its
functionality it must be set as an input pin.
When set as input, the SS’ pin should be given a HIGH (Vcc) signal on a Master device, and a
LOW (Grounded) on a Slave device.
When as an output pin on the Master microcontroller, the SS’ pin can be used as a GPIO
(General Input-Output) pin.
The importance of this pin comes into picture when we are communicating between multiple
(master/slave) devices on SPI through the same bus; this SS’ pin is used to select the slave or the
master b/w which we want communication to take place.
Let us consider the following two cases to understand this well:
1. When there are multiple slaves and a single master.
In this case, the SS’ pins of all the slaves are connected to the master microcontroller. Since
we want only a specific slave to receive the data, a low signal must be given to the SS’ pin of
that specific slave microcontroller, and hence only that slave device would receive data in the
stage.
2. When there are multiple masters and a single slave.
In this case, the SS’ pins of all the masters are connected to a slave microcontroller. Since we
want only a specific master to transmit the data, a high signal must be given to the SS’ pin of
that specific master microcontroller, and hence only that master device would transmit data to
the slave device.
Now the above cases can be handled with two SPI slave configurations, namely The Daisy
chained multi slave system and The Parallel multi slave system. To understand this watch –
https://www.youtube.com/watch?v=oWLriIreYRU
What are the Applications of SPI?
SPI interface is chiefly used for In-system programming (ISP) of the AVR microcontrollers.
On the other hand, this “3-wire interface” protocol is also widely being used to communicate with a
wide variety of peripherals like sensors, memory devices, real time clocks, communication protocols
like Ethernet. SPI can be used when we are using multiple slave or master systems, as addressing is
much simpler in SPI. The full-duplex capability makes SPI very simple and efficient for single
master/single slave applications. For Multiple Slave devices or Multiple Master devices appropriate
switching of SS’ pin is required to communicate b/w a particular Slave or a particular Master device.
Some devices use the full-duplex mode to implement an efficient, swift data stream for applications
such as digital audio, digital signal processing, or telecommunications channels. It also consumes
less power as compared to I2C. In today’s technologies this interface is being increasingly used for
artificial intelligence in Swarm Robotics! This Master to Slave operation between robots is also used
for making mutual decisions. Many Autonomous Robotics competitions like Robo-soccer require
similar Master-Slave operation.
SPI in USE:-
Objective: To interface between two ATmega16 microcontrollers using SPI protocol. Consider one
AVR device as master and the other as slave. The master would control the led switching operation
on the slave device via its push buttons.
Circuit Diagram:
AVR code for Master:
#include <ioavr.h>
#include <intrinsics.h>
#define SCK PB7
#define MOSI PB5
#define SW1 PINC5 //All Switches are connected to PORTC
#define SW2 PINC4
#define SW3 PINC3
#define CHECKBIT(ADDRESS, BIT) (ADDRESS & (1 << BIT))
void SPI_MasterInit(void)
{
/* Set MOSI and SCK output, all others input */
DDRB |= (1<<MOSI)|(1<<SCK);
/* Enable SPI, Master, set clock rate fck/16 */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
void SPI_MasterTransmit(unsigned char cData)
{
/* Start transmission */
SPDR = cData;
/* Wait for transmission complete */
while(!(SPSR) & (1<<SPIF));
}
void main(void)
{
DDRC = 0x00; // defining PORTC Switches as input
int c[3]={1,1,1};
PORTC |= (1<<PC3)|(1<<PC4)|(1<<PC5); //Enable Pull-ups on PORTC Switches
SPI_MasterInit();
while(1)
{
if (!(CHECKBIT(PINC, SW1)))
{
c[0]++;
if(c[0]%2==0) // For Altrnate switching
SPI_MasterTransmit('1');
else
SPI_MasterTransmit('4');
__delay_cycles(16000000);
}
if (!(CHECKBIT(PINC, SW2)))
{
c[1]++;
if(c[1]%2==0)
SPI_MasterTransmit('2');
else
SPI_MasterTransmit('5');
__delay_cycles(16000000);
}
if (!(CHECKBIT(PINC, SW3)))
{
c[2]++;
if(c[2]%2==0)
SPI_MasterTransmit('3');
else
SPI_MasterTransmit('6');
__delay_cycles(16000000);
}
}
}
AVR code for Slave:
#include <ioavr.h>
#include <intrinsics.h>
#define MISO PB6
#define LED1 PORTC2 //Connected to PORTC
#define LED2 PORTC1 //Connected to PORTC
#define LED3 PORTC0 //Connected to PORTC
#define SETBIT(ADDRESS, BIT) (ADDRESS |= (1 << BIT))
#define CLEARBIT(ADDRESS, BIT) (ADDRESS &= ~(1 << BIT))
void SPI_SlaveInit(void)
{
/* Set MISO output, all others input */
DDRB = (1<<MISO);
/* Enable SPI */
SPCR = (1<<SPE);
}
unsigned char SPI_SlaveReceive(void)
{
// Wait for reception to complete
while(!(SPSR) & (1<<SPIF));
/* Return data register */
return SPDR;
}
void main (void)
{
DDRC = 0X07;
unsigned char a;
SPI_SlaveInit();
while(1)
{
a = SPI_SlaveReceive();
switch(a)
{
case '1':
SETBIT(PORTC, LED1);
break;
case '2':
SETBIT(PORTC, LED2);
break;
case '3':
SETBIT(PORTC, LED3);
break;
case '4':
CLEARBIT(PORTC, LED1);
break;
case '5':
CLEARBIT(PORTC, LED2);
break;
case '6':
CLEARBIT(PORTC, LED3);
break;
default:
break;
}
}
}
Video tutorials:
For Illustration of the above Example
https://www.youtube.com/watch?v=1TlYnlDd1qQ
Contributed By:
Chirag Parikh
Member of Robolution Club

More Related Content

PDF
SPI Protocol
PPT
SPI Bus Protocol
PPTX
SPI introduction(Serial Peripheral Interface)
PPT
Serial Peripheral Interface(SPI)
PDF
I2C Bus (Inter-Integrated Circuit)
PPTX
Serial Peripheral Interface
PDF
I2C BUS PROTOCOL
PPT
Spi master core verification
SPI Protocol
SPI Bus Protocol
SPI introduction(Serial Peripheral Interface)
Serial Peripheral Interface(SPI)
I2C Bus (Inter-Integrated Circuit)
Serial Peripheral Interface
I2C BUS PROTOCOL
Spi master core verification

What's hot (20)

PPTX
AMBA AHB 5
PPTX
Serial peripheral Interface - Embedded System Protocol
PPTX
I2C introduction
PPTX
PDF
Pcie basic
ODP
PPTX
I2c protocol - Inter–Integrated Circuit Communication Protocol
PPT
AMBA_APB_pst
PPTX
I2C Protocol
PPTX
Introduction about APB Protocol
PPTX
I2 c protocol
PPTX
PPTX
Slideshare - PCIe
PDF
SPI Protocol in LPC2148
PDF
Session 8,9 PCI Express
ODP
APB protocol v1.0
PPTX
AMBA Ahb 2.0
PPTX
I2C Protocol
ODP
Pc ie tl_layer (3)
PPTX
AMBA 3 APB Protocol
AMBA AHB 5
Serial peripheral Interface - Embedded System Protocol
I2C introduction
Pcie basic
I2c protocol - Inter–Integrated Circuit Communication Protocol
AMBA_APB_pst
I2C Protocol
Introduction about APB Protocol
I2 c protocol
Slideshare - PCIe
SPI Protocol in LPC2148
Session 8,9 PCI Express
APB protocol v1.0
AMBA Ahb 2.0
I2C Protocol
Pc ie tl_layer (3)
AMBA 3 APB Protocol
Ad

Viewers also liked (10)

PPT
I2C And SPI Part-23
PPTX
I2C BUS
PPT
I2C Protocol
PDF
The I2C Interface
PPTX
Serial peripheral interface
PPTX
Spi in arm7(lpc2148)
PPTX
Communication protocols
PDF
Communication protocols - Embedded Systems
PDF
Communication Protocols (UART, SPI,I2C)
PPT
I2C And SPI Part-23
I2C BUS
I2C Protocol
The I2C Interface
Serial peripheral interface
Spi in arm7(lpc2148)
Communication protocols
Communication protocols - Embedded Systems
Communication Protocols (UART, SPI,I2C)
Ad

Similar to Serial Peripheral Interface (20)

PDF
Welcome to International Journal of Engineering Research and Development (IJERD)
PDF
communication interfaces-Embedded real time systems
PDF
Serial Peripheral Interface Design for Advanced Microcontroller Bus Architect...
PPTX
embedded system on board communication.pptx
PPTX
embedded system on board communication.pptx
PPTX
embedded system on board communication.pptx
PPTX
UNI T 6- SPI_I2C_Lecture8.pptx
PDF
Cd36479483
PPTX
spi-180501092933-converted.pptx
PPT
Introduction to embedded systems
PPTX
Part-2: Mastering microcontroller with embedded driver development
PDF
SPI Design Report used in serial communication
PDF
AREA OPTIMIZATION OF SPI MODULE USING VERILOG HDL
PPT
Spi (1)
PDF
Assembler4
PPT
I2C PRESENTATION.PPT
DOCX
D-7-17 Interface an 8-bit serial device using SPI- Thecontrol pin is i.docx
PPT
Lesson 7+8.pptGroup5_TA2_.pptxGroup5_TA2_.pptx
PPT
10_2Starting with serial_II.ppt
PPTX
Serial Peripheral Interface (SPI).pptx
Welcome to International Journal of Engineering Research and Development (IJERD)
communication interfaces-Embedded real time systems
Serial Peripheral Interface Design for Advanced Microcontroller Bus Architect...
embedded system on board communication.pptx
embedded system on board communication.pptx
embedded system on board communication.pptx
UNI T 6- SPI_I2C_Lecture8.pptx
Cd36479483
spi-180501092933-converted.pptx
Introduction to embedded systems
Part-2: Mastering microcontroller with embedded driver development
SPI Design Report used in serial communication
AREA OPTIMIZATION OF SPI MODULE USING VERILOG HDL
Spi (1)
Assembler4
I2C PRESENTATION.PPT
D-7-17 Interface an 8-bit serial device using SPI- Thecontrol pin is i.docx
Lesson 7+8.pptGroup5_TA2_.pptxGroup5_TA2_.pptx
10_2Starting with serial_II.ppt
Serial Peripheral Interface (SPI).pptx

Recently uploaded (20)

PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
PPT on Performance Review to get promotions
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPT
Project quality management in manufacturing
PDF
Well-logging-methods_new................
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
web development for engineering and engineering
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPT
Mechanical Engineering MATERIALS Selection
DOCX
573137875-Attendance-Management-System-original
CYBER-CRIMES AND SECURITY A guide to understanding
CH1 Production IntroductoryConcepts.pptx
PPT on Performance Review to get promotions
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Lecture Notes Electrical Wiring System Components
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Project quality management in manufacturing
Well-logging-methods_new................
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Strings in CPP - Strings in C++ are sequences of characters used to store and...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Internet of Things (IOT) - A guide to understanding
web development for engineering and engineering
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Mechanical Engineering MATERIALS Selection
573137875-Attendance-Management-System-original

Serial Peripheral Interface

  • 1. SPI (Serial Peripheral Interface) INTRODUCTION! Serial communication is the best and efficient way of transmitting one byte of data between devices wired or wirelessly. Many newbies may have this doubt about why learning so much about serial communication if we can simply transfer one bit analog data from one device to the other by connecting wires simply? You can do that but what if your other device is far away, here you would require wireless communication. Now only serial data can be transmitted wirelessly, moreover you can send 8-bits of analog data serially. Thus it’s more efficient & time saving. Serial Peripheral Interface (SPI) is a type of serial communication between microcontrollers. Serial communication is also possible via USART Module for communication b/w a microcontroller and PC. Another alternative of SPI is TWI (I2C) but SPI is faster, reliable and less power consuming. SPI also has an advantage when there are multiple masters and multiple slaves all interconnected, SPI communication is the only way to communicate selectively between particular masters and particular slaves. What is SPI? The Serial Peripheral Interface (SPI) is a high-speed synchronous data transfer between a microcontroller and peripheral devices or between several AVR devices. The ATmega16 SPI includes Full-duplex (incoming & outgoing data exchange both at the same time), Three-wire Synchronous Data Transfer through MOSI, MISO & SCK pins. It has Seven Programmable Bit Rates for data transfer. It is used for Master to Slave Data transfer through these three pins. How does SPI work? Through SPI Data is always shifted from Master to Slave on the Master Out – Slave In, MOSI, line, and from Slave to Master on the Master In – Slave Out, MISO, line. The SPI is synchronous data transfer protocol, so clock pulse is needed to synchronize both master and slave device. The clock pulse is generated from master side. The SCK pin of master provides clock pulse to slave device. The SS pin is also included in SPI system. To make any device as master, the SS pin must be set as high. If it is configured as an output pin, then it made high using the software. If the SS is considered as input pin, it should make high externally. In slave mode SS is always an input pin, which should be connected to ground (to make it slave device). The system consists of two Shift Registers, and a Master clock generator. The SPI Master initiates the communication cycle when pulling low the Slave Select SS pin of the desired Slave. Master and Slave prepare the data to be sent in their respective Shift Registers, and the Master generates the required clock pulses on the SCK line to interchange data. After each data packet being transferred, the Master will synchronize the Slave by pulling high the Slave Select, SS, line. Following table explains the functionality of the pins involved in SPI interface: MOSI (Master Output Slave Input) An output data transfer pin for a Master device while is an input pin for a Slave device MISO (Master Input Slave Output) An output data transfer pin for a Slave device while is an input pin for a Master device SCK (Slave Clock) On this pin Master generates the required clock pulses to interchange data SS’ (Slave Select) When set as input, the SS’ pin should be given as HIGH (Vcc) on as Master device, and a LOW (Grounded) on a Slave device
  • 2. Registers of SPI System: The SPI system consists of three registers which are described below:  SPI Control Register – SPCR Bit: 7 6 5 4 3 2 1 0 SPIE SPE DORD MSTR CPOL CPHA SPR1 SPR0 o SPIE (SPI Interrupt Enable): This bit if set causes the SPI interrupt to be executed if SPIF bit in the SPSR Register is set and if the global interrupt enable bit in SREG is set. o SPE (SPI Enable): When the SPE bit is written to one, the SPI system is enabled. This bit must be set to enable any SPI operations. o DORD (Data Order): For DORD=1, LSB will be transmitted first. For DORD=0, MSB will be transmitted first. {Hardly matters, whether you transmit MSB/LSB first because finally you would get the same result.} o MSTR (Master/Slave Select): For MSTR=1, to select device as master. For MSTR=0, to select device as slave. o The CPHA and CPOL control bits collectively determine SCK phase and polarity with respect to serial data. o SPR [1:0] (SPI clock Rate): This SPR [1:0] and SPI2X bit of SPSR register decides frequency of SCK. The combinations of these three bits to select SCK frequency are shown in following table: Relationship between SCK and the Oscillator Frequency SPI2X SPR1 SPR0 SCK Frequency 0 0 0 fosc/4 0 0 1 fosc/16 0 1 0 fosc/64 0 1 1 fosc/128 1 0 0 fosc/2 1 0 1 fosc/8 1 1 0 fosc/32 1 1 1 fosc/64 When the SPI is configured as Slave, the SPI is only guaranteed to work at fosc/4 or lower.  SPI Status Register – SPSR Bit: 7 6 5 4 3 2 1 0 SPIF WCOL - - - - - SPI2X o SPIF (SPI Interrupt Flag): This bit becomes set automatically after completion of serial data transfer. The SPIF bit is cleared by first reading the SPI Status Register with SPIF set, then accessing the SPI Data Register (SPDR). o WCOL (Write Collision Flag): This bit is set if SPDR is written during data transfer. o SPI2X (Double SPI Bit): By setting 1 to this bit SCK frequency becomes double.  SPI Data Register – SPDR: Bit: 7 6 5 4 3 2 1 0 MSB LSB The SPI Data Register is an 8-bit read/write buffer register used for data transfer between the Register File and the SPI Shift Register. Writing to the register initiates data transmission. Reading the register causes the Shift Register Receive buffer to be read. Master transfers one bit from its SPDR to slave device in every clock cycle. It means to send one byte data (8-bits), 8 clock pulses are needed. Here is a pictorial representation of how a single bit is transmitted in MOSI & MISO lines.
  • 3. SPI Master-Slave Interconnection for Data Transfer For DORD = 0 The Slave Select (SS’) Pin SS’ (means SS complemented) works in active low configuration, which means to use its functionality it must be set as an input pin. When set as input, the SS’ pin should be given a HIGH (Vcc) signal on a Master device, and a LOW (Grounded) on a Slave device. When as an output pin on the Master microcontroller, the SS’ pin can be used as a GPIO (General Input-Output) pin. The importance of this pin comes into picture when we are communicating between multiple (master/slave) devices on SPI through the same bus; this SS’ pin is used to select the slave or the master b/w which we want communication to take place. Let us consider the following two cases to understand this well: 1. When there are multiple slaves and a single master. In this case, the SS’ pins of all the slaves are connected to the master microcontroller. Since we want only a specific slave to receive the data, a low signal must be given to the SS’ pin of that specific slave microcontroller, and hence only that slave device would receive data in the stage. 2. When there are multiple masters and a single slave. In this case, the SS’ pins of all the masters are connected to a slave microcontroller. Since we want only a specific master to transmit the data, a high signal must be given to the SS’ pin of that specific master microcontroller, and hence only that master device would transmit data to the slave device. Now the above cases can be handled with two SPI slave configurations, namely The Daisy chained multi slave system and The Parallel multi slave system. To understand this watch – https://www.youtube.com/watch?v=oWLriIreYRU What are the Applications of SPI? SPI interface is chiefly used for In-system programming (ISP) of the AVR microcontrollers. On the other hand, this “3-wire interface” protocol is also widely being used to communicate with a wide variety of peripherals like sensors, memory devices, real time clocks, communication protocols like Ethernet. SPI can be used when we are using multiple slave or master systems, as addressing is much simpler in SPI. The full-duplex capability makes SPI very simple and efficient for single master/single slave applications. For Multiple Slave devices or Multiple Master devices appropriate switching of SS’ pin is required to communicate b/w a particular Slave or a particular Master device. Some devices use the full-duplex mode to implement an efficient, swift data stream for applications such as digital audio, digital signal processing, or telecommunications channels. It also consumes less power as compared to I2C. In today’s technologies this interface is being increasingly used for artificial intelligence in Swarm Robotics! This Master to Slave operation between robots is also used for making mutual decisions. Many Autonomous Robotics competitions like Robo-soccer require similar Master-Slave operation.
  • 4. SPI in USE:- Objective: To interface between two ATmega16 microcontrollers using SPI protocol. Consider one AVR device as master and the other as slave. The master would control the led switching operation on the slave device via its push buttons. Circuit Diagram: AVR code for Master: #include <ioavr.h> #include <intrinsics.h> #define SCK PB7 #define MOSI PB5 #define SW1 PINC5 //All Switches are connected to PORTC #define SW2 PINC4 #define SW3 PINC3 #define CHECKBIT(ADDRESS, BIT) (ADDRESS & (1 << BIT)) void SPI_MasterInit(void) { /* Set MOSI and SCK output, all others input */ DDRB |= (1<<MOSI)|(1<<SCK); /* Enable SPI, Master, set clock rate fck/16 */ SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0); }
  • 5. void SPI_MasterTransmit(unsigned char cData) { /* Start transmission */ SPDR = cData; /* Wait for transmission complete */ while(!(SPSR) & (1<<SPIF)); } void main(void) { DDRC = 0x00; // defining PORTC Switches as input int c[3]={1,1,1}; PORTC |= (1<<PC3)|(1<<PC4)|(1<<PC5); //Enable Pull-ups on PORTC Switches SPI_MasterInit(); while(1) { if (!(CHECKBIT(PINC, SW1))) { c[0]++; if(c[0]%2==0) // For Altrnate switching SPI_MasterTransmit('1'); else SPI_MasterTransmit('4'); __delay_cycles(16000000); } if (!(CHECKBIT(PINC, SW2))) { c[1]++; if(c[1]%2==0) SPI_MasterTransmit('2'); else SPI_MasterTransmit('5'); __delay_cycles(16000000); } if (!(CHECKBIT(PINC, SW3))) { c[2]++; if(c[2]%2==0) SPI_MasterTransmit('3'); else SPI_MasterTransmit('6'); __delay_cycles(16000000); } } } AVR code for Slave: #include <ioavr.h> #include <intrinsics.h> #define MISO PB6 #define LED1 PORTC2 //Connected to PORTC #define LED2 PORTC1 //Connected to PORTC #define LED3 PORTC0 //Connected to PORTC #define SETBIT(ADDRESS, BIT) (ADDRESS |= (1 << BIT)) #define CLEARBIT(ADDRESS, BIT) (ADDRESS &= ~(1 << BIT)) void SPI_SlaveInit(void) { /* Set MISO output, all others input */
  • 6. DDRB = (1<<MISO); /* Enable SPI */ SPCR = (1<<SPE); } unsigned char SPI_SlaveReceive(void) { // Wait for reception to complete while(!(SPSR) & (1<<SPIF)); /* Return data register */ return SPDR; } void main (void) { DDRC = 0X07; unsigned char a; SPI_SlaveInit(); while(1) { a = SPI_SlaveReceive(); switch(a) { case '1': SETBIT(PORTC, LED1); break; case '2': SETBIT(PORTC, LED2); break; case '3': SETBIT(PORTC, LED3); break; case '4': CLEARBIT(PORTC, LED1); break; case '5': CLEARBIT(PORTC, LED2); break; case '6': CLEARBIT(PORTC, LED3); break; default: break; } } } Video tutorials: For Illustration of the above Example https://www.youtube.com/watch?v=1TlYnlDd1qQ Contributed By: Chirag Parikh Member of Robolution Club