SPI Serial Peripheral Interface: The AVR Microcontroller and Embedded Systems
SPI Serial Peripheral Interface: The AVR Microcontroller and Embedded Systems
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
SPI …
◼ Started by Motorola Corp. (now Freescale)
◼ Uses 2 pins for data (SDI and SDO ) one for clock (SCLK) and chip
enable (CE)
◼ The pins are alternatively named MISO (Master In Slave out) , MOSI
(Master Out Slave In) , SS (Slave Select) and SCK
◼ Read and Write happens at the same time
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
SPI …
Master Slave
MISO MISO
MOSI MOSI
SCK SCK
OUT1 SS
OUT2
Slave
MISO
MOSI
SCK
SS
AVR Microcont roller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
SPI pins in Atmega32
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
SPI BUS PROTOCOL
➢ SPI was developed to provide a low-cost and simple interface between
microcontrollers and peripheral chips.
➢ SPI devices use only 2 pins for data transfer, SDI (Din) & SDO (Dout), instead of 8
or more pins used in traditional buses.
➢ This reduces package size and power consumption.
➢ SPI bus has the SCLK (clock) pin to synchronize the data transfer between two
chips, CE (chip enable) used to initiate and terminate the data transfer.
➢ Four pins, SDI, SDO, SCLK, and CE, make the SPI a 4-wire interface.
➢ SDI, SDO, SCLK, and CE signals are alternatively named as MOSI, MISO, SCK, and
SS.
➢ There is also a standard called 3-wire interface bus where SCLK, CE, and only a
single pin for data transfer.
➢ SPI 4-wire bus can become 3-wire interface when the SDI and SDO data pins are
tied together.
➢ There are some major differences between the SPI and 3-wire devices in the data
transfer protocol. For that reason, a device must support the 3-wire protocol
internally in order to be used as a 3-wire device.
SPI Bus vs. Traditional Parallel Bus Connection
SPI Interface
• Also known as a four-wire interface.
• Used to interface to memory (for data storage), ADC, DAC, real-time
clock calendars, LCD drivers, sensors, audio chips, and even other
processors.
• Unlike a standard serial port, SPI is a synchronous protocol in which all
transmissions are referenced to a common clock, generated by the
master (processor).
• The receiving peripheral (slave) uses the clock to synchronize its
acquisition of the serial bit stream.
Basic SPI Interface
• SPI uses four main signals:
• Master Out Slave In (MOSI)
• Master In Slave Out (MISO)
• Serial CLocK (SCLK or SCK)
• Chip Select (CS)
SPI Transmission
• Both masters and slaves contain a serial shift register.
• The master starts a transfer of a byte by writing it to its SPI shift
register.
• As the register transmits the byte to the slave on the MOSI signal
line, the slave transfers the contents of its shift register back to the
master on the MISO signal line.
• In this way, the contents of the two shift registers are exchanged.
• Both a write and a read operation are performed with the slave
simultaneously.
SPSR (SPI Status Register)
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
SPCR (SPI Control Register)
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
SPI Timing
➢ SPI has four modes of operation, depending on clock
polarity and clock phase.
➢ For low (0) clock polarity, the clock (SCK) is low when
idle and toggles high during a transfer and vice versa.
➢ The two clock phases are known as clock phase zero
and clock phase one.
➢ For clock phase zero, MOSI and MISO outputs are valid
on the rising edge of the clock (SCK) if the clock polarity
is low and vice versa.
SPCR (SPI Control Register)
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Slave Operating Mode
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
SPI Application: SPI-Based Clock/Calendar
What is Calendar Integration?
Allows two different applications to sync together and share data from your
calendar, avoiding duplicate calendar entries or overlapping appointments.
Calendar integration is a two-way process; when you book appointments in
either application, that data is automatically synced so that both applications
stay current in real time.
Every electronic device is now integrated with the electronic calendar to
enable the user to keep time and date anywhere at any time.
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
MAX 7921 (SPI 7-Seg Driver)
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
MAX 7921 Connections to AVR
◼ Up to 8 7-Segs can be connected to MAX7921
◼ The ISET resistor control the intensity of light
◼ The VCC and GND must be able to handle up to
300 mA
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.