# Programming dsPIC30F4011/13 on board dsPICDEM 2 with MPLAB ICD 2

I have Microchip's dsPICDEM 2 Development Board with two MCUs on it, a dsPIC30F4011 Motor Control MCU and a dsPIC30F4013 General Purpose MCU. I need to program both microcontrollers with the MPLAB ICD 2 In-Circuit Debugger.

When I build a test project (with a program that just toggles on and off LEDs D3 and D4), everything is built successfully (using the C30 Language Tools). The program looks like this:

#include <p30fxxxx.h>

#include "delay.h"

int main(void)
{
// setup
LATBbits.LATB0 = 0;
TRISBbits.TRISB0 = 0;

while (1)
{
LATBbits.LATB0 = ~LATBbits.LATB0;
Delay5ms(100);
}

return 0;
}


(the functions prtotyped in delay.h are functions defined in assembly code in delay.s and have been excerpted from Microchip's examples)

When I program any MCU by means of MPLAB 8.80 and the ICD, I get the message Programming succeeded. However, when I run the program on the board, nothing happends (no LED switches on and off).

I have tried including some files into the project:

• libpic30-coff.a under Library Files
• p30F4011.gld under Linker Script
• p30F4011.h and p30Fxxxx.h under Header Files

but that has not helped.

I have not been able to find any detailed, step-by-step information on how to program these PICs using the named tools, neither in Microchip's documentation nor on the internet.

What do I have to don in MPLAB IDE 8.80 in order to program both dsPIC30F4011 and dsPIC30F4013 with MPLAB ICD 2, assuming MPLAB IDE and ICD 2 are already set up?

EDIT:

I was using the following directives to set the configuration bits:

_FOSC(CSW_FSCM_OFF & XT_PLL8);  //Run this project using an external crystal
//routed via the PLL in 8x multiplier mode
//For the 7.3728 MHz crystal we will derive a
//throughput of 7.3728e+6*8/4 = 14.74 MIPS(Fcy)
//,~67nanoseconds instruction cycle time(Tcy).
_FWDT(WDT_OFF);                 //Turn off the Watch-Dog Timer.
_FBORPOR(MCLR_EN & PWRT_OFF);   //Enable MCLR reset pin and turn off the
//power-up timers.
_FGS(CODE_PROT_OFF);            //Disable Code Protection


I saw this code in Microchip's examples.

However, this was not working. Anyway, the solution is as Brett pointed out. I tried it, and it worked from the very first time.

I have tried again the first code, and now it does work. Weird. I still do not know what I was doing wrong.

In addition, I have tried removing the files I included, and I have realized that there is no need to include those libraries / header files / linker scripts into the project.

You have to set the configuration bits. Near the end of the device's header file (line 5880 for dsPIC30F4013) is a listing of all the device's configuration bits that need to be set or cleared to ensure proper operation of the device, as well as code comments for how to do it.

For example:

_FOSC( XT & CSW_FSCM_OFF )  // Configure for external XT crystal oscillator, turn off clock switching and monitoring


The oscillator and watchdog timer configurations are very important.

EDIT: In your edited post you tried using the phase-locked loop option for the oscillator. You have to add the following code in your main routine in order to use an external oscillator + PLL:

/*
*  Set up the PLL for full 60 MIPS operation (dsPIC33EP512MU810)
*
*  Fcy = Fin*K/2 --> 60MHz = 8MHz*K/2 --> K = 15
*  K = M/(N2*N1) --> 15 = M/(N2*N1)   --> N2 = 2, N1 = 2, M = 60
*  Fcy = 8MHz*15/2 = 60MHz
*
*  p. 164-171 datasheet
*/
void configPLL()
{
PLLFBDbits.PLLDIV = 58; // M = 60
CLKDIVbits.PLLPOST = 0; // N1 = 2
CLKDIVbits.PLLPRE = 0;  // N2 = 2
OSCTUN = 0;
// Writing to the OSCCON register requires a special unlock sequence
// Using a compiler built-in write instruction to handle this
__builtin_write_OSCCONH(0x03);  // Select primary oscillator with PLL
__builtin_write_OSCCONL(0x01);  // Request oscillator switch
while (OSCCONbits.COSC != 0x3); // Wait for the PLL to lock
}