Documente Academic
Documente Profesional
Documente Cultură
SPI.1
I/O - Communications
Serial I/O:
I/O Pin
Microcontroller
time
Parallel I/O:
1
I/O Pins
1
0
Microcontroller
1
1
Advantage serial:
Only need one data line and ground.
Advantage parallel:
Multiple bits are sent or received
at the same time.
1
0
1
SPI.2
Serial Communications
Asynchronous vs. Synchronous
Data Lines
Device
(A)
Data Lines
Device
(B)
Device
(A)
Device
(B)
Common
Clock Line
Notes #12
SPI.3
Serial Communications
Synchronous
SPI.4
PIC Micro
Receive Register
Microcontroller
(Slave)
SDO
SDI
SDI
SDO
SCK
SCK
Receive Register
Serial Buffer
SS
SPI.5
Microcontroller
(Slave)
MOSI
MISO
Receive Register
Serial Buffer
Receive Register
SCLK
SS
SPI.6
SDI
SDO
SCK
SS
SPI
Slave #1
SDI
SDO
SCK
SS
SPI
Slave #2
SDI
SDO
SCK
SS
SPI
Slave #3
SPI.7
MSSP SPI
PORTC<4>
Serial Data In
Serial Data Out
PORTC<5>
Slave Select
Master:
TRISC<5> = 0;
TRISC<3> = 0;
TRISC<4> dont worry
Slave:
TRISC<5> = 0;
Serial Clock
TRISC<3> = 1;
TRISC<4> dont worry
TRISA<5> = 1; // input
PORTA<5>
PORTC<3>
SPI.8
Example
PIC16F877
Peripheral Device
(3-axis accelerometers)
RE2
SDO
SDI
SCK
SPI.9
SPI Clock
Master Mode
SPI.10
Master Mode
Clock rate: Fosc/4 (or Tcy)
SPI.11
SSPCON
SPI.12
SPI Configuration
SPI.13
Configuration
of Clock
SPI Configuration
CKE = Clock Phase (CPHA)
CKP = Clock Polarity (CPOL)
SPI.14
Opposite of CKE
0
SPI.15
SPI.16
SPI.17
Example
RE2
SDO
SDI
SCK
SPI.18
SPI.19
SMP = 0
SPI.20
Example Configuration
0
0
0
0
0
Read-only bits: leave as defaults
Enable SSP
SPI.21
SSPCON
SPI.22
Assembly-code
bsf
movlw
movwf
bcf
bcf
bsf
bcf
bcf
movlw
movwf
STATUS, RP0
b00000000
SSPSTAT
TRISC,5
TRISC,3
PIE1, SSPIE
STATUS, RP0
PIR1, SSPIF
b00110000
SSPCON
bsf
bsf
INTCON, PEIE
INTCON, GIE
SPI.23
char getch()
{
while(!SSPIF) continue;
}
return SSPBUF;
SPI.24
Slave Mode
SPI.25
Slave Mode
SPI.26
Advantages
Advantages
Slaves don't need a unique address unlike I2C
Transceivers are not needed;
Uses only four pins on IC packages, and wires in board
layouts or connectors, much fewer than parallel
interfaces;
At most one unique bus signal per device (chip select);
all others are shared;
Signals are unidirectional allowing for easy Galvanic
isolation;
Not limited to any maximum clock speed, enabling
potentially high throughput.
SPI.28
Disadvantages
Requires more pins on IC packages than IC, even in
the three-wire variant
No in-band addressing; out-of-band chip select signals
are required on shared buses
No hardware flow control by the slave (but the master
can delay the next clock edge to slow the transfer
rate)
No hardware slave acknowledgment (the master could
be transmitting to nowhere and not know it)
Supports only one master device
No error-checking protocol is defined
SPI.29
Disadvantages
Generally prone to noise spikes causing faulty
communication
Without a formal standard, validating conformance is
not possible
Only handles short distances compared to RS-232,
RS-485, or CAN-bus
Many existing variations, making it difficult to find
development tools like host adapters that support
those variations
SPI does not support hot plugging (dynamically adding
nodes).
SPI.30