Professional Documents
Culture Documents
SPI
Antarmuka Serial Asinkron
Comparison of Serial Interfaces
Outline
• What is SPI?
• SPI Configuration
• SPI Operation
• Master Slave Setup
• SPI Transactions
• SPI Peripheral Types
• SPI and Microcontrollers
• ESBUS
SPI
(Serial Peripheral Interface)
• Developed by Motorola
•Also known as MicroWire (National
Semiconductor), QSPI (Queued), MicrowirePlus
•Synchronous Serial Communication
SPI Bus
SPI Configuration
• Primarily used for serial
communication between a
host processor and
peripherals.
• Can also connect 2
processors via SPI
• SPI works in a master slave
configuration with the
master being the host
microcontroller for example
and the slave being the
peripheral
SPI Operation
• For SPI, there are Serial Clocks (SCLK), Chip Select lines (CS),
Serial Data In (SDI) and Serial Data Out( SDO)
• There is only one master, there number of slaves depends
on the number of chip select lines of the master.
• Synchronous operation, latch on rising or falling edge of
clock, SDI on rising edge, SDO on falling edge
• Operates in 1 to 2 MHz range
• Master sends out clocks and chip selects. Activates the
slaves it wants to communicate with
Master Slave Setup
• Multiple Independent Slave
• In this setup, there are 3 slave devices. The SDO lines are
Configuration
tied together to the SDI line of the master.
• The master determines which chip it is talking to by the CS
lines. For the slaves that are not being talked to, the data
output goes to a Hi Z state
Master Slave Setup
Multiple slave cascaded
SPI module
$DA
$D8
The SPI module
SPIxCON register
Initializing SPI2
Pin 12 of PORTD is connected to the memory chip select (CS):
#define CSEE _RD12 // select line for Serial EEPROM
#define TCSEE _TRISD12 // tris control for CSEE pin
Peripheral initialization:
// 1. init the SPI peripheral
TCSEE = 0; // make SSEE pin output
CSEE = 1; // de-select the Serial EEPROM
SPI2CON1 = SPI_MASTER; // select mode
SPI2STAT = SPI_ENABLE; // enable the peripheral
Where:
#define SPI_MASTER 0x0120 // 8-bit master mode, CKE=1, CKP =0
#define SPI_ENABLE 0x8000 // enable SPI port, clear status
A small function that will be used to transfer data to and from the serial EEPROM device:
// send one byte of data and receive one back at the same time
int WriteSPI2( int data)
{
SPI2BUF = data; // write to buffer for TX
while( !SPI2STATbits.SPIRBF); // wait transfer completion
return SPI2BUF; // read the received value
} // WriteSPI2
The 25LC256 Serial EEPROM
// 25LC256 Serial EEPROM commands
#define SEE_WRSR 1 // write status register
#define SEE_WRITE 2 // write command
#define SEE_READ 3 // read command
#define SEE_WDI 4 // write disable
#define SEE_STAT 5 // read status register
#define SEE_WEN 6 // write enable
25LC256 Status Register
Consider additionally:
Battery gauges
Audio codecs
GPS receivers
LCD displays controllers
Card readers
I2C Data Transfer Rules
Here is an example of a read command sequence for a 24LC00 (128 bit) SEE:
S 0b01010001 (A) (Data) NP
A Write Example
S 0b01010000 (A) ByteAddress (A) Data (A) P
I2CxCON register
Initializing the I2C module
void InitSEE( long fcy)
// fcy = processor operating frequency in Hz (system clock)
{ // Configure I2C for 7 bit address mode 100kHz
OpenI2C1( I2C_ON | I2C_IDLE_CON | I2C_7BIT_ADD | I2C_STR_EN
| I2C_GCALL_DIS | I2C_SM_DIS | I2C_IPMI_DIS,
(fcy /(2*BUS_FRQ))-1);
IdleI2C1();
T1CON=0x8030;
TMR1=0;
while( TMR1< 100);
} //InitSEE
Sending the Address