20.5.1.1 Transmission

Address Detect Transmission

#include <xc.h>

//Clocking based on CPU @8MHz, UART1 clocked from 1/2 speed peripheral bus @4MHz
#define FUART 4000000
#define BAUDRATE 9600
//Baud rate calculation for fractional baud rate mode
#define BRGVAL (FUART/BAUDRATE)

#define ADDRESS1 0x45
#define ADDRESS2 0x55

#define TRANSMIT_CHAR_SIZE 4

uint8_t transmitChar1[TRANSMIT_CHAR_SIZE] = "abcd";
uint8_t transmitChar2[TRANSMIT_CHAR_SIZE] = "efgh";


int main(void) {

    //Configure I/O
    _RP114R = _RPOUT_U1TX;      //Assign UART1 TX output functionality to RP114 (RH1)
    _TRISH1 = 0;                //Set RH1 as output

    U1CONbits.MODE = 4;        // Asynchronous 9-bit UART with address detect
    U1CONbits.CLKSEL = 0;      // FPB/2 as Baud Clock source
    U1CONbits.STP = 0;         // 1 stop bit

    //Use fractional baud rate divider
    U1CONbits.CLKMOD = 1;   
    U1BRG = BRGVAL;            // Baud Rate setting for 9600

    U1CONbits.ON = 1;          // Enable UART
    U1CONbits.TXEN = 1;        // Enable UART TX.
    
    //Transmit using Address1
    U1PAbits.P1 = ADDRESS1;   // Write the address1 value to Parameter1
    
    /* Send data bytes for address1 */
    for (int i = 0; i < TRANSMIT_CHAR_SIZE; i++) {
        U1TXB = transmitChar1[i];
    }
    //Wait for transmission to complete
    while(U1STATbits.TXMTIF == 0);
    
    //Transmit using Address2
    U1PAbits.P1 = ADDRESS2; // Write the address2 value to Parameter1
    
    /* Send data bytes for address2 */
    for (int i = 0; i < TRANSMIT_CHAR_SIZE; i++) {
        U1TXB = transmitChar2[i];
    }
    //Wait for transmission to complete
    while(U1STATbits.TXMTIF == 0);
    
    //Periodically repeat the transmission
    while(1) {
        
        //Delay before re-transmitting
        for (int i = 0; i < 0x1234; i++);
        
        U1PAbits.P1 = ADDRESS1; // Write the address1 value to Parameter1
    
        /* Send data bytes for address1 */
        for (int i = 0; i < TRANSMIT_CHAR_SIZE; i++) {
            U1TXB = transmitChar1[i];
        }
        //Wait for transmission to complete
        while(U1STATbits.TXMTIF == 0);

        U1PAbits.P1 = ADDRESS2; // Write the address2 value to Parameter1
        /* Send data bytes for address2 */
        for (int i = 0; i < TRANSMIT_CHAR_SIZE; i++) {
            U1TXB = transmitChar2[i];
        }
        //Wait for transmission to complete
        while(U1STATbits.TXMTIF == 0);
        
    }
    
    return 0;
}