Electronic – PIC24FJ256GA410, UART interrupt, freeze after enable

interruptspicuart

I'm fairly new to PIC and MCUs. My board has PIC24FJ256GA410 MCU with external 3.6864 MHz crystal. I'd like to use UART1 for debugging purposes. Below is my main() that just initializes UART1. However, once I program the MCU with code that calls InitUART(), my board becomes unprogrammable. Every attempt at programming it gives me Failed to program device error. Also, sometimes, it writes something like this into console:

Address: 278 Expected Value: eb0000 Received Value: a90000

or

The programmer could not be started: Could not connect to tool hardware: ICD3PlatformTool, com.microchip.mplab.mdbcore.ICD3Tool.ICD3DbgToolManager

I'm using Microchip ICD3 and MPLAB X IDE for programming. If I comment out the InitUART() call, I can program the board again. What's the problem with my code? It's mostly identical with the code from dsPIC33-PIC24 FRM – UART datasheet except I don't write default values. Also, I've seen other examples on the Internet and my is quite similar. I configured only the output pin.

EDIT: After I comment assignment to _RP11R in InitUART() function, I can successfully program the board every time. AFAIK, I need to select pin for the UART1 perpherial too. But once I set the _RP11R, the board becomes unprogrammable (and UART1 doesn't work anyway). I'm pretty sure the RP11 is the correct UART output pin.

#include <xc.h>

#define FOSC 3686400
#define FCY (FOSC/2)

#include <libpic30.h>

#pragma config JTAGEN = OFF
#pragma config GWRP = OFF
#pragma config FWDTEN = OFF
#pragma config ICS = PGx3
#pragma config FWPSA = PR128
#pragma config WDTPS = PS2048
#pragma config FCKSM = CSECMD
#pragma config OSCIOFCN = OFF
#pragma config POSCMOD = XT
#pragma config FNOSC = PRI

#define BAUDRATE 115200

static void InitUART(void)
{
    __builtin_write_OSCCONL(OSCCON & 0xbf);
    _RP11R = _RPOUT_U1TX;
    __builtin_write_OSCCONL(OSCCON | 0x40);
    U1BRG = (int) (FCY/(16 * BAUDRATE) - 1);
    _U1TXIE = 1;
    U1MODEbits.UARTEN = 1;
    U1STAbits.UTXEN = 1;
}


void __attribute__((__interrupt__, auto_psv)) _U1TXInterrupt(void)
{
    _U1TXIF = 0;
}

int main(void)
{
    TRISBbits.TRISB3 = 0;
    LATBbits.LATB3 = 0;

    InitUART();

    int i = 0;
    while (1) {
        LATBbits.LATB3 = i;
        i = !i;
        __delay_ms(500);
    }

    return 0;
}

Best Answer

I had bad cable connections. TX pin was connected to the ground.

My colleague says: If it doesn't work check the cables. If the cables are okay and it still doesn't work, check the cables again.