0% found this document useful (0 votes)
14 views15 pages

Lab 8-MPMC

Uploaded by

sami khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views15 pages

Lab 8-MPMC

Uploaded by

sami khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

International Islamic University Islamabad

Faculty of Engineering and Technology


Department of Electrical and Computer Engineering

MICOPROCESSORS AND MICROCONTROLLER LAB

Experiment No. 8: SPI Protocol Programming with Arduino

Name of Student: ……………………………………

Registration No.: ……………………………………..

Date of Experiment: …………………………………

Submitted To: ………………………………………...

Experiment No. 8: SPI Protocol Programming with Arduino Page


1
Objectives:
• To set up and use the on-chip Serial Peripheral Interface (SPI) of the Arduino board.
Equipment Required:
• Breadboard
• Personal Computer (PC)
• Arduino Board with USB cable [Qty = 2]
• Connecting wires
Theory:
A Serial Peripheral Interface (SPI) bus is a system for serial communication, which
uses up to four conductors, commonly three. One conductor is used for data receiving,
one for data sending, one for synchronization and one alternatively for selecting a
device to communicate with. It is a full duplex connection, which means that the data is
sent and received simultaneously. The maximum baud rate is higher than that in the I2C
communication system.
The SPI allows high-speed synchronous data transfer between the AVR and
peripheral devices or between several AVR devices. On most parts the SPI has a
second purpose where it is used for In System Programming (ISP).

The interconnection between two SPI devices always happens between a master
device and a slave device. Compared to some peripheral devices like sensors, which
can only run in slave mode, the SPI of the AVR can be configured for both master and
slave mode. The mode the AVR is running in is specified by the settings of the master
bit (MSTR) in the SPI control register (SPCR). Special considerations about the SS pin
must be considered for Multi Slave Systems. The master is the active part in this system
and must provide the clock signal a serial data transmission is based on. The slave is
not capable of generating the clock signal and thus cannot get active on its own. The
slave just sends and receives data, if the master generates the necessary clock signal.
The master, however, generates the clock signal only while sending data. That means
the master must send data to the slave to read data from the slave.

Experiment No. 8: SPI Protocol Programming with Arduino Page


2
SPI uses the following four wires −
1. SCK − This is the serial clock driven by the master.
2. MOSI − This is the master output / slave input driven by the master.
3. MISO − This is the master input / slave output driven by the master.
4. SS − This is the slave-selection wire.
The following functions are used. You have to include the SPI.h.
i. SPI.begin() − Initializes the SPI bus by setting SCK, MOSI, and SS to outputs,
pulling SCK and MOSI low, and SS high.
ii. SPI.setClockDivider(divider) − To set the SPI clock divider relative to the system
clock. On AVR based boards, the dividers available are 2, 4, 8, 16, 32, 64 or 128.
The default setting is SPI_CLOCK_DIV4, which sets the SPI clock to one-quarter
of the frequency of the system clock (5 Mhz for the boards at 20 MHz).
iii. Divider − It could be (SPI_CLOCK_DIV2, SPI_CLOCK_DIV4, SPI_CLOCK_DIV8,
SPI_CLOCK_DIV16, SPI_CLOCK_DIV32, SPI_CLOCK_DIV64,
SPI_CLOCK_DIV128).
iv. SPI.transfer(val) − SPI transfer is based on a simultaneous send and receive: the
received data is returned in receivedVal.
v. SPI.beginTransaction(SPISettings(speedMaximum, dataOrder, dataMode)) −
speedMaximum is the clock, dataOrder(MSBFIRST or LSBFIRST),
dataMode(SPI_MODE0, SPI_MODE1, SPI_MODE2, or SPI_MODE3).
vi. SPI.attachInterrupt(handler) − Function to be called when a slave device
receives data from the master.
We have four modes of operation in SPI as follows –
a. Mode 0 (the default) − Clock is normally low (CPOL = 0), and the data is sampled
on the transition from low to high (leading edge) (CPHA = 0).
b. Mode 1 − Clock is normally low (CPOL = 0), and the data is sampled on the
transition from high to low (trailing edge) (CPHA = 1).
c. Mode 2 − Clock is normally high (CPOL = 1), and the data is sampled on the
transition from high to low (leading edge) (CPHA = 0).
d. Mode 3 − Clock is normally high (CPOL = 1), and the data is sampled on the
transition from low to high (trailing edge) (CPHA = 1).

CPOL CPHA Data Read and Change Time SPI Mode


0 0 Read on Rising Edge, Changed on a Falling 0
Edge
0 1 Read on Falling Edge, Changed on a Rising 1
Edge
1 0 Read on Falling Edge, Changed on a Rising 2
Edge
1 1 Read on Rising Edge, Changed on a Falling 3
Edge

Experiment No. 8: SPI Protocol Programming with Arduino Page


3
AVR Registers
• Control register:
SPCR (SPI Control Register)
• Status Register:
SPSR (SPI Status Register)
• Data Register:
SPDR (SPI Data Register)

SPSR Register:
SPSR: SPIF WCOL - - - - - SPI2X
◼ SPIF (SPI Interrupt Flag)
A serial transfer is completed.
The SS pin is driven low in slave mode
WCOL (Write Collision)
SPI2X (Double SPI Speed)

SPCR Register:
SPCR: SPIE SPE DORD MSTR CPOL CPHA SPR1 SPR0

◼ SPIE (SPI Interrupt Enable)


◼ SPE (SPI Enable)
◼ DORD (Data Order)
◼ MSTR (Master)
◼ CPOL (Clock Polarity)
◼ CPHA (Clock Phase)
◼ SPR1, SPR0 :SPI Clock Rate

SPI2X SPR1 SPR0 SCK Freq.


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

Experiment No. 8: SPI Protocol Programming with Arduino Page


4
Now, we will connect two Arduino UNO boards together; one as a master and the other
as a slave.
◼ (SS) : pin 10
◼ (MOSI) : pin 11
◼ (MISO) : pin 12
◼ (SCK) : pin 13
Programming for SPI Protocol:
Example 1:
Sketch for Master:

Experiment No. 8: SPI Protocol Programming with Arduino Page


5
Sketch for Slave :

Experiment No. 8: SPI Protocol Programming with Arduino Page


6
Experiment No. 8: SPI Protocol Programming with Arduino Page
7
Schematic Diagram:

Example 2:
When push button at Master side is pressed, white LED at slave side turns ON. And
when the push button at Slave side is pressed, Red LED at Master side turns ON.

Sketch for Master

Experiment No. 8: SPI Protocol Programming with Arduino Page


8
Sketch for Slave

Experiment No. 8: SPI Protocol Programming with Arduino Page


9
Experiment No. 8: SPI Protocol Programming with Arduino Page
10
Schematic Diagram

Experiment No. 8: SPI Protocol Programming with Arduino Page


11
Example 3:
Making a Slave Arduino work like a memory element (EEPROM)
Master will write data to the 1KB EEPROM using Read or Write opcode and
slave will save and return data accordingly.

Sketch for Master

Experiment No. 8: SPI Protocol Programming with Arduino Page


12
Sketch for Slave

Experiment No. 8: SPI Protocol Programming with Arduino Page


13
Schematic Diagram

Experiment No. 8: SPI Protocol Programming with Arduino Page


14
Lab Task:
Master unit sends hello world data to slave unit. Slave unit waits for data as soon as
data is arrived, process variable becomes true, indicating there is a data in the buffer. In
main loop, we read this buffer and send it to serial terminal.

Experiment No. 8: SPI Protocol Programming with Arduino Page


15

You might also like