MSSP (SPI-I2C) module in PIC18Fxxx
Master Synchoronus Serial Port(MSSP) Module
The Master Synchronous Serial Port (MSSP) module is a serial interface, useful for communicating with other peripheral or microcontroller devices. These peripheral devices may be serial EEPROMs, shift registers, display drivers, A/D Converters, etc.
The MSSP module can operate in one of two modes:
• Serial Peripheral Interface (SPI)
• Inter-Integrated Circuit (I2C) - Full Master mode - Slave mode (with general address call)
The I2C interface supports the following modes in hardware:
• Master mode
• Multi-Master mode
• Slave mode
The MSSP module has three associated registers. These include a status register (SSPSTAT) and two control registers (SSPCON1 and SSPCON2). The use of these registers and their individual configuration bits differ significantly depending on whether the MSSP module is operated in SPI or I2C mode.
The SPI mode allows 8 bits of data to be synchronously transmitted and received simultaneously. All four modes of SPI are supported. To accomplish communication, typically three pins are used:
• Serial Data Out (SDO) – RC5/SDO
• Serial Data In (SDI) – RC4/SDI/SDA
• Serial Clock (SCK) – RC3/SCK/SCL Additionally, a fourth pin may be used when in a Slave mode of operation:
• Slave Select (SS) – RA5/SS Figure shows the block diagram of the MSSP module when operating in SPI mode
The MSSP module has four registers for SPI mode operation.
• MSSP Control Register 1 (SSPCON1)
• MSSP Status Register (SSPSTAT)
• Serial Receive/Transmit Buffer Register (SSPBUF)
• MSSP Shift Register (SSPSR) – Not directly accessible SSPCON1 and SSPSTAT are the control and status registers in SPI mode operation.
The SSPCON1 register is readable and writable. The lower 6 bits of the SSPSTAT are read-only. The upper two bits of the SSPSTAT are read/write.
SSPSR is the shift register used for shifting data in or out. SSPBUF is the buffer register to which data bytes are written to or read from. In receive operations, SSPSR and SSPBUF together create a double-buffered receiver. When SSPSR receives a complete byte, it is transferred to SSPBUF and the SSPIF interrupt is set. During transmission, the SSPBUF is not doublebuffered. A write to SSPBUF will write to both SSPBUF and SSPSR.
SSPSTAT: MSSP STATUS REGISTER (SPI MODE)
|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.
|6|| CKE: SPI Clock Select bit(1)|
1 = Transmit occurs on transition from active to Idle clock state
0 = Transmit occurs on transition from Idle to active clock state
|5||D/A: Data/Address bit Used in I2C™ mode only.|
|4|| P: Stop bit Used in I2C mode only.|
This bit is cleared when the MSSP module is disabled, SSPEN is cleared.
|3||S: Start bit Used in I2C mode only.|
|2||R/W: Read/Write Information bit Used in I2C mode only.|
|1||UA: Update Address bit Used in I2C mode only.|
|0|| BF: Buffer Full Status bit (Receive mode only)|
1 = Receive complete, SSPBUF is full
0 = Receive not complete, SSPBUF is empty
SSPCON1: MSSP CONTROL REGISTER 1 (SPI MODE)
|7||WCOL: Write Collision Detect bit|
1 = The SSPxBUF register is written while it is still transmitting the previous word (must be cleared in software)
0 = No collision
|6|| SSPOV: Receive Overflow Indicator bit(1) 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
|5|| SSPEN: Master Synchronous Serial Port Enable bit(2)|
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
|4||CKP: Clock Polarity Select bit|
1 = Idle state for clock is a high level
0 = Idle state for clock is a low level
|3-0|| SSPM<3:0>: Master Synchronous Serial Port Mode Select bits(3)|
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
When initializing the SPI, several options need to be specified. This is done by programming the appropriate control bits (SSPCON1<5:0> and SSPSTAT<7:6>). These control bits allow the following to be specified:
- Master mode (SCK is the clock output)
- Slave mode (SCK is the clock input)
- Clock Polarity (Idle state of SCK)
- Data Input Sample Phase (middle or end of data output time)
- Clock Edge (output data on rising/falling edge of SCK)
- Clock Rate (Master mode only)
- Slave Select mode (Slave mode only)
The MSSP consists of a transmit/receive shift register (SSPSR) and a buffer register (SSPBUF). The SSPSR shifts the data in and out of the device, MSb first. The SSPBUF holds the data that was written to the SSPSR until the received data is ready. Once the 8 bits of data have been received, that byte is moved to the SSPBUF register. Then, the Buffer Full detect bit, BF (SSPSTAT<0>) and the interrupt flag bit, SSPIF, are set. This double-buffering of the received data (SSPBUF) allows the next byte to start reception before reading the data that was just received. Any write to the SSPBUF register during transmission/reception of data will be ignored and the write collision detect bit, WCOL (SSPCON1<7>), will be set. User software must clear the WCOL bit so that it can be determined if the following write(s) to the SSPBUF register completed successfully. When the application software is expecting to receive valid data, the SSPBUF should be read before the next byte of data to transfer is written to the SSPBUF. The Buffer Full bit, BF (SSPSTAT<0>), indicates when SSPBUF has been loaded with the received data (transmission is complete). When the SSPBUF is read, the BF bit is cleared. This data may be irrelevant if the SPI is only a transmitter. Generally, the MSSP interrupt is used to determine when the transmission/reception has completed. The SSPBUF must be read and/or written. If the interrupt method is not going to be used, then software polling can be done to ensure that a write collision does not occur. Example17-1 shows the loading of the SSPBUF (SSPSR) for data transmission. The SSPSR is not directly readable or writable and can only be accessed by addressing the SSPBUF register. Additionally, the MSSP Status register (SSPSTAT) indicates the various status conditions.
MSSP-I2C-mode (Slave Mode)
FIG: MSSP BLOCK DIAGRAM (I2C MODE)
The MSSP module in I2C mode fully implements all master and slave functions (including general call support) and provides interrupts on Start and Stop bits in hardware to determine a free bus (multi-master function). The MSSP module implements the standard mode specifications, as well as 7-Bit and 10-Bit Addressing modes.
SCL (Serial Clock)
SDA (Serial Data)
Note-The user must configure these pins as inputs by setting the associated TRIS bits.
MSSP( I2C Mode) REGISTERS
The MSSP module has six registers for I2C operation. These are: I2C Register Map:
MSSP Control Register 1
MSSP Control Register 2
MSSP Status Register
MSSP Address Register
Serial Rx/Tx Buffer Register
|SSPSR||MSSP Shift Register||Not Accesible||-||-|
- SSPCON1, SSPCON2 and SSPSTAT are the control and status registers in I2C mode operation. The SSPCON1 and SSPCON2 registers are readable and writable. The lower 6 bits of the SSPSTAT are read-only. The upper two bits of the SSPSTAT are read/write.
- SSPSR is the shift register used for shifting data in or out.
- SSPBUF is the buffer register to which data bytes are written to or read from.
- SSPADD register holds the slave device address when the MSSP is configured in I2C Slave mode.
- When the MSSP is configured in Master mode, the lower seven bits of SSPADD act as the Baud Rate Generator reload value.
- In receive operations, SSPSR and SSPBUF together create a double-buffered receiver.
- When SSPSR receives a complete byte, it is transferred to SSPBUF and the SSPIF interrupt is set.
- During transmission, the SSPBUF is not doublebuffered. A write to SSPBUF will write to both SSPBUF and SSPSR.
SSPCON1: MSSP CONTROL REGISTER 1 (I2C Mode)
Write Collision Bit
Receive Overflow Indicator bit
Master Synchronous Serial Port Enable bit
1 = Enables the serial port and configures the SDA and SCL pins as the serial port pins
SCK Release Control bit
Master Synchronous Serial Port Mode Select bits
1111 = I2C Slave mode, 10-bit address with Start and Stop bit interrupts enabled
MSSP Control Register 2 (SSPCON2); MASTER MODE
General Call Enable bit (Slave mode only)
Acknowledge Status bit (Master Transmit mode only)
Acknowledge Data bit (Master Receive mode only)
Acknowledge Sequence Enable bit
Receive Enable bit (Master Receive mode only)
Stop Condition Enable bit
Repeated Start Condition Enable bit
Start Condition Enable/Stretch Enable bit
1 = Initiate Start condition. Automatically cleared by hardware.
SSPSTAT: MSSP STATUS REGISTER (I2C MODE)
Slew Rate Control bit
SMBus Select bit
Read/Write Information bit
In Slave mode: 1 = Read
In Master mode:
Update Address bit (10-Bit Slave mode only)
Buffer Full Status bit
In Transmit mode:
In Receive mode:
SSPADD register holds the slave device address when the MSSP is configured in I2C Slave mode. When the MSSP is configured in Master mode, the lower seven bits of SSPADD act as the Baud Rate Generator reload value
|-||Baud Rate Generator Reload Value|
I2C Master mode
SSPM3: SSPMO = 1000 (SSPCON1)
I2C Clock = Fosc/(4 +(SSPADD+1))
Case 1: For Fosc = 48 Mhz and I2C Clock = 100Khz
SSPADD = ((Fosc/(4*I2C Clock)) - 1
SSPADD = ((48 MHz / (4*100 khz )) - 1
therefor SSPADD = (119) in decimal = (77) in Hex
Case 2: For Fosc = 20 Mhz and I2C Clock = 100Khz
SSPADD = ((Fosc/(4*I2C Clock)) - 1
SSPADD = ((20 Mhz /(4*100khz )) - 1
therefor SSPADD = (49) in decimal = (31) in hex
- Created and Developed by Prof Sujit Wagh, Sinhgad's SKNCOE, Pune
- WikiNote Foundation