1.8 Getting Started with UART

1.8.1 Introduction

Author: Alexandru Niculae, Microchip Technology Inc.

The UART-capable peripherals come in different variants on microcontrollers. Sometimes, the peripheral is named UART or USART and sometimes it is called EUSART to emphasize enhanced functionalities. The data sheet of each device shows the type of UART peripheral it has.

The purpose of this document is to describe how to configure the Enhanced Universal Synchronous Asynchronous Receiver Transmitter (EUSART) on PIC18 devices to demonstrate its usage for some common use cases.

Note:
  • For each use case, there are three different implementations, which have the same functionalities: one generated with MPLAB® Code Configurator (MCC), one generated using the Foundation Services MCC Library and one bare metal.
  • The MCC generated code offers hardware abstraction layers that ease the use of the code across different devices of the same family. The Foundation Services generated code offers a driver-independent Application Programming Interface (API) and facilitates the portability of code across different platforms. The bare metal code is easier to follow and allows a fast ramp-up on the use case associated code.

While EUSART is a complex peripheral and can work in various modes, this document will use it in Asynchronous mode and describes the following use cases:

  • Send ‘Hello World’:

    This example shows how to send a string to the PC and see it in the MPLAB® Data Visualizer Terminal.

  • Send Formatted Messages Using printf:

    This example shows how to enhance the first use case with the ability to use the printf function to send messages over EUSART. In this example, messages are Data Stream protocol frames, and the MPLAB Data Visualizer can be used to display them as plots.

  • Receive Control Commands:

    This example shows how to implement a command line interface. This way, the microcontroller can receive control commands via the EUSART. In this use case, an LED is controlled using commands sent from the MPLAB Data Visualizer.

The examples in this technical brief have been developed using the PIC18F47Q10 Curiosity Nano development board, which is equipped with the QFN40 package.

1.8.2 Peripheral Overview

The EUSART module is a serial Input/Output (I/O) communications peripheral. It contains all the clock generators, shift registers and data buffers necessary to perform an input or output serial data transfer, independent of device program execution. The EUSART, also known as a Serial Communications Interface (SCI), can be configured as a full-duplex asynchronous system or half-duplex synchronous system. The Full-Duplex mode is useful for communications with peripheral systems, such as CRT terminals and personal computers. The Half-Duplex Synchronous mode is intended for communications with peripheral devices, such as A/D or D/A integrated circuits, serial EEPROMs or other microcontrollers. These devices typically do not have internal clocks for baud rate generation and require the external clock signal provided by a master synchronous device.

The RXx/DTx and TXx/CKx input pins are selected with the RXxPPS and TXxPPS registers, respectively. TXx, CKx and DTx output pins are selected with each of the pin’s RxyPPS register. Since the RX input is coupled with the DT output in Synchronous mode, it is the user’s responsibility to select the same pin for both of these functions when operating in Synchronous mode. The EUSART module will control the data direction drivers automatically.

Figure 1-53. EUSART Block Diagram

1.8.3 Send ‘Hello World’

This use case demonstrates how to send string messages from the microcontroller to the PC and use MPLAB Data Visualizer to see them. The EUSART will be configured for Asynchronous mode and only the TX pin will be used.

Note:

If a platform without an on-board UART to USB converter is used, there are many options of external UART to USB, such as the MCP2200 and the MCP2221A.

This use case follows the steps:

  • Configure the system clock
  • Configure EUSART2
  • Configure the pins

1.8.3.1 MCC Generated Code

To generate this project using the MPLAB Code Configurator (MCC), follow these steps:

  1. Create a new MPLAB X IDE project .
  2. Open the MCC from the toolbar (information on how to install the MCC plug-in can be found on the Microchip website).
  3. Go to Project Resources > System > System Module and use the following configurations:
    • Oscillator Select: HFINTOSC
    • HF Internal Clock: 4 MHz
    • Clock Divider: 4
    • In the Watchdog Timer Enable field, in the WWDT tab, ensure WDT Disable is selected
    • In the Programming tab, ensure Low-Voltage Programming Enable is checked
  4. From the Device Resources window, add EUSART2 to the project, then use the following configurations:
    • Mode: Asynchronous
    • Enable EUSART: Checked
    • Enable Transmit: Checked
    • Baud Rate: 9600
    • Transmission-bits: 8 bits
    • Reception-bits: 8 bits
    • Data Polarity: Noninverted
  5. Open the Pin Manager > Grid View window, select UQFN40 in the MCU package field and select pin RD0 as EUSART TX.
    Figure 1-54. Pin Mapping
  6. Click Generate in the Project Resources tab.
  7. In the main.c file, which has been generated by MCC:
    • Add the following code in the main function (replacing the existing while(1) loop):
      char msg[] = "Hello World\r\n";
      while (1)
      {
          for(uint8_t i = 0; i < strlen(msg); i++)
          {
              EUSART2_Write(msg[i]);
          }
      }
  8. Use MPLAB X Data Visualizer as described in the appendix, How to Receive Data in MPLAB X Data Visualizer.

1.8.3.2 Foundation Services Generated Code

To generate this project using the Foundation Services library, follow these steps:

  1. Create a new MPLAB X project.
  2. Open the MCC from the toolbar (information on how to install the MCC plug-in can be found on the Microchip website).
  3. Go to Project Resources > System > System Module and use the following configurations:
    • Oscillator Select: HFINTOSC
    • HF Internal Clock: 4 MHz
    • Clock Divider: 4
    • In the WDT Enable field, in the WWDT tab, ensure WDT Disable is selected
    • In the Programming tab, ensure Low-Voltage Programming Enable is checked
  4. In Device Resources under Libraries, find Foundation Services, expand it and double-click UART to add it to the project.
  5. Click the + button at the bottom to add a new Foundation Services federated UART.
  6. Select EUSART2 from the UART drop down in Foundation Services.
  7. Open the Pin Manager > Grid View window, select UQFN40 in the MCU package field and select pin RD0 EUSART TX.
    Figure 1-55. Pin Mapping
  8. Click Generate in the Project Resources tab.
  9. In the main.c file, which was generated by MCC:
    • Uncomment the line that enables global interrupts and the one that enables peripheral interrupts
    • Add the following code in the main function (replacing the existing while(1) loop):
      char msg[] = "Hello world\r\n";
      while (1)
      {
          for(uint8_t i = 0; i < strlen(msg); i++)
          {
              uart[UART0].Write(msg[i]);
          }
      }
  10. Use MPLAB X Data Visualizer as described in the appendix, How to Receive Data in MPLAB Data Visualizer.

1.8.3.3 Bare Metal Code

The first step is to configure the microcontroller to disable the Watchdog Timer (WDT) and to enable the Low-Voltage Programming (LVP).

/* WDT operating mode → WDT Disabled */
#pragma config WDTE = OFF
/* Low-voltage programming enabled, RE3 pin is MCLR */
#pragma config LVP = ON

1.8.3.3.1 How to Configure the System Clock

To use the High-Frequency Internal Oscillator (HFINTOSC) at 1 MHz, two settings must be made in the registers. First, select HFINTOSC as the oscillator source in the OSCCON1 register by writing to the NOSC bit field:

/* Set HFINTOSC as new oscillator source. */
OSCCON1bits.NOSC = 0b110;

Then, select the nominal frequency of 1 MHz in the OSCFRQ register:

/* Set HFFRQ to 1 MHz. */
OSCFRQbits.HFFRQ = 0;

1.8.3.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 1-8 1-11 1-15. Sample Baud Rates for Asynchronous Modes
BAUD
 RATE SYNC = 0, BRGH = 1, BRG16 = 1 or SYNC = 1, BRG16 = 1
FOSC = 8.000 MHz FOSC = 4.000 MHz FOSC = 3.6864 MHz FOSC = 1.000 MHz
Actual
 Rate %
 Error SPBRG
 value
 (decimal) Actual
 Rate %
 Error SPBRG
 value
 (decimal) Actual
 Rate %
 Error SPBRG
 value
 (decimal) Actual
 Rate %
 Error SPBRG
 value
 (decimal)
300 300.0 0.00 6666 300.0 0.01 3332 300.0 0.00 3071 300.1 0.04 832
1200 1200 -0.02 1666 1200 0.04 832 1200 0.00 767 1202 0.16 207
2400 2401 0.04 832 2398 0.08 416 2400 0.00 383 2404 0.16 103
9600 9615 0.16 207 9615 0.16 103 9600 0.00 95 9615 0.16 25
10417 10417 0 191 10417 0.00 95 10473 0.53 87 10417 0.00 23
19.2k 19.23k 0.16 103 19.23k 0.16 51 19.20k 0.00 47 19.23k 0.16 12
57.6k 57.14k -0.79 34 58.82k 2.12 16 57.60k 0.00 15
115.2k 117.6k 2.12 16 111.1k -3.55 8 115.2k 0.00 7
/* Baud rate 9600 */
SP2BRGL = 25;
SP2BRGH = 0;

1.8.3.3.3 How to Configure the Pins

Since only the transmission is necessary for this use case, only the TX pin must be configured. The pin used as TX in this example, is the RD0 pin and it must be configured using the Peripheral Pin Select (PPS). To find the value that needs to be written to the RD0PPS register, inspect the Peripheral PPS Output Selection Codes table below, taken from the device data sheet.

Table 1-9 1-12 1-16. Peripheral PPS Output Selection Codes
RxyPPSPin Rxy Output SourcePORT to Which Output can be Directed
28-Pin Devices40-Pin Devices
0x0CEUSART2 (DT)BCBD
0x0BEUSART2 (TX/CK)BCBD
0x0AEUSART1 (DT)BCBC
0x09EUSART1 (TX/CK)BCBC

The following line will route the EUSART2 TX to RD0:

/* RD0 is TX2 */
RD0PPS = 0x0B;

The pin direction is set by default as output, but if it were not, the following line sets it.

/* Configure RD0 as output. */
TRISDbits.TRISD0 = 0;
Table 1-10. Pin Locations
FunctionPin
EUSART2 TXRD0

Before sending data, the user needs to check if the previous transmission is complete by checking the PIR3.TXnIF bit field. The following code example waits until the transmit buffer is empty, then writes a character to the TXnREG register:

static void EUSART2_write(uint8_t txData)
{
    while(0 == PIR3bits.TX2IF)
    {
        ;
    }

    TX2REG = txData; 
}  

Use MPLAB X Data Visualizer as described in the appendix, How to Receive Data in MPLAB X Data Visualizer.

1.8.4 Send Formatted Messages Using printf

It is a common use case for an application to send a message with variable fields over EUSART, such as when the application reports a sensor reading. Using formatted messages is a very flexible approach and reduces the number of code lines. This can be accomplished by redirecting Standard Input/Output (STDIO) to EUSART.

In this example, a counter value and twice its value are sent in a binary format, using the Data Stream Protocol, over EUSART. The MPLAB X Data Visualizer has a built-in Data Stream Protocol decoder and can display charts of the values in the message in real time.

This use case follows the steps:

  • Configure the system clock
  • Configure EUSART2
  • Configure the pins

1.8.4.1 MCC Generated Code

To generate this project using the MCC, follow these steps:

  1. Create a new MPLAB X IDE project.
  2. Open the MCC from the toolbar (information on how to install the MCC plug-in can be found on the Microchip website).
  3. Go to Project Resources > System > System Module and use the following configurations:
    • Oscillator Select: HFINTOSC
    • HF Internal Clock: 4 MHz
    • Clock Divider: 4
    • In the Watchdog Timer Enable field, in the WWDT tab, ensure WDT Disable is selected
    • In the Programming tab, make sure Low-Voltage Programming Enable is checked
  4. From the Device Resources window, add EUSART2 to the project, then use the following configurations:
    • Mode: Asynchronous
    • Enable EUSART: Checked
    • Enable Transmit: Checked
    • Baud Rate: 9600
    • Transmission-bits: 8 bits
    • Reception-bits: 8 bits
    • Data Polarity: Noninverted
    • Redirect STDIO to USART: Checked
  5. Open Pin Manager > Grid View window, select UQFN40 in the MCU package field and select pin RD0 as EUSART TX.
    Figure 1-56. Pin Mapping
  6. Click Generate in the Project Resources tab.
  7. In the main.c file, which has been generated by MCC:
    • Add the following defines:
      #define START_DATA_STREAM_PROTOCOL 0x03
      #define STOP_DATA_STREAM_PROTOCOL 0xFC
    • Add the following code in the main function (replacing the existing while(1) loop):
      uint8_t cnt = 0;
      while (1)
      {
          printf("%c%c%c%c", START_DATA_STREAM_PROTOCOL, cnt, cnt * 2, STOP_DATA_STREAM_PROTOCOL);
          cnt = cnt + 1;
          __delay_ms(50);
      }
  8. Use MPLAB X Data Visualizer as described in the appendix, How to Configure MPLAB X Data Visualizer to Decode Data Stream Protocol.

1.8.4.2 Foundation Services Generated Code

To generate this project using the Foundation Services library, follow these steps:

  1. Create a new MPLAB X IDE project for PIC18F47Q10.
  2. Open the MCC from the toolbar (information on how to install the MCC plug-in can be found on the Microchip website).
  3. Go to Project Resources > System > System Module and use the following configurations:
    • Oscillator Select: HFINTOSC
    • HF Internal Clock: 4 MHz
    • Clock Divider: 4
    • In the WDT Enable field, in the WWDT tab, ensure WDT Disable is selected
    • In the Programming tab, make sure Low-Voltage Programming Enable is checked
  4. In Device Resource, under Libraries, find Foundation Services, expand it and double-click UART to add it to the project.
  5. Click the + button at the bottom to add a new Foundation Services federated UART.
  6. Select EUSART2 from the UART drop down in Foundation Services.
  7. Open the Pin Manager > Grid View window, select UQFN40 in the MCU package field and select pin RD0 EUSART TX.
    Figure 1-57. Pin Mapping
  8. In Project Resources, under Peripherals, select EUSART2 and use the following configuration:
    • Redirect STDIO to USART: Checked
  9. Click Generate in the Project Resources tab.
  10. In the main.c file, which was generated by MCC:
    • Uncomment the line that enables global interrupts and the one that enables peripheral interrupts
    • Add the following defines:
      #define START_DATA_STREAM_PROTOCOL 0x03
      #define STOP_DATA_STREAM_PROTOCOL 0xFC
    • Add the following code in the main function (replacing the existing while(1) loop):
      uint8_t cnt = 0;
      while (1)
      {
          printf("%c%c%c%c", START_DATA_STREAM_PROTOCOL, cnt, cnt * 2, STOP_DATA_STREAM_PROTOCOL);
          cnt = cnt + 1;
          __delay_ms(50);
      }
  11. Use MPLAB X Data Visualizer as described in the appendix, How to configure MPLAB X Data Visualizer to Decode Data Stream Protocol.

1.8.4.3 Bare Metal Code

The first step will be to configure the microcontroller to disable the WDT and to enable the LVP.

/* WDT operating mode → WDT Disabled */
#pragma config WDTE = OFF
/* Low-voltage programming enabled, RE3 pin is MCLR */
#pragma config LVP = ON

1.8.4.3.1 How to Configure the System Clock

To use the HFINTOSC at 1 MHz, two settings must be made in the registers. First, select HFINTOSC as the oscillator source in the OSCCON1 register by writing to the NOSC bit field:

/* Set HFINTOSC as new oscillator source. */
OSCCON1bits.NOSC = 0b110;

Then, select the nominal frequency of 1 MHz in the OSCFRQ register:

/* Set HFFRQ to 1 MHz. */
OSCFRQbits.HFFRQ = 0;

1.8.4.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 1-8 1-11 1-15. Sample Baud Rates for Asynchronous Modes
BAUD
 RATE SYNC = 0, BRGH = 1, BRG16 = 1 or SYNC = 1, BRG16 = 1
FOSC = 8.000 MHz FOSC = 4.000 MHz FOSC = 3.6864 MHz FOSC = 1.000 MHz
Actual
 Rate %
 Error SPBRG
 value
 (decimal) Actual
 Rate %
Error SPBRG
 value
 (decimal) Actual
 Rate %
Error SPBRG
 value
 (decimal) Actual
 Rate %
Error SPBRG
 value
 (decimal)
300 300.0 0.00 6666 300.0 0.01 3332 300.0 0.00 3071 300.1 0.04 832
1200 1200 -0.02 1666 1200 0.04 832 1200 0.00 767 1202 0.16 207
2400 2401 0.04 832 2398 0.08 416 2400 0.00 383 2404 0.16 103
9600 9615 0.16 207 9615 0.16 103 9600 0.00 95 9615 0.16 25
10417 10417 0 191 10417 0.00 95 10473 0.53 87 10417 0.00 23
19.2k 19.23k 0.16 103 19.23k 0.16 51 19.20k 0.00 47 19.23k 0.16 12
57.6k 57.14k -0.79 34 58.82k 2.12 16 57.60k 0.00 15
115.2k 117.6k 2.12 16 111.1k -3.55 8 115.2k 0.00 7
/* Baud rate 9600 */
SP2BRGL = 25;
SP2BRGH = 0;

1.8.4.3.3 How to Configure the Pins

Since only the transmission is necessary for this use case, only the TX pin must be configured. The pin used as TX, in this example, is the RD0 pin and it must be configured using the Peripheral Pin Select (PPS). To find the value that needs to be written to the RD0PPS register, inspect the Peripheral PPS Output Selection Codes table below, taken from the device data sheet.

Table 1-9 1-12 1-16. Peripheral PPS Output Selection Codes
RxyPPSPin Rxy Output SourcePORT to Which Output can be Directed
28-Pin Devices40-Pin Devices
0x0CEUSART2 (DT)BCBD
0x0BEUSART2 (TX/CK)BCBD
0x0AEUSART1 (DT)BCBC
0x09EUSART1 (TX/CK)BCBC

The following line will direct the EUSART2 TX to RD0:

/* RD0 is TX2 */
RD0PPS = 0x0B; 

The pin direction is set by default output, but otherwise, the following line sets it.

/* Configure RD0 as output. */
TRISDbits.TRISD0 = 0;
Table 1-13. EUSART Pin Locations
FunctionPin
EUSART2 TXRD0

Before sending data, the user needs to check if the previous transmission is complete by checking the PIR3.TXnIF bit field. The following code example waits until the transmit buffer is empty, then writes a character to the TXnREG register:

static void EUSART2_write(uint8_t txData)
{
    while(0 == PIR3bits.TX2IF)
    {
        ;
    }

    TX2REG = txData; 
}  

1.8.4.3.4 How to Implement the Function that Handles STDIO

The function that handles STDIO is void puthc(char c). Implementing this function will transmit its char argument over EUSART and will cause the printf to be redirected to EUSART.

void putch(char txData)
{
    EUSART2_write(txData);
}

The following line will send messages over EUSART:

printf("%c%c%c%c", START_DATA_STREAM_PROTOCOL, cnt, cnt * 2, STOP_DATA_STREAM_PROTOCOL);

The Data Stream Protocol frame has start and stop tokens, as inverse/one’s complement each other. In between those, the payload may contain any number of values. The protocol must be sent in binary format.

Note: The printf function uses placeholder specifiers in the format string to mark where to insert variables. Some of the available placeholders are displayed in the table below.
Table 1-14.  printf Specifiers
Placeholder Description
%d Insert a signed integer
%f Insert a floating point number
%s Insert a sequence of characters
%c Insert a character
%x Insert integer unsigned in hex format

In this example, the %c specifier is used, because the row binary value will be transmitted.

Note: The %x specifier inserts the hex value as ASCII characters, thus, this is not well-suited here.

Use MPLAB X Data Visualizer as described in the appendix, How to Configure MPLAB X Data Visualizer to Decode Data Stream Protocol.

1.8.5 Receive Control Commands

One important usage of the EUSART represents the implementation of a command line interface. This way, the microcontroller can receive control commands via EUSART. It is convenient to use the line terminator as command delimiter, so for this use case, EUSART will read full lines and then check if the line contains a valid command.

This use case follows the steps:

  • Configure the system clock
  • Configure EUSART2
  • Configure the pins
  • Implement STDIO receive and send functions
  • Read lines and execute valid commands

1.8.5.1 MCC Generated Code

To generate this project using MPLAB Code Configurator (MCC), follow these steps:

  1. Create a new MPLAB X IDE project.
  2. Open the MCC from the toolbar (information on how to install the MCC plug-in can be found on the Microchip website).
  3. Go to Project Resources > System > System Module and use the following configurations:
    • Oscillator Select: HFINTOSC
    • HF Internal Clock: 4 MHz
    • Clock Divider: 4
    • In the Watchdog Timer Enable field, in the WWDT tab, ensure WDT Disable is selected
    • In the Programming tab, make sure Low-Voltage Programming Enable is checked
  4. From the Device Resources window, add EUSART2 to the project, then use the following configurations:
    • Mode: Asynchronous
    • Enable EUSART: Checked
    • Enable Transmit: Checked
    • Baud Rate: 9600
    • Transmission-bits: 8 bits
    • Reception-bits: 8 bits
    • Data Polarity: Noninverted
    • Redirect STDIO to USART: Checked
  5. Open Pin Manager > Grid View window, select UQFN40 in the MCU package field and make the following pin configurations:
    • RD0 – EUSART2 TX
    • RD1 – EUSART RX
    • RE0 – GPIO output
    Figure 1-58. Pin Mapping
  6. In Project Resource, locate Pin Module and rename RE0 from IO_RE0 to LED0; uncheck the Analog check box.
  7. Click Generate in the Project Resources tab.
  8. Replace the main.c file with the code provided in the code example.
  9. Use MPLAB X Data Visualizer as described in the appendix, Send Commands from MPLAB X Data Visualizer.

1.8.5.2 Foundation Services Generated Code

To generate this project using the Foundation Services library, follow these steps:

  1. Create a new MPLAB X IDE project.
  2. Open the MCC from the toolbar (information on how to install the MCC plug-in can be found on the Microchip website).
  3. Go to Project Resources > System > System Module and use the following configurations:
    • Oscillator Select: HFINTOSC
    • HF Internal Clock: 4 MHz
    • Clock Divider: 4
    • In the WDT Enable field, in the WWDT tab, ensure WDT Disable is selected
    • In the Programming tab, make sure Low-Voltage Programming Enable is checked
  4. In device resource, under Libraries find Foundation Services, expand it and double-click UART it to add it to the project.
  5. Click the + button at the bottom to add a new Foundation Services federated UART.
  6. Select EUSART2 from the UART drop down in Foundation Services.
  7. Open the Pin Manager > Grid View window, select UQFN40 in the MCU package field and make the following pin configurations:
    • RD0 – EUSART TX
    • RD1 – EUSART RX
    • RE0 – GPIO output
    Figure 1-59. Pin Mapping
  8. In Project Resources, under Peripherals, select EUSART2 and use the following configuration:
    • Redirect STDIO to USART: Checked
  9. In Project Resources, locate Pin Module and rename RE0 from IO_RE0 to LED0, and uncheck the Analog check box.
  10. Click Generate in the Project Resources tab.
  11. Replace the main.c file with the code provided in the code example.
  12. Use MPLAB X Data Visualizer as described in the appendix, Send Commands from MPLAB X Data Visualizer.

1.8.5.3 Bare Metal Code

The first step will be to configure the microcontroller to disable the WDT and to enable the LVP.

/* WDT operating mode → WDT Disabled */
#pragma config WDTE = OFF
/* Low-voltage programming enabled, RE3 pin is MCLR */
#pragma config LVP = ON

1.8.5.3.1 How to Configure the System Clock

To use the HFINTOSC at 1 MHz, two settings must be made in the register. First, select HFINTOSC as the oscillator source in the OSCCON1 register by writing to the NOSC bit field:

/* Set HFINTOSC as new oscillator source. */
OSCCON1bits.NOSC = 0b110;

Then, select the nominal frequency of 1 MHz in the OSCFRQ register:

/* Set HFFRQ to 1 MHz. */
OSCFRQbits.HFFRQ = 0;

1.8.5.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.

In Asynchronous mode, the EUSART supports two types of receive: Single Receive and Continuous Receive. Since a command can be received at any time, Continuous Receive mode will be used.

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

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

Table 1-8 1-11 1-15. Sample Baud Rates for Asynchronous Modes
BAUD
 RATE SYNC = 0, BRGH = 1, BRG16 = 1 or SYNC = 1, BRG16 = 1
FOSC = 8.000 MHz FOSC = 4.000 MHz FOSC = 3.6864 MHz FOSC = 1.000 MHz
Actual
 Rate %
 Error SPBRG
 value
 (decimal) Actual
 Rate %
Error SPBRG
 value
 (decimal) Actual
 Rate %
Error SPBRG
 value
 (decimal) Actual
 Rate %
Error SPBRG
 value
 (decimal)
300 300.0 0.00 6666 300.0 0.01 3332 300.0 0.00 3071 300.1 0.04 832
1200 1200 -0.02 1666 1200 0.04 832 1200 0.00 767 1202 0.16 207
2400 2401 0.04 832 2398 0.08 416 2400 0.00 383 2404 0.16 103
9600 9615 0.16 207 9615 0.16 103 9600 0.00 95 9615 0.16 25
10417 10417 0 191 10417 0.00 95 10473 0.53 87 10417 0.00 23
19.2k 19.23k 0.16 103 19.23k 0.16 51 19.20k 0.00 47 19.23k 0.16 12
57.6k 57.14k -0.79 34 58.82k 2.12 16 57.60k 0.00 15
115.2k 117.6k 2.12 16 111.1k -3.55 8 115.2k 0.00 7
/* Baud rate 9600 */
SP2BRGL = 25;
SP2BRGH = 0;

1.8.5.3.3 How to Configure the Pins

In this example, the pin used as TX is RD0 and it must be configured using the Peripheral Pin Select (PPS). To find the value that needs to be written to the RD0PPS register, inspect the Peripheral PPS Output Selection Codes table below, taken from the device data sheet.

Table 1-9 1-12 1-16. Peripheral PPS Output Selection Codes
RxyPPSPin Rxy Output SourcePORT to Which Output can be Directed
28-Pin Devices40-Pin Devices
0x0CEUSART2 (DT)BCBD
0x0BEUSART2 (TX/CK)BCBD
0x0AEUSART1 (DT)BCBC
0x09EUSART1 (TX/CK)BCBC

The following line will direct the EUSART2 TX to RD0:

/* RD0 is TX2 */
RD0PPS = 0x0B; 

The pin direction is set by default output, but if it were not, the following line sets it.

/* Configure RD0 as output. */
TRISDbits.TRISD0 = 0;
The pin used as RX is RD1, so using PPS, EUSART2 RX must be routed to this pin. For this, the RX2PPS register is used. The value to be written is determined based on the xxxPPS register definition:
Figure 1-60. Peripheral xxx Input Selection

Therefore, the following line routes RX2 to pin RD1.

RX2PPS = 0b00011001;

Since the pin is not a digital input by default, this needs to be configured (enable digital input buffers using the ANSELD register and set as input using the TRISD register).

/* Configure RD1 as input. */
TRISDbits.TRISD1 = 1;
/* Enable RD1 digital input buffers.*/
ANSELDbits.ANSELD1 = 0;
The LED pin, RE0 in this example, must be configured as a digital output.
/* Configure RE0 as output. */
TRISEbits.TRISE0 = 0;
Table 1-17. EUSART Pin Locations
FunctionPin
EUSART2 TXRD0
EUSART2 RXRD1
LED0RE0
Before sending data, the user needs to check if the previous transmission is complete by checking the PIR3.TXnIF bit field. The following code example waits until the transmit buffer is empty, then writes a character to the TXnREG register:
static void EUSART2_write(uint8_t txData)
{
    while(0 == PIR3bits.TX2IF)
    {
        ;
    }
    TX2REG = txData;
}

Before reading the data, the user must wait for it to be available by polling the PIR3.RCnIF flag.

uint8_t EUSART2_read(void)
{
    while(0 == PIR3bits.RC2IF)
    {
        ;
    }

    return RC2REG;
}

1.8.5.3.4 Implement STDIO Receive and Send Functions

The function that handles STDIO is void puthc(char c). Implementing this function will transmit its char argument over EUSART and will cause printf to be redirected to EUSART.

void putch(char txData)
{
    EUSART2_write(txData);
}

Similarly, to implementing putch, char getch(void) must be implemented so that the EUSART incoming data are mapped to STDIO.

char getch(void)
{
    return EUSART2_read();
}

1.8.5.3.5 How to Read Lines and Execute Valid Commands

The following code snippet reads one line of data and stores it in an array. A valid line is shorter than the array length in this example.

The array index is reset to zero when reaching the array end, to avoid a bugger overflow error in case of longer lines received. The characters, ‘\n’ (line feed) and ‘\r’ (carriage return), are ignored, because they are part of the line terminator. When ‘\n’ is found, the string end (NULL) is added to the command, and the function ‘executeCommand’ will interpret it and change the state of the LED .

char command[MAX_COMMAND_LEN];
uint8_t index = 0;
char c;
while(1)
{
    c = getch();
    if(c != '\n' && c != '\r')
    {
        command[index++] = c;
        if(index > MAX_COMMAND_LEN)
        {
            index = 0;
        }
    }

    if(c == '\n')
    {
        command[index] = '\0';
        index = 0;
        executeCommand(command);
    }
}

Using the MPLAB X Data Visualizer, ‘LED ON’ and ‘LED OFF’ commands can be sent to the board.

Use the MPLAB X Data Visualizer as described in the appendix, Send Commands from MPLAB X Data Visualizer.

1.8.7 How to Receive Data in MPLAB® X Data Visualizer

First, ensure the plug-in is installed; if not, install it from within MPLAB® X Integrated Development Environment (IDE) by going to Tools > Plug-ins > Available Plug-ins.

If using a platform with on-board UART to USB, no further hardware setup is needed. If not, connect the TX pin to the RX pin of a UART to USB converter and connect that to the PC.

Open the Data Visualizer by clicking the icon at the top of the MPLAB X IDE.

Figure 1-61. MPLAB® X Data Visualizer Icon

Available COM ports will be listed on the left hand side of the screen. Find the one associated with the connected board and click the Play button. In the terminal window settings (right hand side), select Input Source to be the COM port associated with the board. It will start receiving messages from the microcontroller.

Figure 1-62. MPLAB® X Data Visualizer Receiving Data

1.8.8 How to Configure MPLAB® X Data Visualizer to Decode Data Stream Protocol

  • In Data Visualizer, click the Plot Data button and select Data Stream Protocol.
    Figure 1-63. MPLAB® X Data Visualizer Data Format
  • Click Next and in the second screen, configure the frame format for the two fields in the payload.
    Figure 1-64. MPLAB® X Data Visualizer Data Stream Protocol Payload Configuration
  • Click Next and then Plot, as the default name for the Decoder works well the way it is generated. As soon as this is done, given the device is correctly programmed with the example code and is connected to the PC, the plots should start showing in real time.
    Figure 1-65. MPLAB® X Data Visualizer Displaying Plots

1.8.9 Send Commands from MPLAB® X Data Visualizer

Program the example code on the device, then open MPLAB® X Data Visualizer. Select the correct COM port as the input source. In the output section, ensure Newline Character is set to ‘CR+LF’. Type one of the commands, ‘ON’ or ‘OFF’, and observe the LED changing its state accordingly.

Figure 1-66. Commands Sent from MPLAB® X Data Visualizer