0
\$\begingroup\$

I am using PIC32 and I have few inputs. I want a UART Tx Interrupt to fire whenever any input becomes HIGH. I have somehow manged to write code for UART tx and rx interrupt and its working but I don't have an idea how to implement it for my application.

int main()
{
  //configure UART
UARTConfigure(UART1, UART_ENABLE_PINS_TX_RX_ONLY);
UARTSetFifoMode(UART1, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
UARTSetLineControl(UART1, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
UARTSetDataRate(UART1, FPB, 115200);
UARTEnable(UART1, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));

// Configure UART  Interrupt
INTEnable(INT_SOURCE_UART_RX(UART1), INT_ENABLED);
INTSetVectorPriority(INT_VECTOR_UART(UART1), INT_PRIORITY_LEVEL_2);
INTSetVectorSubPriority(INT_VECTOR_UART(UART1), INT_SUB_PRIORITY_LEVEL_0);

// Enable multi-vector interrupts
INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
INTEnableInterrupts();

 while(1)
{
  //main code
}

}

void __ISR(_UART1_VECTOR, ipl2) IntUart1Handler(void)
{
// RX interrupt
if(INTGetFlag(INT_SOURCE_UART_RX(UART1)))
{
        // Clear the RX interrupt Flag
    INTClearFlag(INT_SOURCE_UART_RX(UART1));

        // Echo what we just received.
    char RxBuffer[5];
    if((U1STAbits.URXDA)!=0)
    {
        getsUART1(5,RxBuffer , 123);
        U1STAbits.URXDA = 0;
        U1STAbits.OERR = 0; 
        putsUART1(RxBuffer);
        putsUART1("\n");
        memset(RxBuffer,0,5*sizeof(char));

    }


}

// TX interrupt
if ( INTGetFlag(INT_SOURCE_UART_TX(UART1)) )
{
        INTClearFlag(INT_SOURCE_UART_TX(UART1));

}
}

I don't know what logic should I made so that if any inputs become HIGH, lets say

if(INPUT1 == HIGH)
{
  //generate UART tx interrupt
}

Please help. Thanks!

\$\endgroup\$
  • \$\begingroup\$ "I want a UART Tx Interrupt to fire whenever any input becomes HIGH" that is illogical, and likely impossible. The functionality you want is a GPIO pin change interrupt, or whatever your vendor chooses to call it. You can probably point the interrupt vector at the same service routing used for UART Tx, though it is unclear why you would want to. Typically a UART Tx interrupt files when the UART's hardware buffers are able to accept more data from a buffering routine. \$\endgroup\$ – Chris Stratton Nov 26 '15 at 16:59
1
\$\begingroup\$

Use pseudo-code:

if(INPUT1 == HIGH)
{
    //fill Tx FIFO buffer with some data, which causes UART Tx interrupt
}

UART TX interrupt is fired, when UART TX FIFO buffer is filled with data.

\$\endgroup\$
  • \$\begingroup\$ you mean to say tx register of UART. I am sending data to U1TXREG but not working. \$\endgroup\$ – user46573544 Nov 26 '15 at 11:24
  • \$\begingroup\$ Enable interrupts first. \$\endgroup\$ – Junior Nov 26 '15 at 11:26
  • 1
    \$\begingroup\$ OK i got it working \$\endgroup\$ – user46573544 Nov 26 '15 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.