2.3.2 How to Configure EUSART2

EUSART2 will be configured for 9600 baud rate and the standard 8-N-1 (eight data bits, no parity bit and one Stop bit) frame format.

Given the configured frequency and the desired baud rate of 9600, the 16-bit Baud Rate Generator (BRG16) must be used, and High Baud Rate (BRGH) must be enabled. The SPEN bit enables the Serial Port (EUSART) as a whole, while TXEN enables its transmitter.

/* Transmit Enable */
TX2STAbits.TXEN = 1;
/* High Baud Rate Select */
TX2STAbits.BRGH = 1;
/* 16-bit Baud Rate Generator is used */
BAUD2CONbits.BRG16 = 1;
/* Serial Port Enable */
RC2STAbits.SPEN = 1;

The value to be written in SP2BRG is found in the table below, taken from the device data sheet.

Table 2-1. Sample Baud Rates for Asynchronous Modes
BAUD
 RATESYNC = 0, BRGH = 1, BRG16 = 1 or SYNC = 1, BRG16 = 1
FOSC = 8.000 MHzFOSC = 4.000 MHzFOSC = 3.6864 MHzFOSC = 1.000 MHz
Actual
 Rate%
 ErrorSPBRG
 value
 (decimal)Actual
 Rate%
 ErrorSPBRG
 value
 (decimal)Actual
 Rate%
 ErrorSPBRG
 value
 (decimal)Actual
 Rate%
 ErrorSPBRG
 value
 (decimal)
300300.00.006666300.00.013332300.00.003071300.10.04832
12001200-0.02166612000.0483212000.0076712020.16207
240024010.0483223980.0841624000.0038324040.16103
960096150.1620796150.1610396000.009596150.1625
10417104170191104170.0095104730.5387104170.0023
19.2k19.23k0.1610319.23k0.165119.20k0.004719.23k0.1612
57.6k57.14k-0.793458.82k2.121657.60k0.0015
115.2k117.6k2.1216111.1k-3.558115.2k0.007
/* Baud rate 9600 */
SP2BRGL = 25;
SP2BRGH = 0;