You are on page 1of 36

SPI MASTER SYNCHRONOUS SERIAL PORT (MSSP) MODULE

PIC18f452
MSSP MODULE MODES
The MSSP module can operate in one of two modes:
 Serial Peripheral Interface (SPI)
 Inter-Integrated Circuit (I2C)

We will only Discuss SPI


SPI BASICS
 A 4-wire communications bus
 Typically communicate across short distances
 Supports
 Single master
 Multiple slaves

 Synchronized
 Communications are “clocked”

3
SPI PERIPHERAL TYPES
 Converters (ADC, DAC)
 Memories (EEPROM, RAM’s,Flash)
 Sensors (Temperature, Humidity, Pressure)
 Real Time Clocks
 Misc- Potentiometers, LCD controllers, UART’s, USB controller, CAN
controller,amplifiers
 I/O Expanders etc……

MAJOR USE:: Read/ Write data to specific


memory location of any device
GENERAL CONNECTIONS
INTRO
 Registers and Flags associated with SPI
 SSPCON1 (8-bit)  MSSP Control Register1
 SSPSTAT (8-bit)  MSSP Status Register
 SSPBUF (8-bit)  Serial Receive/Transmit Buffer
 SSPSR (8-bit)  MSSP Shift Register
 SSPIF flag
INSIDE SSPSTAT REGISTER

bit 7 SMP: Sample bit


SPI Master mode:
1 = Input data sampled at end of data output time
0 = Input data sampled at middle of data output time
SPI Slave mode:
SMP must be cleared when SPI is used in Slave mode

bit 6 CKE: SPI Clock Edge Select


When CKP = 0:
1 = Data transmitted on rising edge of SCK
0 = Data transmitted on falling edge of SCK
When CKP = 1:
1 = Data transmitted on falling edge of SCK
0 = Data transmitted on rising edge of SCK
INSIDE SSPSTAT REGISTER

bit 5 D/A: Data/Address bit


Used in I2C mode only
bit 4 P: STOP bit
Used in I2C mode only. This bit is cleared when the MSSP module is
disabled, SSPEN is cleared.
bit 3 S: START bit
Used in I2C mode only
bit 2 R/W: Read/Write bit information
Used in I2C mode only
bit 1 UA: Update Address
Used in I2C mode only
bit 0 BF: Buffer Full Status bit (Receive mode only)
1 = Receive complete, SSPBUF is full
0 = Receive not complete, SSPBUF is empty
INSIDE SSPCON1 REGISTER
bit 7 WCOL: Write Collision Detect bit (Transmit mode only)
1 = The SSPBUF register is written while it is still transmitting the previous word
(must be cleared in software)
0 = No collision

bit 6 SSPOV: Receive Overflow Indicator bit


SPI Slave mode:
1 = A new byte is received while the SSPBUF register is still holding the
previous data. In case of overflow, the data in SSPSR is lost. Overflow can
only occur in Slave mode. The user must read the SSPBUF, even if only
transmitting data, to avoid setting overflow (must be cleared in software).
0 = No overflow
Note: In Master mode, the overflow bit is not set

bit 5 SSPEN: Synchronous Serial Port Enable bit


1 = Enables serial port and configures SCK, SDO, SDI, and SS as serial port pins
0 = Disables serial port and configures these pins as I/O port pins
Note: When enabled, these pins must be properly configured as input or output.
INSIDE SSPCON1 REGISTER
bit 4 CKP: Clock Polarity Select bit
1 = IDLE state for clock is a high level
0 = IDLE state for clock is a low level

bit 3-0 SSPM3:SSPM0: Synchronous Serial Port Mode Select bits


0101 = SPI Slave mode, clock = SCK pin, SS pin control disabled, SS can be used as I/O pin
0100 = SPI Slave mode, clock = SCK pin, SS pin control enabled
0011 = SPI Master mode, clock = TMR2 output/2
0010 = SPI Master mode, clock = FOSC/64
0001 = SPI Master mode, clock = FOSC/16
0000 = SPI Master mode, clock = FOSC/4
Rest of the combinations are used in I2C mode.
SSPCON2 REGISTER  ONLY USED ON I2C MODE
Now HOW to read or write on specific
memory location of any slave device????
SPI SINGLE BYTE WRITE PROTOCOL

NOTE: while
writing A7 is R/W
bit. Its not always 1 .
It can be 0, depends
upon slave device
attached
SPI MULTI BYTE BURST WRITE PROTOCOL
SPI SINGLE BYTE READ PROTOCOL

NOTE: while
reading A7 is R/W
bit. Its not always 0 .
It can be 1, depends
upon slave device
attached
SPI MULTI BYTE BURST READ PROTOCOL
Do proper
Load TRIS settings Load SSPBUF reg
SSPCON1 of SDI, SDO, with data to be
and SSPSTAT SS’ , SCK transmitted
pins

Goto next step Wait for BF bit


according your to become high
requirement (wait for
completion of data
transmission)
Do proper TRIS Wait for BF bit
Load SSPCON1 settings of SDI, to become high
and SSPSTAT SDO, SS’ , SCK (wait for
pins completion of data
reception)

Goto next step Take data out


according your from SSPBUF.
requirement
Lets consider a RTC chip as a slave.
How to change time or date of this chip using
SPI ….??

Lets Discuss it!!!!


NECESSARY BASICS OF DS1306 RTC CHIP (REAL TIME CLK)

NOTE: SERMODE pin of


RTC must be @ VCC to
use it in SPI mode
RTC INTERNAL
REGISTERS
ADDRESSES AND
SETTINGS
NOTE: To write to
RTC memory WP bit
in Control Register
must be cleared
first.
PROBLEM: Code to change time and date of RTC. SPI(0x55); // 55 seconds

SOLUTION: SPI(0x58); // 58 minutes

#include <P18f452.h> SPI(0x16); // 24 hour clock at 16 hours


unsigned char SPI(unsigned char); SPI(0x3); // Tuesday
Void SDELAY(int ms); SPI(0x19); // 19th of the month
Void main(){ SPI(0x10); // october
SSPSTAT=0; //read at middle, send on active edge SPI(0x04); //2004
SSPCON1=0x22; //master SPI enable,Fosc/64 PORTCbits.RC2=0; //end multiByte write
TRISC=0; //make PORTC output
SDELAY(1);
TRISCbits.TRISC4=1;//except SDI
SDELAY(1);
PORTCbits.RC2=1; //enable the RTC
}
SDELAY(1);
//--SPI write/read subroutine
SPI(0x8F); // control register address
unsigned char SPI(unsigned char myByte)
SPI(0x00); // clear WP bit for write
{
PORTCbits.RC2=0; // end of singleByte write
SSPBUF=myByte;//load SSPBUF for transfer
SDELAY(1);
PORTCbits.RC2=1; //begin multiByte write
While(!SSPSTATbits.BF);//wait for all bits

SPI(0x80); // seconds register address Return SSPBUF;//return with received byte


}
ASSIGNMENT
Try to understand Example 16-5c from book!!!
SOME USEFUL MIKROC LIBRARY
FUNCTIONS OF SPI
MIKROC SPI LIBRARY FUNCTIONS
 void SPIx_Init();
Configures and initializes the SPI module. The internal SPI module is
setting configured in this function can be seen in MIKROC manual. We
can also use another function namely SPIx_Initi_Advanced(…) to change
dafault settings of SPI.
// CODE to Initialize SPI
SPI1_Init();

 unsigned short SPIx_Read(unsigned short buffer);


Returns the received data. SPIx_Init() must be called before using this
function. // code to read data from addr 0x06
unsigned short take;
unsigned short buffer = 0x06;
SPI1_Init();
take = SPI1_Read(buffer); // read from addr 0x06
MIKROC LIBRARY FUNCTIONS
 void SPIx_Write(unsigned short data_);
Writes byte via the SPI bus. SPIx_Init() must be called before using this function.

//code to transmit 0x03 to SPI


SPI1_Init();
char buffer = 0x03;
SPI1_Write(buffer); //send 0x03 via SPI
MCP23S17
MCP23S17 – SPI INTERFACE I/O
EXPANADER
MCP23S17
INTERNAL
REGISTERS
ADDRESSES
AND SETTINGS
SPI READ/WRITE OPCODE
E.G if pins A0 = A1 = A2 = 0V (Ground)

Then:
 Read Opcode = 0b01000001 = 0x41
 And Write Opcode = 0b01000000 = 0x40
HOW TO READ/WRITE TO I/O EXPANDER?
 Steps to read data from a  Steps to write data on a
specific memory location specific memory location
1. Send read OPCODE 1. Send write OPCODE
2. Send Register Address 2. Send Register address
3. In return, data at that 3. Send Data to be stored at
address would be thrown that memory location
out by IO expander. We
have to read that data
Code following scenario using Mikro C libraries to toggle GPA port continuously
of MCP23S17 constantly via SPI
void main() {
unsigned char val=0xaa;
while(1){
TRISC.TRISC0=0; //CS ,CE
TRISC.TRISC5=0;//SDO val=~val;

TRISC.TRISC3=0; //CLK PORTC.RC0=0; //CS


TRISC.TRISC4 = 1; //SDI SPI1_Write(0x40); //write opcode
SPI1_Write(0x14); //select GPA
SPI1_Init(); // Initialize SPI module SPI1_Write(val); //Write Val GPA
PORTC.RC0=1; //CS
PORTC.RC0=0; //CS delay_ms(500);
SPI1_Write(0x40); //write opcode }
SPI1_Write(0x00); //select IODIRA
} // main ends here
SPI1_Write(0x00); //select as O/P
PORTC.RC0=1; //CS
ASSIGNMENT
Using MIKRO-C Library functions, Read switches
attached with GPB of MCP23S17 and show their
value continuously on PORTB of PIC. Simulate it in
Proteus

You might also like