The Enhanced Universal Synchronous Asynchronous Receiver Transmitter (EUSART) module is a serial I/O communication peripheral. It is also known as Serial Communications Interface (SCI). It contains all clock generators, shift registers and data buffers necessary to perform an input or output serial data transfer independently of the device program execution. As its name states, apart from the usage of clock for synchronization, this module can also establish asynchronous connection, which makes it irreplaceable in some applications.
For example, in the event that it is difficult or impossible to provide special channels for clock and data transfer (for example, radio remote control or infrared), the EUSART module presents itself as a convenient solution.
Fig. 6-1 Remote Control and Plane
The EUSART system integrated into the PIC16F887 microcontroller has the following features:
The EUSART transmits and receives data using standard non-return-to-zero (NRZ) format. As seen in figure 6-2 below, this mode does not use clock signal, while the data format being transferred is very simple:
Fig. 6-2 EUSART Asynchronous Mode
Briefly, each data is transferred in the following way:
Fig. 6-3 EUSART Asynchronous Transmitter
In order to enable data transmission via EUSART module, it is necessary to configure it to operate as a transmitter. In other words, it is necessary to define the state of the following bits:
TXEN = 1 - EUSART transmitter is enabled by setting this bit of the TXSTA register;
SYNC = 0 - EUSART is configured to operate in asynchronous mode by clearing this bit of the TXSTA register; and
SPEN = 1 - By setting this bit of the RCSTA register, EUSART is enabled and the TX/CK pin is automatically configured as output. If this bit is simultaneously used for some analog function, it must be disabled by clearing the corresponding bit of the ANSEL register.
The central part of the EUSART transmitter is the shift register TSR which is not directly accessible by the user. In order to start transmission, the module must be enabled by setting the TXEN bit of the TXSTA register. Data to be sent should be written to the TXREG register, which will cause the following sequence of events:
Sending 9-bit data is enabled by setting the TX9 bit of the TXSTA register. The TX9D bit of the TXSTA register is the ninth and Most Significant data bit. When transferring 9-bit data, the TX9D data bit must be written before writing the 8 least significant bits into the TXREG register. All nine bits of data will be transferred to the TSR shift register immediately after the TXREG write is complete.
Fig. 6-4 EUSART Asynchronous Receiver
Similar to the activation of EUSART transmitter, in order to enable the receiver it is necessary to define the following bits:
CREN = 1 - EUSART receiver is enabled by setting this bit of the RCSTA register;
SYNC = 0 - EUSART is configured to operate in asynchronous mode by clearing this bit stored in the TXSTA register; and
SPEN = 1 - By setting this bit of the RCSTA register, EUSART is enabled and the RX/DT pin is automatically configured as input. If this bit is simultaneously used for some analog function, it must be disabled by clearing the corresponding bit of the ANSEL register.
When this first and necessary step is accomplished and START bit is detected, data is transferred to the shift register RSR through the RX pin. When the STOP bit has been received, the following occurs:
There are two types of errors which the microcontroller can automatically detect. The first one is called Framing error and occurs when the receiver does not detect the STOP bit at the expected time. Such error is indicated via the FERR bit of the RCSTA register. If this bit is set, it means that the last received data may be incorrect. It is important to know several things:
Another type of error is called Overrun Error. The receive FIFO can hold two characters. An overrun error will be generated if the third character is received. Simply, there is no space for another one byte and an error is unavoidable! When this happens the OERR bit of the RCSTA register is set. The consequences are the following:
In addition to receiving standard 8-bit data, the EUSART system supports 9-bit data reception. On the transmit side, the ninth bit is "attached" to the original byte just before the STOP bit. On the receive side, when the RX9 bit of the RCSTA register is set, the ninth data bit will be automatically written to the RX9D bit of the same register. When this byte is received, one should take care of how to read its bits- the RX9D data bit must be read before reading the 8 least significant bits of the RCREG register. Otherwise, the ninth data bit will be automatically cleared.
Fig. 6-5 Receiving 9-bit Data
When the ADDEN bit of the RCSTAregister is set, the EUSART module is able to receive only 9-bit data, whereas all 8-bit data will be ignored. Although it seems like a restriction, such modes enable serial communication between several microcontrollers. The principle of operation is simple. The master device sends 9-bit data which represents the address of one microcontroller. All slave microcontrollers sharing the same transmission line, receive this data. Of course, each of them must have the ADDEN bit set because it enables address detection.

Upon receiving this data each slave checks if that address matches its own. Software, in which address match occurs, must disable address detection by clearing its ADDEN bit. The master device keeps on sending 8-bit data. All data passing through the transmission line will be received by "recognized" EUSART module only. Upon receiving the last byte, the slave device should set the ADDEN bit in order to enable new address detection.
Fig. 6-7 Sending Data
Fig.6-8 TXSTA Register
CSRC - Clock Source Select bit - determines clock source. It is used only in synchronous mode.
TX9 - 9-bit Transmit Enable bit
TXEN - Transmit Enable bit
SYNC - EUSART Mode Select bit
SENDB - Send Break Character bit is only used in asynchronous mode and only in case it is required to observe LIN bus standard.
BRGH - High Baud Rate Select bit determines baud rate in asynchronous mode. It does not affect EUSART in synchronous mode.
TRMT - Transmit Shift Register Status bit
TX9D - Ninth bit of Transmit Data can be used as address or parity bit.
Fig.6-9 RCSTA Register
SPEN - Serial Port Enable bit
RX9 - 9-bit Receive Enable bit
SREN - Single ReceiveEnable bit is used only in synchronous mode when the microcontroller operates as master.
CREN - Continuous Receive Enable bit acts differently depending on EUSART mode.
Asynchronous mode:
Synchronous mode:
ADDEN - Address Detect Enable bit is only used in address detect mode.
FERR - Framing Error bit
OERR - Overrun Error bit.
RX9D - Ninth bit of Received Data can be used as address or parity bit.
If you carefully look at the asynchronous EUSART receiver or transmitter diagram, you will see, in both cases, that clock signal from the local timer BRG is used for synchronization. The same clock source is also used in synchronous mode.
This timer consists of two 8-bit registers comprising one 16-bit register.
Fig. 6-10 EUSART Baud Rate Generator (BRG)
A number written to these two registers determines the baud rate. Besides, both the BRGH bit of the TXSTA register and the BRGH16 bit of the BAUDCTL register affect clock frequency.
The formula used to determine Baud Rate is given in the table below.
| Bits | BRG / EUSART Mode | Baud Rate Formula | ||
|---|---|---|---|---|
| SYNC | BRG1G | BRGH | ||
| 0 | 0 | 0 | 8-bit / asynchronous | Fosc / [64 (n + 1)] |
| 0 | 0 | 1 | 8-bit / asynchronous | Fosc / [16 (n + 1)] |
| 0 | 1 | 0 | 16-bit / asynchronous | Fosc / [16 (n + 1)] |
| 0 | 1 | 1 | 16-bit / asynchronous | Fosc / [4 (n + 1)] |
| 1 | 0 | X | 8-bit / asynchronous | Fosc / [4 (n + 1)] |
| 1 | 1 | X | 16-bit / asynchronous | Fosc / [4 (n + 1)] |
Table 6-1 Baud Rate
The following tables contain values that should be written to the 16-bit register SPBRG and assigned to the SYNC, BRGH and BRGH16 bits in order to obtain some of the standard baud rates.
The formulas used to determine the Baud Rate are:









Table 6-2 Determining Baud Rate
Fig. 6-11 BAUDCTL Register
ABDOVF - Auto-Baud Detect Overflow bit is only used in asynchronous mode during baud rate detection.
RCIDL - Receive Idle Flag bit is only used in asynchronous mode.
SCKP - Synchronous Clock Polarity Select bit acts differently depending on EUSART mode.
Asynchronous mode:
Synchronous mode:
WUE Wake-up Enable bit
ABDEN - Auto-Baud Detect Enable bit is used in asynchronous mode only.
Sending data via asynchronous EUSART communication:
Receiving data via asynchronous EUSART communication:
Setting Address Detection Mode:
MSSP module (Master Synchronous Serial Port) is a very useful, but at the same time one of the most complex circuit within the microcontroller. It enables high speed communication between a microcontroller and other peripherals or microcontroller devices by using few input/output lines (maximum two or three). Therefore, it is commonly used to connect the microcontroller to LCD displays, A/D converters, serial EEPROMs, shift registers etc. The main feature of this type of communication is that it is synchronous and suitable for use in systems with a single master and one or more slaves. A master device contains the necessary circuitry for baud rate generation and supplies the clock for all devices in the system. Slave devices may in that way eliminate the internal clock generation circuitry. The MSSP module can operate in one of two modes:
As seen in figure 6-12 below, one MSSP module represents only a half of the hardware needed to establish serial communication, while another half is stored in the device the data is exchanged with. Even though the modules on both ends of the line are the same, their modes are essentially different depending on whether they operate as a Master or a Slave:
If the microcontroller to be programmed controls another device or circuit (peripherals), it should operate as a master device. A module defined as such will generate clock when needed, i.e. only when data receive and transmit is required by the software. It depends on the master whether the connection will be established or not. Otherwise, if the microcontroller to be programmed is a part of some peripheral which belongs to some more complex device (for example PC), then it should operate as a slave device. As such, it always has to wait for request for data transfer from master device.
Fig.6-12 MSSP Module
The SPI mode allows 8 bits of data to be transmitted and received simultaneously using 3 input/output lines:
In addition to these three lines, if the microcontroller exchanges data with several peripheral devices, the forth line (SS) may be also used. Refer to figure 6-13 below.
SS - Slave Select - is additional pin used for specific device selection. It is active only in case the microcontroller is in slave mode, i.e. when the external - master device requires data exchange.
When operating in SPI mode, MSSP module uses in total of 4 registers:
The first three registers are writable/readable and can be changed at any moment, while the forth register, since not available, is used for converting data into "serial" format.
Fig. 6-13 SPI Mode
As seen in figure 6-14, the central part of the SPI module consists of two registers connected to pins for receive, transmit and synchronization.
Shift register (SSPRS) is directly connected to the microcontroller pins and used for data transmission in serial format. The SSPRS register has its input and output and shifts the data in and out of device. In other words, each bit appearing on input (receive line) simultaneously shifts another bit toward output (transmit line).
The SSPBUF register (Buffer) is a part of memory used to temporarily hold the data written to the SSPRS until the received data is ready. Upon receiving all 8 bits of data, that byte is moved to the SSPBUF register. 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. Since having been the most accessed, this register is considered the most important from the programmers’ point of view.
Namely, if mode settings are neglected, data transfer via SPI actually means to write and read data from this register, while another "acrobatics" such as moving registers are automatically performed by hardware.
Fig. 6-14 SPI Mode
Prior to initializing the SPI, it is necessary to specify several options:
Step 1.
Data to transmit should be written to the buffer register SSPBUF. Immediately after that, if the SPI module operates in master mode, the microcontroller will automatically perform the following steps 2, 3 and 4. If the SPI module operates as Slave, the microcontroller will not perform these steps until the SCK pin detects clock signal.
Fig. 6-15 Step 1
Step 2.
This data is now moved to the SSPSR register and the SSPBUF register is not cleared.
Fig. 6-16 Step 2
Step 3.
Synchronized with clock signal, this data is shifted to the output pin (MSB bit first) while the register is simultaneously being filled with bits through input pin. In Master mode, the microcontroller itself generates clock, while the Slave mode uses external clock (pin SCK).
Fig. 6-17 Step 3
Step 4.
The SSPSR register is full once the 8 bits of data have been received. It is indicated by setting the BF and SSPIF bits. The received data (that byte) is automatically moved from the SSPSR register to the SSPBUF register. Since data transfer via serial communication is performed automatically, the rest of the program is normally executed while data transfer is in progress. In that case, the function of the SSPIF bit is to generate interrupt when one byte transmission is completed.
Fig. 6-18 Step 4
Step 5.
At last, the data stored in the SSPBUF register is ready for use and moved to any register available.
Fig. 6-19 Step 5
I²C mode (Inter IC Bus) is especially suitable when the microcontroller and integrated circuit, which the microcontroller should exchange data with, are within the same device. It is commonly about another microcontrollers or specialized, cheap integrated circuits belonging to the new generation of so called "smart peripheral components" (memories, temperature sensors, real-time clocks etc.)
Similar to serial communication in SPI mode, data transfer in I²C mode is synchronous and bidirectional. This time only two pins are used for data transfer. These are the SDA (Serial Data) and SCL (Serial Clock) pins. The user must configure these pins as inputs or outputs through the TRISC bits.
Perhaps it is not directly visible. By observing particular rules (protocols), this mode enables up to 122 different components to be simultaneously connected in a simple way by using only two valuable I/O pins. Briefly, everything works as follows: Clock necessary to synchronize the operation of both devices is always generated by the master device (microcontroller) and its frequency directly affects baud rate. There are protocols allowing maximum 3,4 MHz clock frequency (so called high-speed I²C bus), but the clock frequency of the most frequently used protocol is limited to 100 KHz. There is no limit in case of minimal frequency.
When master and slave components are synchronized by the clock, every data exchange is always initialized by master. Once the MSSP module has been enabled, it waits for a Start condition to occur. First the master device sends the START bit (logic zero) through the SDA pin, then the 7-bit address of the selected slave device, and finally, the bit which requires data write (0) or read (1) to that device. Accordingly, following the start condition, the eight bits are shifted into the SSPSR register. All slave devices share the same transmission line and all will simultaneously receive the first byte, but only one of them has the address to match.
Fig. 6-20 Master and Slave Configuration
Once the first byte has been sent (only 8-bit data are transmitted), master goes into receive mode and waits for acknowledgment from the receive device that address match has occurred. If the slave device sends acknowledge data bit (1), data transfer will be continued until the master device (microcontroller) sends the Stop bit.
This is the simplest explanation of how two components communicate. If needed, this microcontroller is able to control more complicated situations when 1024 different components, shared by several different master devices, are connected. Such devices are rarely used in practice and there is no need to discuss them at greater length.
Fig. 6-21 Data Transfer
Figure below shows the block diagram of the MSSP module in I²C mode.
Fig. 6-22 MSSP Block Diagram in I²C Mode
The MSSP module uses six registers for I²C operation. Some of them are shown in figure above:
Fig. 6-23 SSPSTAT Register
SMP Sample bit
SPI master mode - This bit determines input data phase.
SPI slave mode This bit must be cleared when SPI is used in Slave mode.
I²C mode (master or slave)
CKE - Clock Edge Select bit selects synchronization mode.
CKP = 0:
CKP = 1:
D/A - Data/Address bit is used in I²C mode only.
P - Stop bit is used in I²C mode only.
S - Start bit is used in I²C mode only.
R/W - Read Write bit is used in I²C mode only. 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 I²C slave mode
In I²C master mode
UA - Update Address bit is used in 10-bit I²C mode only.
BF Buffer Full Status bit
During data receive (in SPI and I²C modes)
During data transmit (in I²C mode only)
Fig. 6-24 SSPCON Register
WCOL Write Collision Detect bit
SSPOV Receive Overflow Indicator bit
SSPEN - Synchronous Serial Port Enable bit determines the microcontroller pins function and initializes MSSP module:
In SPI mode
In I²C mode
CKP - Clock Polarity Select bit is not used in I²C master mode.
In SPI mode
In I²C slave mode
SSPM3-SSPM0 - Synchronous Serial Port Mode Select bits. SSP mode is determined by combining these bits:
| SSPM3 | SSPM2 | SSPM1 | SSPM0 | Mode |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | SPI master mode, clock = Fosc/4 |
| 0 | 0 | 0 | 1 | SPI master mode, clock = Fosc/16 |
| 0 | 0 | 1 | 0 | SPI master mode, clock = Fosc/64 |
| 0 | 0 | 1 | 1 | SPI master mode, clock = (output TMR)/2 |
| 0 | 1 | 0 | 0 | SPI slave mode, SS pin control enabled |
| 0 | 1 | 0 | 1 | SPI slave mode, SS pin control disabled, SS can be used as I/O pin |
| 0 | 1 | 1 | 0 | I²C slave mode, 7-bit address used |
| 0 | 1 | 1 | 1 | I²C slave mode, 10-bit address used |
| 1 | 0 | 0 | 0 | I²C master mode, clock = Fosc / [4(SSPAD+1)] |
| 1 | 0 | 0 | 1 | Mask used in I²C slave mode |
| 1 | 0 | 1 | 0 | Not used |
| 1 | 0 | 1 | 1 | I²C controlled master mode |
| 1 | 1 | 0 | 0 | Not used |
| 1 | 1 | 0 | 1 | Not used |
| 1 | 1 | 1 | 0 | I²C slave mode, 7-bit address used,START and STOP bits enable interrupt |
| 1 | 1 | 1 | 1 | I²C slave mode, 10-bit address used,START and STOP bits enable interrupt |
Table 6-3 Synchronous Serial Port Mode Select Bits
Fig. 6-25 SSPCON2 Register
GCEN - General Call Enable bit
In I²C slave mode only
ACKSTAT - Acknowledge Status bit
In I²C Master Transmit mode only
ACKDT - Acknowledge data bit
In I²C Master Receive mode only
ACKEN - Acknowledge condition Enable bit
In I²C Master Receive mode
RCEN - Receive Enable bit
In I²C Master mode only
PEN - STOP condition Enable bit
In I²C Master mode only
RSEN - Repeated START Condition Enabled bit
In I²C master mode only
SEN - START Condition Enabled/Stretch Enabled bit
In I²C Master mode only
The most common case is when the microcontroller operates as a master and the peripheral component as a slave. This is why this book covers just this mode. It is also considered that the address consists of 7 bits and device contains only one microcontroller (one master device).
In order to enable MSSP module in this mode, it is necessary to do the following:
Set baud rate (SSPADD register), turn off slew rate control (by setting the SMP bit of the SSPSTAT register) and select master mode (SSPCON register). After the preparation has been finished and module has been enabled (SSPCON register: SSPEN bit), one should wait for internal electronics to signal that everything is ready for data transmission, i.e. the SSPIF bit of the PIR1 register is set.
This bit should be cleared by software and after that, the microcontroller is ready to start "communication" with peripherals.
Fig. 6-27 I²C in Master Mode
Each clock condition on the SDA pin starts with logic zero (0) which appears upon setting the SEN bit of the SSPCON2 register. Even enabled, the microcontroller has to wait a certain time before it starts communication. It is the so called "Start condition" during which internal preparations and checks are performed. If all conditions are met, the SSPIF bit of the PIR1 is set and data transfer starts as soon as the SSPBUF register is loaded.
Since maximum 112 integrated circuits may simultaneously share the same transmission line, the first data byte must contain address which matches only one slave device. Each component has its own address listed in the proper data sheet. The eighth bit of the first data byte specifies direction of data transmission, the microcontroller is to send or receive data. In this case, it is all about data receive and the eighth bit therefore is logic zero (0).
Fig. 6-28 Data Transmission in I²C Master Mode
When address match occurs, the microcontroller has to wait for the acknowledge data bit. The slave device acknowledges address match by clearing the ASKSTAT bit of the SSPCON2 register. If the match properly occurred, all bytes representing data are transmitted in the same way.
Data transmission ends by setting the SEN bit of the SSPCON2 register. The so called STOP condition occurs, which enables the SDA pin to receive pulse condition: Start - Address - Acknowledge - Data - Acknowledge ....Data - Acknowledge - Stop!
Fig.6-29 Data Transmission in I²C Master Mode
Preparations for data reception are similar to those for data transmission, with exception that the last bit of the first sent byte (containing address) is logic one (1). It specifies that master expects to receive data from addressed slave device. With regard to the microcontroller, the following events occur:
After internal preparations are finished and START bit is set, slave device starts sending one byte at a time. These bytes are stored in the serial register SSPSR. Each data is, after receiving the last eighth bit, loaded to the SSPBUF register from where it can be read. By reading this register, the acknowledge bit is automatically sent, which means that master device is ready to receive new data.
At the end, similar to data transmission, data reception ends by setting the STOP bit:
Fig. 6-30 Data Reception in I²C Master Mode
Start - Address - Acknowledge - Data - Acknowledge .... Data - Acknowledge - Stop!
In this pulse condition, the acknowledge bit is sent to slave device.
In order to synchronize data transmission, all events taking place on the SDA pin must be synchronized with the clock generated in master device. This clock is generated by a simple oscillator whose frequency depends on the microcontroller’s main oscillator frequency, value written to the SSPADD register and the current SPI mode.
The clock frequency of the mode described in this book depends on selected quartz crystal and the SPADD register. The formula used to calculate it is shown in figure below.
Fig. 6-31 Baud Rate Generator
When the microcontroller communicates with peripheral components, it may happen that data transfer fails for some reason. In that case, it is recommended to check the status of some bits which can clarify the problem. In practice, the state of these bits is checked by executing a short subroutine after each byte transmission and reception (just in case).
WCOL (SPCON,7) - If you try to write a new data to the SSPBUF register while another data transmit/receive is in progress, the WCOL bit will be set and the contents of the SSPBUF register remains unchanged. Write does not occur. After this, the WCOL bit must be cleared in software.
BF (SSPSTAT,0) - In transmit mode, this bit is set when the CPU writes to the SSPBUF register and remains set until the byte in serial format is shifted from the SSPSR register. In receive mode, this bit is set when data or address is loaded to the SSPBUF register. It is cleared when the SSPBUF register is read.
SSPOV (SSPCON,6) - In receive mode, this bit is set when a new byte is received by the SSPSR register via serial communication, whereas the previously received data has not been read from the SSPBUF register yet.
SDA and SCL Pins - When SPP module is enabled, these pins turns into Open Drain outputs. It means that these pins must be connected to the resistors which, at the other end, are connected to positive power supply.
Fig. 6-32 Open Drain Output Resistors
In order to establish serial communication in I²C mode, the following should be done: