09 Communication Modules
09 Communication Modules
PIC16F877
Introduction
The PIC16F877 has two serial communication modules. These are the universal synchronous asynchronous
receiver transmitter (USART) and the master synchronous serial port (MSSP). The USART module is also
known as the Serial Communication Interface (SCI).
TSR and RSR are not accessible by the user. TSR is used to shift data bits out as data is being transmitted out.
RSR is used to shift in data bits as data is being received. Following completion of a reception session,
received data is stored in the receive buffer register RCREG. To initiate transmission of data, data is written
to the transmit data buffer, TXREG.
62
Reception of data can raise an interrupt. Likewise, end of a transmission session can raise an interrupt.
Operation of the USART is controlled through control registers RCSTA (mainly for receiver part) and TXSTA
(mainly for the transmit part). Baud rate is generated from the value written in the register SPBRG.
The USART can be configured to make the PIC an asynchronous receiver transmitter, a synchronous master
receiver/transmitter or a synchronous slave receiver/transmitter.
Associated Registers
TXREG (address 19h)
It is the buffer register to which is written to initiate a transmission of data.
63
1 – Overrun error (can be cleared by clearing bit CREN)
0 – No overrun error
RX9D: 9th bit of Received Data (can be parity bit but must be calculated by user firmware)
64
INTCON (addresses 0Bh, 8Bh, 108Bh and 18Bh) contains the global interrupt enable bits GIE and
PEIE, that also affect the operation of the USART interrupts.
The MSSP essentially consists of a transmit/receive shift register (SSPSR) and a buffer register (SSPBUF).
Figure 9.2 shows the basic structure of the MSSP.
Figure 9.2
The SSPSR is not accessible by the user and is used to shift data in or out one bit at a time during a serial
transmission session.
For reception, when the SSPSR has received a complete byte, the byte is transferred to the SSPBUF where it
can be accessed by the user. SSPSR and SSPBUF, thus, together create a double-buffered receiver that allows
another byte to be received while the previous one is held in the SSPBUF.
65
For transmission, the byte to be transmitted is written to the SSPBUF. Writing to the SSPBUF automatically
writes the same byte to the SSPSR so that transmission may begin if all necessary conditions are satisfied.
Transmission is not double-buffered.
Completion of reception or transmission of byte can raise an interrupt called the SSP interrupt.
Operation of the MSSP is controlled by three control registers namely SSPSTAT, SSPCON1 and SSPCON2. The
lowest 4 bits in SSPCON1 are used to select the mode (SPI or I2C) and the baud rate (when operating as the
master). The roles of the other bits in SSPCON and the bits in SSPSTAT depend on whether SPI or I2C mode is
selected. SSPCON2 is used only in the I2C mode. An address register, SSPADD is available for use in the I2C
mode. SSPADD is used to hold the slave address when the PIC is in slave mode. In the master mode, SSPADD
is used generate the baud rate.
66
SSPOV: Receive overflow indicator bit
o 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
o Note: In Master mode, the overflow bit is not set, since each new reception (and
transmission) is initiated by writing to the SSPBUF register.
WCOL: Write collision detect bit (transmit mode only)
o 1 – The SSPBUF register is written while it is still transmitting the previous word. (Must be
cleared in software.)
o 0 – No collision
Any serial port function that is not desired may be overridden by programming the corresponding data
direction (TRIS) register to the opposite value.
67
INTCON (addresses 0Bh, 8Bh, 108Bh and 18Bh) contains the global interrupt enable bits GIE and
PEIE, that also affect the operation of the SSP interrupt.
When the slave mode is receiving only, the SDO can be disabled by setting it as an input.
Notes
1. A write to SSPBF during transmission of a byte is ignored. The SSPBF is not changed and WCOL is set.
2. Data reception before last received value in SSPBF is read is ignored. The received data is lost and
the SSPOV is set.
3. SPI in mast mode is halted when sleep mode is entered. SPI in slave mode continues to operate and
causes a wakeup whenever data is received.
68
Figure 9.3
The MSSP module in I2C mode provides interrupts on start and stop bits in hardware to determine a free bus
(multi-master function).
69
0110 – I2C Slave mode, 7-bit address
Note: Bit combinations not specifically listed here are either reserved or implemented in SPI mode
only.
CKP: SCK Release Control bit
In Slave mode:
o 1 – Release clock
o 0 – Holds clock low (clock stretch). Used to ensure data setup time is met.
In Master mode, it not used.
SSPEN: Synchronous serial port enable bit
1 – Enables serial port and configures SDA and SCL as serial port pins
0 – Disables serial port and configures these pins as I/O port pins
Note: When enabled, SDA and SCL pins must be properly configured as input or output.
SSPOV: Receive overflow indicator bit
In Receive mode:
o 1 – A new byte is received while the SSPBUF register is still holding the previous data. (Must
be cleared in software.)
o 0 – No overflow
In Transmit mode, this is a “don’t care” bit in Transmit mode.
WCOL: Write collision detect bit
In master transmit mode:
o 1 – The SSPBUF register is attempted while conditions were not valid for a transmission to
be started. (Must be cleared in software.)
o 0 – No collision
In slave mode:
o 1 – The SSPBUF register is written while it is still transmitting the previous word. (Must be
cleared in software.)
o 0 – No collision
In master receive mode, the bit is a “don’t care”.
70
1 – Initiate Acknowledge sequence on SDA and SCL pins and transmit ACKDT data bit. Automatically
cleared by hardware.
0 – Acknowledge sequence Idle
RCEN: Receive Enable bit (Master mode only)
1 – Enables Receive mode for I2C
0 – Receive Idle
PEN: Stop Condition Enable bit (Master mode only)
1 – Initiate Stop condition on SDA and SCL pins. Automatically cleared by hardware.
0 – Stop condition Idle
RSEN: Repeated Start Condition Enabled bit (Master mode only)
1 – Initiate Repeated Start condition on SDA and SCL pins. Automatically cleared by hardware.
0 – Repeated Start condition Idle
SEN: Start Condition Enabled/Stretch Enabled bit
In Master mode:
o 1 – Initiate Start condition on SDA and SCL pins. Automatically cleared by hardware.
o 0 – Start condition Idle
In Slave mode:
o 1 – Clock stretching is enabled for both slave transmit and slave receive (stretch enabled)
o 0 – Clock stretching is enabled for slave transmit only (PIC16F87X compatibility)
71
Note: This bit is cleared on Reset and when SSPEN is cleared.
R/W: Read/Write bit information (I2C mode only)
In Slave mode:
o 1 – Read
o 0 – Write
o Note: This bit holds the R/W bit information following the last address match. This bit is only
valid from the address match to the next Start bit, Stop bit or not ACK bit.
In Master mode:
o 1 – Transmit is in progress.
o 0 – Transmit is not in progress.
o Note: ORing this bit with SEN, RSEN, PEN, RCEN or ACKEN will indicate if the MSSP is in Idle
mode.
UA: Update Address (10-bit Slave mode only)
1 – Indicates that the user needs to update the address in the SSPADD register.
0 – Address does not need to be updated.
BF: Buffer Full Status bit
In Transmit mode:
o 1 – Receive complete, SSPBUF is full.
o 0 – Receive not complete, SSPBUF is empty.
In Receive mode:
o 1 – Data Transmit in progress (does not include the ACK and Stop bits), SSPBUF is full.
o 0 – Data Transmit complete (does not include the ACK and Stop bits), SSPBUF is empty.
TRISC
Some of the pins used for SPI communication need their directions to be set as follows:
SCL must have TRISC<3> bit set.
SDA must have TRISC<4> bit set.
The MSSP module will override the input state with the output data when required (for slave-transmitter).
To ensure proper operation of the module, pull-up resistors must be provided externally to the SCL and SDA
pins.
72
4. To the SSPADD
Write the slave address when device is to be in slave mode.
Write a value to generate the required baud rate. The baud rate is given by the formulae
𝐹𝑂𝑆𝐶
𝑏𝑎𝑢𝑑 𝑟𝑎𝑡𝑒 = 4(𝑆𝑃𝐴𝐷𝐷 + 1)
5. Using TRISC, configure port pin directions of SCL and SDA as required for I2C operation.
If 10-bit address mode is used, the address is received in two bytes. The first byte carries the information
indicating that 10-bit address is used and the two MSBs of the address. The high byte is if the format
‘11110 A9 A8 0’, where ‘A9’ and ‘A8’ are the two MSBs of the address and the upper 5-bit pattern
indicates use of a 10-bit address. The SSPADD of the intended slave initially contains the high byte of a 10-bit
address. When a match of the high byte occurs, the received address is stored in SSPBUF. The slave SSPBUF
can then be read and the SSPADD is updated with the low byte. If the low byte matches in the next
reception, the received low address is stored in SSPBUF. The SSPBUF can then be read so that data reception
can begin.
73
Operation of the master side
The master device generates all of the serial clock pulses and the start and stop conditions. A transfer is
started using a start condition and is ended using a stop or a repeated start condition. Serial data is
transmitted 8 bits at a time. After each byte is transmitted, an acknowledge bit is received.
The first byte transmitted contains the slave address of the receiving device (7 bits) and the Read/Write
(R/W) bit.
The baud rate generator used for the SPI mode operation is used to set the SCL clock frequency for either
100 kHz, 400 kHz or 1 MHz I2C operation.
Tutorial Questions
1. Develop C programs to perform the following:
a) Set a PIC as a synchronous USART master and send or receive data.
b) Set a PIC as a synchronous USART slave and receive or transmit data.
c) Set a PIC as an asynchronous USART and receive or transmit data.
74