2.1 Printf In Transmit Mode

The UART transmitter supports the use of printf() statements. Printf() statements can be used instead of directly writing to the UTXB register. The printf() function is part of the STDIO library, which is included in the XC8 compiler tool suite. It is important to note that while the use of printf() statements simplify user software routines, the printf() function takes more RAM space and adds additional instruction cycles to complete.

The MPLAB® Code Configurator (MCC) plug-in tool can be used to add printf() functionality to the UART. Simply add a check to the “Redirect STDIO to UART” check box under the “Software Settings” drop-down menu in the UART Easy Setup tab. When the box is checked, MCC will generate the additional code needed by the STDIO library, as shown in Printf() Additional Code for Transmit Mode for PIC18(L)FXXK42. The example printf() statement, as well as the UART transmit routine, is also shown in the example below for reference. Refer to the data sheet for more details on interrupts.

Printf() Additional Code for Transmit Mode for PIC18(L)FXXK42

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

void UART1_Write(uint8_t txData)
{    
    while(0 == PIR3bits.U1TXIF);    
    U1TXB = txData;                        // Write the data byte to the UART.
}

printf ("Hello World!\r\n");

Info: MCC is an integral part of the code development for Microchip products. The MCC can be used to set up UART configuration code to allow the UART to function. MCC is a dynamic program that is used in conjunction with MPLAB® X as a plug in. Refer to http://www.microchip.com/mcc for more information.