Using The SPI Module On 8-Bit PIC® Microcontrollers
Using The SPI Module On 8-Bit PIC® Microcontrollers
Using The SPI Module On 8-Bit PIC® Microcontrollers
Introduction
The Serial Peripheral Interface (SPI) module is a synchronous serial interface useful for communicating
with other peripheral or microcontroller devices. These peripheral devices can be serial EEPROMs, shift
registers, display drivers, Analog-to-Digital Converters and so on. The SPI module is compatible with
Motorola® SPI and Serial Input/Output Port (SIOP) interfaces.
The Serial Peripheral Interface (SPI) has long been part of the PIC® MCU set of core peripherals. One of
the newest features introduced with the PIC18F2x/4xK42 microcontroller series is a stand-alone module
for the SPI functionality. Previous devices have integrated SPI capabilities with the other serial
communications protocols and implemented them in the Master Synchronous Serial Port (MSSP) module,
where SPI shared resources such as registers and interrupt flags.
The dedicated module for SPI allows significant improvement in the implementation of traditional SPI
capabilities and expansions by adding new features for more flexibility and control. This technical brief
discusses the implementation, modes of operation and other additional features of the SPI module
Table of Contents
Introduction......................................................................................................................1
3. Transfer Counter......................................................................................................11
3.1. Total Bit Count Mode (BMODE = 0)........................................................................................... 11
3.2. Variable Transfer Size Mode (BMODE = 1)............................................................................... 11
4. Master Mode............................................................................................................12
4.1. Full-Duplex Mode....................................................................................................................... 13
4.2. Transmit-Only Mode................................................................................................................... 13
4.3. Receive-Only Mode....................................................................................................................13
4.4. Transfer-Off Mode...................................................................................................................... 14
4.5. Clear Buffer/FIFOS.....................................................................................................................14
6. Slave Mode..............................................................................................................17
7. Conclusion...............................................................................................................18
8. Revision A (4/2018)................................................................................................. 19
Customer Support......................................................................................................... 20
Legal Notice...................................................................................................................21
Trademarks................................................................................................................... 21
Name rotatethispage90 Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
© 2018 Microchip Technology Inc.
TB3192
Table 2-2. Registers Associated with IO Control
Name rotatethispage90 Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
© 2018 Microchip Technology Inc.
TB3192
TB3192
Configuration of the SPI Peripheral
// Oscillator Configuration
OSCCON1 = 0x60; // HFINTOSC; NDIV = 1;
OSCCON3 = 0x00; // CSWHOLD may proceed; SOSCPWR Low power;
OSCEN = 0x00; // Oscillator Manual Enable Register;
OSCFRQ = 0x03; // HFINTOSC = 8 MHz;
OSCTUNE = 0x00; // // Calibrated Frequency (Default);
// Port Configuration
TRISA = 0x00; // RA3 = output;
TRISB = 0x10; // RB1 = input, RB3 = output;
ANSELA = 0x00; // Digital I/O;
ANSELB = 0x00; // Digital I/O;
SPI1SDIPPSbits.SPI1SDIPPS = 0x0A; // RB2->SPI1:SDI1; -- input;
RB1PPS = 0x1E; // RB1->SPI1:SCK1; -- output;
RB3PPS = 0x1F; // RB3->SPI1:SDO1; -- output;
RA3PPS = 0x20; // RA3->SPI1:SS1; -- output;
// SPI Configuration
SPI1CON1 = 0x64; // CKE; CKP; SDI/SDO Polarity;
SPI1CON2 = 0x07; // TXR; RXR;
SPI1BAUD = 0x07; // SPI Baud Pre-Scaler;
SPI1CLK = 0x00; // SPI Clock Select;
SPI1CON0 = 0x83; // SPI Enable; BMODE; Master/Slave; MSb/LSb;
3. Transfer Counter
The transfer counter is a feature that has been added to the SPI module and provides the capability to
determine how many data transfers that the SPI will send/receive. It is important to configure the transfer
counter and the BMODE (Bit-Length Mode Selection) bit of the SPIxCON0 register appropriately to
ensure that the SPI module operates as intended. The functionality of the transfer counter depends on
the value of the BMODE bit of the SPIxCON0 register.
4. Master Mode
There are four different configurations for the SPI module while in Master mode that are selected by
setting the TXR and RXR bits of the SPIxCON2 register. The four modes of operation available, while the
SPI device is operating as a master, are illustrated in Table 4-1. There are typically three types of
scenarios in a SPI data transaction:
• The master sends useful data and the slave sends useful data.
• The master sends useful data and the slave sends dummy data.
• The master sends dummy data and the slave sends useful data.
This SPI module allows the user to handle these different scenarios without software involvement by
configuring the TXR and RXR pins of the SPI1xCON2 register accordingly.
Table 4-1. Master Mode TXR/RXR Settings
TXR = 1 TXR = 0
If BMODE = 1, transfer when RXFIFO is Transfer when RxFIFO is not full and the
not full and TXFIFO is not empty. transfer counter is non-zero.
If BMODE = 0, transfer when RXFIFO is Transmitted data is either the top of the
not full, TXFIFO is not empty and the most-recently received data.
transfer counter is non-zero.
RXR = 0 No Transfers
Transmit-Only mode
6. Slave Mode
Table 6-1. Slave Mode TXR/RXR Settings
Setting Description
TXR = 1 Data from the TX buffer is transmitted and the write FIFO pointer is
incremented. If the TX buffer is empty, the most recently received data is
transmitted and the Transmit Underflow Interrupt Flag (TXUIF) bit is set to
indicate that this type of error occurred.
TXR = 0 Data in the TX buffer is transmitted if available, but the write FIFO pointer is
not incremented. If the TX buffer is empty, the most recently received data is
transmitted, but the TXUIF bit will not be set.
RXR = 1 Data will be stored in the RX buffer if it is not full and the read FIFO pointer is
incremented. If data is received and the RX buffer is full, the Receive
Overflow Interrupt Flag (RXOIF) bit is set to indicate the error and the data
received is discarded.
RXR = 0 All received data will be ignored and not stored in the RX register.
The TXR and RXR bits control how data is transferred when a device is configured as a slave on the SPI
bus. Table 6-1 summarizes how the configuration of the RXR and TXR bits affect the operation of the SPI
module while in Slave mode. For more information about how data is transmitted/received while in Slave
mode, refer to the device data sheet.
In the event where the Slave Select (SS) line transitions to an inactive state while a data transfer is
ongoing, the Slave Select Fault (SSFLT) bit in the SPIxCON2 register will be set. The SSP bit of the
SPIxCON1 register controls slave select polarity. When the SSP bit is set, the Slave Select (SS) line is
active-low. Conversely, when the SSP bit is cleared, the Slave Select (SS) line is active-high. In addition
to this, the SCK pin must always be an input and configured to the same clock polarity and edge as the
master device. Clock polarity is controlled by the CKP bit and the clock edge is set by the CKE bit both
found in the SPIxCON1 register.
7. Conclusion
This technical brief gives a brief overview of the new Serial Peripheral Interface (SPI) module, described
and walked through basic modes of operation, and briefly discussed other key features. The code
examples included in this document serve as a walk through on getting started in building an application
that uses this SPI module. For more information or specifications pertaining to this module, refer to the
device datasheet.
8. Revision A (4/2018)
Initial release of this document.
Customer Support
Users of Microchip products can receive assistance through several channels:
• Distributor or Representative
• Local Sales Office
• Field Application Engineer (FAE)
• Technical Support
Customers should contact their distributor, representative or Field Application Engineer (FAE) for support.
Local sales offices are also available to help customers. A listing of sales offices and locations is included
in the back of this document.
Technical support is available through the web site at: http://www.microchip.com/support
• Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their
code. Code protection does not mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the
code protection features of our products. Attempts to break Microchip’s code protection feature may be a
violation of the Digital Millennium Copyright Act. If such acts allow unauthorized access to your software
or other copyrighted work, you may have a right to sue for relief under that Act.
Legal Notice
Information contained in this publication regarding device applications and the like is provided only for
your convenience and may be superseded by updates. It is your responsibility to ensure that your
application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR
WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY
OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS
CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE.
Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life
support and/or safety applications is entirely at the buyer’s risk, and the buyer agrees to defend,
indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting
from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual
property rights unless otherwise stated.
Trademarks
The Microchip name and logo, the Microchip logo, AnyRate, AVR, AVR logo, AVR Freaks, BeaconThings,
BitCloud, CryptoMemory, CryptoRF, dsPIC, FlashFlex, flexPWR, Heldo, JukeBlox, KeeLoq, KeeLoq logo,
Kleer, LANCheck, LINK MD, maXStylus, maXTouch, MediaLB, megaAVR, MOST, MOST logo, MPLAB,
OptoLyzer, PIC, picoPower, PICSTART, PIC32 logo, Prochip Designer, QTouch, RightTouch, SAM-BA,
SpyNIC, SST, SST Logo, SuperFlash, tinyAVR, UNI/O, and XMEGA are registered trademarks of
Microchip Technology Incorporated in the U.S.A. and other countries.
ClockWorks, The Embedded Control Solutions Company, EtherSynch, Hyper Speed Control, HyperLight
Load, IntelliMOS, mTouch, Precision Edge, and Quiet-Wire are registered trademarks of Microchip
Technology Incorporated in the U.S.A.
Adjacent Key Suppression, AKS, Analog-for-the-Digital Age, Any Capacitor, AnyIn, AnyOut, BodyCom,
chipKIT, chipKIT logo, CodeGuard, CryptoAuthentication, CryptoCompanion, CryptoController,
dsPICDEM, dsPICDEM.net, Dynamic Average Matching, DAM, ECAN, EtherGREEN, In-Circuit Serial
Programming, ICSP, Inter-Chip Connectivity, JitterBlocker, KleerNet, KleerNet logo, Mindi, MiWi,
motorBench, MPASM, MPF, MPLAB Certified logo, MPLIB, MPLINK, MultiTRAK, NetDetach, Omniscient
Code Generation, PICDEM, PICDEM.net, PICkit, PICtail, PureSilicon, QMatrix, RightTouch logo, REAL
ICE, Ripple Blocker, SAM-ICE, Serial Quad I/O, SMART-I.S., SQI, SuperSwitcher, SuperSwitcher II, Total
Endurance, TSHARC, USBCheck, VariSense, ViewSpan, WiperLock, Wireless DNA, and ZENA are
trademarks of Microchip Technology Incorporated in the U.S.A. and other countries.
SQTP is a service mark of Microchip Technology Incorporated in the U.S.A.
Silicon Storage Technology is a registered trademark of Microchip Technology Inc. in other countries.
GestIC is a registered trademark of Microchip Technology Germany II GmbH & Co. KG, a subsidiary of
Microchip Technology Inc., in other countries.
All other trademarks mentioned herein are property of their respective companies.
© 2018, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved.
ISBN: 978-1-5224-2965-4
ISO/TS 16949
Microchip received ISO/TS-16949:2009 certification for its worldwide headquarters, design and wafer
fabrication facilities in Chandler and Tempe, Arizona; Gresham, Oregon and design centers in California
® ®
and India. The Company’s quality system processes and procedures are for its PIC MCUs and dsPIC
®
DSCs, KEELOQ code hopping devices, Serial EEPROMs, microperipherals, nonvolatile memory and
analog products. In addition, Microchip’s quality system for the design and manufacture of development
systems is ISO 9001:2000 certified.