Electronic – Interrupt handling for a UART on PIC32 with C32 v2.00 compiler

interruptspicserialuart

I am very new to PIC32 chips (or any PIC chips) and I am trying to create an interrupts handler for the UART to be called when the UART receive a byte. I am using the PIC32MX695F512L PIC32 processor with C32 2.00 compiler on a development board.

I found this example code in another project online. The comments led me to belive that this interrupt would be called whenever the UART receives a byte. I added a break point to this function and a static counter to count how many times this function has been run.

In other parts of the source code I can send BYTES out of the UART and are being revived by my PC. I have tried sending BYTES to the UART from my PC but this function is never triggered and the function counter (UART1Info_count) never increases.

Source code

static BYTE UART1Info_LastByteRXed ;
static UINT32 UART1Info_count ; // Init elsewhere 

BOOL Uart_Connect ( ) {

    UART1Info_count = 0 ; 
    UART1Init();

    UARTConfigure       (UART1, UART_ENABLE_PINS_TX_RX_ONLY);
    UARTSetFifoMode     (UART1, UART_INTERRUPT_ON_RX_NOT_EMPTY);
    UARTSetLineControl  (UART1, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1 );
    UARTSetDataRate     (UART1, GetPeripheralClock(), 9600) ;
    UARTEnable          (UART1, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));

    INTEnable           (INT_SOURCE_UART_RX(UART1), INT_ENABLED);
    INTEnable           (INT_SOURCE_UART_TX(UART1), INT_ENABLED);


    // Set Interrupt priorities    
    INTSetVectorPriority    (INT_VECTOR_UART(UART1), INT_PRIORITY_LEVEL_2);
    INTSetVectorSubPriority (INT_VECTOR_UART(UART1), INT_SUB_PRIORITY_LEVEL_0);

    // configure for multi-vectored mode
    INTConfigureSystem      (INT_SYSTEM_CONFIG_MULT_VECTOR);
    INTEnableInterrupts     ();

    Rs485RxLineConfig();
    Rs485TxLineConfig();
    Rs485TxDisable();
    Rs485RxEnable();

    return TRUE ;
}

#define __UART_1_ISR    __ISR(_UART_1_VECTOR, ipl4)
void __UART_1_ISR  UART_1_InterruptRoutine(void)
{
    UART1Info_count++ ; 
    IFS0CLR = _IFS0_U1RXIF_MASK;
    UART1Info_LastByteRXed = UART1ReceivedValue();
    UART1TransmitTestASCIICharacters( (UART1Info_LastByteRXed+1), 1); //reflect back character, incremented by 1
}

Other information

  • Processor: PIC32MX695F512L
  • Compiler: C32 v2.00
  • IDE: MPLAB X IDE

My question

How to I create a interrupt handler on receive BYTE via UART on PIC32 with C32 v2.00 compiler?

Note:
Updated source code example with UART init()

Best Answer

Your example code doesn't show any of the initialization for the UART.

Are you enabling the receive interrupt for the UART? You need to do something like this:

#define BAUDRATE1       (115200)     // the desired BaudRate
#define BRATE1    (FPB/4/BAUDRATE1) 
#define UART1_CONFIG1   UART_EN|UART_BRGH_FOUR      // defaults to 8,N,1
#define UART1_CONFIG2   UART_RX_ENABLE|UART_TX_ENABLE|UART_INT_RX_CHAR              // only RX interrupts

void ConfigureUart1(void)
{
    OpenUART1(UART1_CONFIG1, UART1_CONFIG2, BRATE1); // approx 115200 baud
}

where FPB is defined as your peripheral bus frequency, e.g. 10000000L for 10 MHz.

Then in main or elsewhere:

ConfigureUart1();
INTEnableSystemMultiVectoredInt();         
INTEnableInterrupts();