Question

I have been working with this code for days and cannot figure out why my interrupts are not being triggered. I know data is coming through successfully because I used a probe on a logic analyzer, also my baud rate is correct as I can transmit with UART successfully.

At this point I'm lost, I've read the datasheet over and over and can't figure out my problem. I will try to include only the relative code but enough that you can see how things work in my project.

Please let me know if you see issues with this code.

Thank you!

Code snippets from main.c:

    // USART RX interrupt priority
    IPR1bits.RCIP = 0;
    IPR1bits.TXIP = 0;

    // configure the hardware USART device
    OpenUSART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT &
        USART_CONT_RX & USART_BRGH_LOW, 14);

Code snippets from interrupts.c

    //----------------------------------------------------------------------------
    // Low priority interrupt routine
    // this parcels out interrupts to individual handlers
    #pragma code
    #pragma interruptlow InterruptHandlerLow

    // This works the same way as the "High" interrupt handler

    void InterruptHandlerLow() {

    // check to see if we have an interrupt on USART RX
    if (PIR1bits.RCIF) {
        PIR1bits.RCIF = 0; //clear interrupt flag
        uart_recv_int_handler();
    }
    // check to see if we have an interrupt on USART TX
    if (PIR1bits.TXIF && PIE1bits.TXIE) {
        // cannot clear TXIF, this is unique to USART TX
        // so just call the handler
        uart_tx_int_handler();
    }
    }

UART RX Interrupt Handler snippet:

    void uart_recv_int_handler() {
        int msgLen;

        //if (DataRdyUSART()) {
        uc_ptr->buffer[uc_ptr->buflen] = RCREG;
        //uc_ptr->buffer[uc_ptr->buflen] = ReadUSART();
            uc_ptr->buflen++;
       }
    }
Was it helpful?

Solution

Did you - Set trisC6/7 correctly? - if you have a part with analog inputs multiplexed on those pins, did you disable them? - Is your BRG value validated for this part and these oscillator settings?

See also

http://www.piclist.com/techref/microchip/rs232.htm

I migrated to dspic, but I used to do the serial receive under interrupt. This I had in the interrupt (serialin1 is a power of two circular buffer, lastserialin1 the pointer into it, and ser1bufinmask is size of buffer-1)

if (PIR1bits.RCIF == 1)  /* check if RC interrupt (receive USART) must be serviced 
{
    while (PIR1bits.RCIF == 1)   /* flag becomes zero if buffer/fifo is empty */
    {
      lastserialin1=(lastserialin1+1)&ser1bufinmask;
      serialin1[lastserialin1]=RCREG;
    }
}

To initialize the uart I had:

// Configure USART
TXSTA = 0x20;  // transmit enable
RCSTA = 0x90;  // spen en cren
RCONbits.IPEN      = 1;  /* Interrupt Priority Enable Bit. Enable priority levels on interrupts */

INTCONbits.GIE     = 1;  /* Set GIE. Enables all high priority unmasked interrupts */
INTCONbits.GIEL    = 1;  /* Set GIEL. Enables all low priority unmasked interrupts */

TRISCbits.TRISC6    = 0;    // page 237
TRISCbits.TRISC7    = 1;    // page 237

Open1USART (  
               USART_TX_INT_OFF 
               &
            USART_RX_INT_ON &
            USART_ASYNCH_MODE &
            USART_EIGHT_BIT &           // 8-bit transmit/receive
            USART_CONT_RX &             // Continuous reception
                       //               USART_BRGH_HIGH, 155);      // High baud rate, 155 eq 19k2
            USART_BRGH_HIGH, brgval);       // High baud rate, 25 eq 115k2 

IPR1bits.RCIP       = 0;
PIR1bits.RCIF       = 0;

with brgval calculated using

     #define GetInstructionClock()  (GetSystemClock()/4)
     #define GetPeripheralClock()   GetInstructionClock()

     // See if we can use the high baud rate setting
     #if ((GetPeripheralClock()+2*BAUD_RATE)/BAUD_RATE/4 - 1) <= 255
        #define BRGVAL ((GetPeripheralClock()+2*BAUD_RATE)/BAUD_RATE/4 - 1)
        #define BRGHVAL (1)
     #else  // Use the low baud rate setting
        #define BRGVAL ((GetPeripheralClock()+8*BAUD_RATE)/BAUD_RATE/16 - 1)
        #define BRGHVAL (0)
     #endif
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top