Search code examples
embeddedinterruptuartmicrochip

PIC24FJ False interrupt trigger during first run


I am working on interrupt driven UART on PIC24FJ256GB606 device. My code works just fine except UART receive interrupt gets falsely triggered for the first time: e.g. main routine is : [code]

int main(){

System_init();
uart_init();

// UART2_PPS
  RPINR19bits.U2RXR= 21;
  RPOR13bits.RP26R = 5;

  TRISGbits.TRISG6 = 1;
  TRISGbits.TRISG7 = 0;

        uart_puts("\n**********************************************");
        uart_puts("\nMy project ");
        uart_puts("\n x");
        uart_puts("\n x");
        uart_puts("\n"__DATE__);
        uart_puts("\n**********************************************");
return -1;
}
}

[/code]

ISR is:

[code]void __attribute__ ( ( interrupt, no_auto_psv ) ) _U2RXInterrupt( void )
{

        IFS1bits.U2RXIF = 0;
        uart_puts("False Trigger");
        __delay_ms(1000);
}[/code]

Initialization is:

[![\[code\]int uart_init()
{
    int status = -1;
        size_t i;


   // STSEL 1; IREN disabled; PDSEL 8N; UARTEN enabled; RTSMD disabled; USIDL disabled; WAKE disabled; ABAUD disabled; LPBACK disabled; BRGH enabled; URXINV disabled; UEN TX_RX; 
   U2MODE = 0x8008;
   // OERR NO_ERROR_cleared; URXISEL RX_ONE_CHAR; UTXBRK COMPLETED; UTXEN disabled; ADDEN disabled; UTXISEL0 TX_ONE_CHAR; UTXINV disabled; 
   U2STA = 0x0000;
   // U2TXREG 0; 
   U2TXREG = 0x0000;
   // BaudRate = 9600; Frequency = 4000000 Hz; U2BRG 103; 
   U2BRG = 0x0067;
   // ADMADDR 0; ADMMASK 0; 
   U2ADMD = 0x0000;

            rb_attr_t attr = {sizeof(_rbmem\[0\]), ARRAY_SIZE(_rbmem), _rbmem};
            if (ring_buffer_init(&_rbd, &attr) == 0) { 
            U2MODEbits.UARTEN = 1; // And turn the peripheral on
            U2STAbits.UTXEN = 1; //UART2 Transmit Enable 
            IFS1bits.U2RXIF = 0; //_U2RXIF = 0;
            IEC1bits.U2RXIE = 1; //_U2RXIE = 1;
                status = 0;
            }

    return status;
}\[/code\]][1]][1]

please see the image attached and consider following, 1) microcontroller is not expected to receive anything at this point. i tried to run the same code leaving rx line open i got the same result. 2) This code works just fine if i disable the receive interrupt IEC1bits.U2RXIE = 0;

UART


Solution

  • Hi Doynax thank you for response. The initialization code was generated by MCC and was buggy! i received a response on another forum:

    Hi, The initialization code for UART look like it may have been generated by MCC. Despite this, it demonstrate bad programming practice, in that it enable the module before other setup operations.

    What is done, is contrary to what is recommended in datasheet for this device, and for all other devices that I have looked at. Datasheet section 19.2 Transmitting in 8-Bit Data Mode and section 19.3 Receiving in 8-Bit or 9-Bit Data Mode: 1. Set up the UARTx: a) Write appropriate values for data, parity and Stop bits. b) Write appropriate baud rate value to the UxBRG register. c) Set up transmit and receive interrupt enable and priority bits. 2. Enable the UARTx. 2. Enable the UARTx by setting the URXEN bit (UxSTA<12>).

    Also, in the generated code, there is a write to TXREG = 0000; This do not only clear the register, it may also queue transmission of a first byte in the transmitter FIFO register.

    Regards, Mysil

    changing the initialization sequence solved the problem!