College of Electronics Technology Bani Walid: Presented By: Mohamed Awad Mohamed Abolobaidh

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

COLLEGE OF ELECTRONICS TECHNOLOGY

BANI WALID

Presented By:
Mohamed awad
Mohamed abolobaidh
Serial Peripheral Interface
(SPI)

Serial Peripheral Interface (SPI)


Basics Revisited
Serial Peripheral Interfacing is one of the most used serial
communication protocols, and very simple to use! As a matter
of fact, I find this one much simpler than USART! ;)
Since SPI has been accepted as a de facto standard , it is
available in almost all architectures
Wired transmission of data (though the first preference is
mostly USART, but SPI can be used when we are using multiple
slave or master systems, as addressing is much simpler in SPI)

The idea of the SPI


The idea GENERAL SPI is to connect 2 Microcontroller other and
transfer data between them and works the idea of Master and
slave is that the data is transferred from the Microcontroller 1
to other 2 and impose it be microcontroller The first is Master
and microcontroller the second is slave shall be transport the
first terms that would be to transfer data from Microcontroller
first to Microcontroller second and contrary but be Master
conditions

Advantages
1_Full duplex communication
2_Less power consumption as compared to I2C
3_Higher hit rates (or throughput)
But there are some disadvantages as well, like higher number of
wires in the bus, needs more pins on the microcontroller, etc.

SPI four pins used to connect to aMISO MOSI SCK SSand these
pins be present in the PORTB

1_MISO MISO stands for Master In Slave Out. MISO is the


input pin for Master AVR, and output pin for Slave AVR device.
Data transfer from Slave to Master takes place through this
channel.
2_SCK This is the SPI clock line (since SPI is a synchronous
communication)

3_SS This stands for Slave Select. This pin would be discussed
in detail later in the post

Master and Slave


In SPI, every device connected is either a Master or a Slave.
The Master device is the one which initiates the connection and
controls it. Once the connection is initiated, then the Master and
one or more Slave(s) can transmit and/or receive data. As
mentioned earlier, this is a full-duplex connection, which means
that Master can send data to Slave(s) and the Slave(s) can also
send the data to the Master at the same time.
As I said earlier, SPI uses 4 pins for data communication. So
lets move on to the pin description.
Register Descriptions
The AVR contains the following three registers that deal with
SPI
1_ SPCR SPI Control Register This register is basically the
master register i.e. it contains the bits to initialize SPI and
control it
2_SPSR SPI Status Register This is the status register. This
register is used to read the status of the bus lines
3_SPDR SPI Data Register The SPI Data Register is the
read/write register where the actual data transfer takes place

The SPI Control Register (SPCR)


As is obvious from the name, this register controls the SPI. We
will find the bits that enable SPI, set up clock speed, configure
master/slave, etc. Following are the bits in the SPCR Register

SPIE SPI Interrupt Enable


Setting this bit to one enables the SPI interrupt.
SPE
SPI Enable Setting this bit to one enables the SPI.
MSTR: Master/Slave Select
If you Want to work in master mode then set this bit to one;
otherwise, slave mode is selected. Notice that if the SS pin is
congured as an input and is driven low while MSTR is set,
MSTR will be cleared, and SPIF will become set.
CPOL: Clock Polarity
This bit set the base value of clock when it is idle. At CPOL = 0
the base value of the clock is zero while at CPOL = 1 the base
value of the clock is one.
CPHA: Clock Phase
CPHA = 0 means sample on the leading (first) clock edge, while
CPHA = l means sample on the trailing (second) clock.

SPRI, SPRO: SPI Clock Rate Select 1 and 0


These two bits control the SCK rate of the device in master
mode..
The SPI Status Register (SPSR)
The SPI Status Register is the register from where we can get
the status of the SPI bus and interrupt flag is also set in this
register. Following are the bits in the SPSR register

SPIF
In master mode, this bit is set in two situations: when a serial
transfer is completed, or when SS pin is an input and is
driven low by an external device. Setting the SPIF ag to one
will cause an interrupt if SPIE in SPCR is set and global
interrupts are enabled.
WCOL
The WCOL bit is set if you write on SPDR during a data transfer.
SPI2X
When the SPI is in master mode, setting this bit to one doubles
the SPI speed.
The SPI Data Register (SPDR)

The SPI Data Register is a read/write register. To Write into SPI


shift reg- ister, data must be written to SPDR. To read from the
SPI shift register, you should read from SPDR. Writing to the
SPDR register initiates data transmission. Notice that you
cannot write to SPDR before the last byte is transmitted
completely, otherwise a collision will happen. You can read the
received data before another byte of data is received
.completely
The Slave Select (SS) Pin
when we are communicating between multiple devices working
on SPI through the same bus, the SS pin is used to select the
slave to which we want to communicate with.
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, the master microcontroller would give a low
signal to the SS pin of that specific microcontroller, and hence
only that slave microcontroller would receive data.
.When there are multiple masters and a single slave
A similar setup as above can be used in this case as well, the
difference being that the SS lines of all the masters is controlled
by the slave, while the slave SS line is always held low. The
slave would select the master through which it has to receive
data by pulling its SS high.Alternatively, a multiplexed system

can be used where each master microcontroller can control


every other master microcontrollers SS pin, and hence when it
has to transmit data, it would pull down every other master
microcontrollers SS Pin, while declaring its own SS as output

Code: Code exists in a separate file


-

REFRENCE
MazidiAVR

You might also like