2
\$\begingroup\$

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;
}
\$\endgroup\$
  • \$\begingroup\$ Check if your UART pins are not the ones that you are using to program the board i.e. PGEC/PGED pins are not configured as UART1 pins. \$\endgroup\$ – ammar.cma Sep 17 '17 at 5:52
  • \$\begingroup\$ Nope. In the code, I configure only the output pin. If I understand correctly, by default, peripheral output pins are disconnected and peripheral input pins are tied to Vdd. \$\endgroup\$ – woky Sep 17 '17 at 8:30
  • \$\begingroup\$ PGECx/PGEDx are on different RPx pins on my MCU. \$\endgroup\$ – woky Sep 17 '17 at 8:46
0
\$\begingroup\$

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.

\$\endgroup\$

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.