PIC24F RefManual - 23 39699a
PIC24F RefManual - 23 39699a
HIGHLIGHTS
This section of the manual contains the following major topics:
23
Serial Peripheral
23.1 Introduction .................................................................................................................. 23-2
Interface (SPI)
23.2 Status and Control Registers ....................................................................................... 23-3
23.3 Modes of Operation ..................................................................................................... 23-7
23.4 Master Mode Clock Frequency .................................................................................. 23-18
23.5 Operation in Power-Saving Modes ............................................................................ 23-19
23.6 Register Maps ............................................................................................................ 23-20
23.7 Electrical Specifications ............................................................................................. 23-21
23.8 Related Application Notes.......................................................................................... 23-25
23.9 Revision History ......................................................................................................... 23-26
23.1 INTRODUCTION
The Serial Peripheral Interface (SPI) module is a synchronous serial interface useful for commu-
nicating with other peripheral or microcontroller devices. These peripheral devices may be serial
EEPROMs, shift registers, display drivers, A/D converters, etc. The SPI module is compatible
with Motorola’s SPI and SIOP interfaces.
Depending on the variant, the PIC24F family offers one or two SPI modules on a single device.
The modules, designated SPI1 and SPI2, are functionally identical. The SPI2 module is available
in many of the higher pin count packages, while the SPI1 module is available on all devices.
Note: In this section, the SPI modules are referred to together as SPIx, or separately as
SPI1 and SPI2. Special Function Registers will follow a similar notation. For
example, SPIxCON refers to the control register for the SPI1 or SPI2 module.
Transfer Transfer
SPIxBUF
16
Internal Data Bus
Serial Peripheral
Interface (SPI)
SPIxBUF register is actually comprised of two separate registers: the Transmit Buffer, SPIxTXB,
and the Receive Buffer, SPIxRXB. These two unidirectional, 16-bit registers share the SFR
address of SPIxBUF. If a user writes data to be transmitted to the SPIxBUF address, internally
the data is written to the SPIxTXB register. Similarly, when the user reads the received data from
SPIxBUF, internally the data is read from the SPIxRXB register.
This technique double-buffers transmit and receive operations and allows continuous data
transfers in the background. Transmission and reception occur simultaneously.
Legend: 22
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
Serial Peripheral
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Interface (SPI)
bit 15-13 Unimplemented: Read as ‘0’
bit 12 DISSCK: Disable SCKx pin bit (SPIx Master modes only)
1 = Internal SPIx clock is disabled, pin functions as I/O
0 = Internal SPIx clock is enabled
bit 11 DISSDO: Disable SDOx Pin bit
1 = SDOx pin is not used by module, pin functions as I/O
0 = SDOx pin is controlled by the module
bit 10 MODE16: Word/Byte Communication Select bit
1 = Communication is word-wide (16 bits)
0 = Communication is byte-wide (8 bits)
bit 9 SMP: SPIx Data Input Sample Phase bit
Master mode:
1 = Input data sampled at end of data output time
0 = Input data sampled at middle of data output time
Slave mode:
SMP must be cleared when SPIx is used in Slave mode.
bit 8 CKE: SPIx Clock Edge Select bit
1 = Serial output data changes on transition from active clock state to Idle clock state (see bit 6)
0 = Serial output data changes on transition from Idle clock state to active clock state (see bit 6).
Note: The CKE bit is not used in the Framed SPIx modes. The user should program this bit to ‘0’
for the Framed SPIx modes (FRMEN = 1).
bit 7 SSEN: Slave Select Enable (Slave mode) bit
1 = SSx pin used for Slave mode
0 = SSx pin not used by module, pin controlled by port function
bit 6 CKP: Clock Polarity Select bit
1 = Idle state for clock is a high level; active state is a low level
0 = Idle state for clock is a low level; active state is a high level
bit 5 MSTEN: Master Mode Enable bit
1 = Master mode
0 = Slave mode
bit 4-2 SPRE2:SPRE0: Secondary Prescale bits (Master mode)
111 = Secondary prescale 1:1
110 = Secondary prescale 2:1
...
000 = Secondary prescale 8:1
bit 1-0 PPRE1:PPRE0: Primary Prescale bits (Master mode)
11 = Primary prescale 1:1
10 = Primary prescale 4:1
01 = Primary prescale 16:1
00 = Primary prescale 64:1
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Serial Peripheral
Interface (SPI)
Data is transmitted out of bit 7 of the SPIxSR for 8-bit operation, while it is transmitted out of bit 15
of the SPIxSR for 16-bit operation. In both modes, data is shifted into bit 0 of the SPIxSR.
When transmitting or receiving data, 8 clock pulses at the SCKx pin are required to shift in/out
data in 8-bit mode, while 16 clock pulses are required in 16-bit mode.
SDOx SDIx
Serial Clock
SPIx Buffer SCKx SCKx SPIx Buffer
(SPIxBUF)(2) (SPIxBUF)(2)
SSx SSx(1)
User writes
User writes new data
to SPIxBUF SPIxTXB to SPIxSR(3)
during transmission
SPITBF
SCKx
(CKP = 0
CKE = 0)(1)
4 Clock
SCKx
(CKP = 1
modes
(clock
22
CKE = 0)(1) output at
Serial Peripheral
SCKx the SCKx
Interface (SPI)
(CKP = 0 pin in
CKE = 1)(1) Master
mode)
SCKx
(CKP = 1
CKE = 1)(1)
SDOx
bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
(CKE = 0)
SDIx
(SMP = 0)(2)
bit 7 bit 0
Input
Sample Two modes
(SMP = 0)(2) available
for SMP
SDIx control
(SMP = 1)(2) bit(4)
bit 7 bit 0
Input
Sample
(SMP = 1)(2)
SPIxIF
SPIRBF
(SPIxSTAT<0>)
User reads
SPIxBUF
Note 1: Four SPIx Clock modes shown to demonstrate CKP (SPIxCON1<6>) and CKE (SPIxCON1<8>) bit functionality only.
Only one of the four modes can be chosen for operation.
2: SDIx and input sample shown for two different values of the SMP (SPIxCON1<9>) bit, for demonstration purposes
only. Only one of the two configurations of the SMP bit can be chosen during operation.
3: If there are no pending transmissions, SPIxTXB is transferred to SPIxSR as soon as the user writes to SPIxBUF.
4: Operation for 8-bit mode shown; the 16-bit mode is similar.
Figure 23-4: SPIx Slave Mode Timing (Slave Select Pin Disabled)(3)
SCKx Input
(CKP = 0
CKE = 0)(1)
SCKx Input
(CKP = 1
CKE = 0)(1)
SDIx Input
22
(SMP = 0)
Serial Peripheral
bit 7 bit 0
Interface (SPI)
Input
Sample
(SMP = 0)
User writes to
SPIxBUF(2)
SPITBF
SPIxSR to
SPIxRXB
SPIRBF
Note 1: Two SPIx Clock modes shown only to demonstrate CKP (SPIxCON<6>) and CKE (SPIxCON<8>) bit functionality.
Any combination of CKP and CKE bits can be chosen for module operation.
2: If there are no pending transmissions or a transmission is in progress, SPIxBUF is transferred to SPIxSR as soon
as the user writes to SPIxBUF.
3: Operation for 8-bit mode shown; the 16-bit mode is similar.
Figure 23-5: SPIx Slave Mode Timing (Slave Select Pin Enabled)(3)
SSx(1)
SCKx
(CKP = 0
CKE = 0)
SCKx
(CKP = 1
CKE = 0)
User writes SPIxBUF
to to
SPIxBUF SPIxSR
SPITBF(2)
SDIx
(SMP = 0)
bit 7 bit 0
Input
Sample
(SMP = 0)
SPIxIF
1 instruction
cycle latency
SPIxSR to
SPIxBUF
SPIRBF
User reads
SPIxBUF
Note 1: When the SSEN (SPIxCON1<7>) bit is set to ‘1’, the SSx pin must be driven low so as to enable transmission and
reception in Slave mode.
2: Transmit data is held in SPIxTXB and SPITBF remains set until all bits are transmitted.
3: Operation for 8-bit mode shown; the 16-bit mode is similar.
SSx(1,2)
SCK Input
(CKP = 0
CKE = 1)
SCK Input
(CKP = 1
CKE = 1)
22
Serial Peripheral
SDO bit 6 bit 5 bit 4 bit 2 bit 1 bit 0
Interface (SPI)
bit 7 bit 3
Output
SDI Input
(SMP = 0)
bit 7 bit 0
Input
Sample
(SMP = 0)
SPIxIF
Write to
SPIxBUF SPIxSR to
SPIxRXB
SPITBF(3)
SPIRBF
Note 1: The SSx pin must be used for Slave mode operation when CKE = 1.
2: When the SSEN (SPIxCON1<7>) bit is set to ‘1’, the SSx pin must be driven low so as to enable transmission and
reception in Slave mode.
3: Transmit data is held in SPIxTXB and SPITBF remains set until all bits are transmitted.
4: Operation for 8-bit mode shown; the 16-bit mode is similar.
The Framed SPIx modes are supported in conjunction with the unframed Master and Slave
modes. This makes four framed SPIx configurations available to the user:
• SPIx Master Mode and Framed Master Mode
• SPIx Master Mode and Framed Slave Mode
• SPIx Slave Mode and Framed Master Mode
• SPIx Slave Mode and Framed Slave Mode
These modes determine whether or not the SPIx module generates the serial clock and the
frame synchronization pulse.
Serial Peripheral
Interface (SPI)
PIC24F PROCESSOR 2
(SPIx Slave, Framed Slave)
SDOx SDIx
SDIx SDOx
Serial Clock
SCKx SCKx
SSx SSx
Frame Sync
Pulse
SCKx
(CKP = 1)
SCKx
(CKP = 0)
FSYNCx
(SPIFPOL = 1)
FSYNCx
(SPIFPOL = 0)
SCKx
(CKP = 1)
SCKx
(CKP = 0)
FSYNCx
(SPIFPOL = 1)
FSYNCx
(SPIFPOL = 0)
PIC24F PROCESSOR 2
(SPIx Master, Framed Slave)
SDOx SDIx
SDIx SDOx
Serial Clock
SCKx SCKx
SSx SSx
Frame Sync
Pulse
SCKx
(CKP = 1)
SCKx
(CKP = 0)
FSYNCx
(SPIFPOL = 1)
FSYNCx
(SPIFPOL = 0)
SCKx
(CKP = 1)
SCKx
(CKP = 0)
FSYNCx
(SPIFPOL = 1)
FSYNCx
(SPIFPOL = 0)
Serial Peripheral
PIC24F PROCESSOR 2
Interface (SPI)
(SPIx Slave, Framed Slave)
SDOx SDIx
SDIx SDOx
Serial Clock
SCKx SCKx
SSx SSx
Frame Sync
Pulse
PIC24F PROCESSOR 2
(SPIx Master, Framed Slave)
SDOx SDIx
SDIx SDOx
Serial Clock
SCKx SCKx
SSx SSx
Frame Sync
Pulse
Equation 23-1 can be used to calculate the SCKx clock frequency as a function of the primary
and secondary prescaler settings.
Equation 23-1:
FCY
FSCK =
Primary Prescaler * Secondary Prescaler
Some sample SPIx clock frequencies (in kHz) are shown in Table 23-1 below:
Note: Not all clock rates are supported. For further information, refer to the SPIx timing
specifications in the specific device data sheet.
Serial Peripheral
for master operation:
Interface (SPI)
• The Baud Rate Generator in the SPIx module stops and is reset.
• The transmitter and receiver will stop in Sleep. The transmitter or receiver does not
continue with a partially completed transmission at wake-up.
• If the SPIx module enters Sleep mode in the middle of a transmission or reception, the
transmission or reception is aborted. Since there is no automatic way to prevent an entry
into Sleep mode if a transmission or reception is pending, the user software must
synchronize entry into Sleep with SPIx module operation to avoid aborted transmissions.
DS39699A-page 23-20
SPIxCON1 — — — DISSCK DISSDO MODE16 SMP CKE SSEN CKP MSTEN SPRE2 SPRE1 SPRE1 PPRE1 PPRE0 0000
SPIxCON2 FRMEN SPIFSD SPIFPOL — — — — — — — — — — — SPIFE r 0000
SPIxBUF SPIx Transmit and Receive Buffer 0000
PMD1 T5MD T4MD T3MD T2MD T1MD — — — I2C1MD U2MD U1MD SPI2MD SPI1MD — — ADCMD 0000
PMD2 — — — IC5MD IC4MD IC3MD IC2MD IC1MD — — — OC5MD OC4MD OC3MD OC2MD OC1MD 0000
PMD3 — — — — — CMPMD RTCCMD PMPMD CRCPMD — — — — — I2CMD — 0000
Legend: — = unimplemented, read as '0’. Reset values are shown in hexidecimal.
PIC24F Family Reference Manual
SCKx
(CKP = 0)
SCKx
(CKP = 1)
22
SP35 SP20 SP21
Serial Peripheral
Interface (SPI)
SDOx MSb Bit 14 - - - - - -1 LSb
SP31 SP30
SP40 SP41
SP36
SCKx
(CKP = 0)
SCKx
(CKP = 1)
SP35
SP20 SP21
SP40
SP30,SP31
SP41
SSx
SP50 SP52
SCKx
(CKP = 0)
SP71 SP70
SP73 SP72
SCKx
(CKP = 1) 22
Serial Peripheral
SP35 SP72 SP73
Interface (SPI)
SDOx MSb Bit 14 - - - - - -1 LSb
SP30,SP31 SP51
SDIx
SDI MSb In Bit 14 - - - -1 LSb In
SP41
SP40
SP60
SSx
SP50 SP52
SCKx
(CKP = 0)
SCKx
(CKP = 1)
SP35
SP72 SP73
SP52
SP30,SP31 SP51
SP40
Serial Peripheral
Interface (SPI)
Note: Please visit the Microchip web site (www.microchip.com) for additional application
notes and code examples for the PIC24F family of devices.