4.4 Functions/Constants to Support A Simulated UART

These functions and constants support UART functionality in the MPLAB X Simulator.

Examples of Use

Example: Using UART2

/* This program transmits a "Hello, world!" message every 1s
 * at 115200 8n1 through UART2 using the default stdio provided
 * by the XC32 compiler.
 * 
 * This example is for a PIC32AK1216GC41064 DIM on a Curiosity
 * Platform Board.
 */

#include <xc.h>
#include <stdio.h>

#define FCY             8000000UL
#include <libpic30.h>

#define UART_CLK        4000000UL
#define BAUDRATE        115200UL

int main(void)
{
    // UART IO configuration
    _TRISD1 = 0;
    _RP50R = _RPOUT_U2TX;
    
    // select UART2
    __C30_UART = 2;
    
    // set UART configuration
    U2CONbits.CLKMOD = 1;       // fractional baud rate generation
    U2CONbits.CLKSEL = 0;       // standard speed peripheral clock
    U2CONbits.TXEN = 1;         // enable UART transmit
    U2BRG = UART_CLK / BAUDRATE;
    
    // turn ON uart
    U2CONbits.ON = 1;
    
    while (1) {
        printf("Hello, world!\r\n");
        __delay_ms(1000);
    }
}

Example: Millisecond Delay

#define FCY 1000000UL 
#include <libpic30.h> 
int main() { 
  /* at 1MHz, these are equivalent */ 
  __delay_ms(1); 
  __delay32(1000); 
}

Example: Microsecond Delay

#define FCY 1000000UL 
#include <libpic30.h> 
int main() { 
  /* at 1MHz, these are equivalent */ 
  __delay_us(1000); 
  __delay32(1000); 
}