14 Using the UART Module to Transmit Data

The UART module with protocol support allows users to perform serial data transfers and can easily be configured to transmit data. To configure the UART for data transmission, the Transmit Enable (TXEN) bit of UxCON0 must be set and the MODE[3:0] bits of UxCON0 must be set as ‘0000’ through ‘0011’, depending on the application. The Baud Rate Generator (BRG) must also be configured and can be accessed using the UxBRG register and the BRGS bits of UxCON0. Data is transmitted via the TX pin of the microcontroller, which is PPS remappable using the RxyPPS register. The UART module is enabled by setting the ON bit of UxCON1.

Once the UART module has been initialized as a transmitter, data can be transmitted by writing to the UART Transmit Buffer (UxTXB) register. The UART Transmit Interrupt Flag (UxTXIF) bit is set when UART transmission has been enabled and the UxTXB register is ready to accept data. Example 14-1 demonstrates the initialization of the UART module for data transmission.

UART Transmit Mode Configuration

void UART_Initialize(){
    U1CON0 = 0xA0;              // BRGS normal; 8-bit mode; RXEN disabled; 
                                // TXEN enabled
    U1CON2 = 0x00;              // TXPOL not inverted; FLO off;
    U1BRGL = 0x19;              // BRGL 25 = 9600 BR @ 1 MHz FOSC
    U1BRGH = 0x00;
    U1FIFO = 0x00;              // STPMD in middle of first Stop bit;
    U1UIR = 0x00;               // Auto-baud not enabled
    U1ERRIR = 0x00;
    U1ERRIE = 0x00;
    RC6PPS = 0x13;              // UART PPS Settings, RC6->UART1:TX1;    
    U1RXPPS = 0x17;             // UART PPS Settings, RC7->UART1:RX1;  
    U1CON1 = 0x80;              // ON enabled;  
}