Simple dsPIC33FJ32MC202 example

This is a very simple example circuit and C program for Microchip’s dsPIC33FJ32MC202 microcontroller.

The basic circuit used to connect the PICkit 2 to the dsPIC33FJ32MC202 is shown below. One thing that confused me when I tried to wire this up first was that the capacitor shown between pin 20 (VCAP/VDDCORE) and pin 19 (VSS) is necessary to get things working at all, which was not the case for the dsPIC microcontrollers that I’ve used before. The datasheet specifies that this capacitance should be between 4.7uF and 10uF (I’m using 6.6uF in my circuit). An LED is also shown connected to pin 26 (RB15), because that’s what I’ll be switching on and off in this specific exaxmple. Obviously, you don’t need that in your circuit if you don’t want an LED on that pin.

dspic33fj32mc202_circuit1

This is the C code:

//
// main.c - Blinking LED example for dsPIC33FJ32MC202
// written by Ted Burke
// last updated 17-11-2012
//

#include <xc.h>
#include <libpic30.h>

// Internal FRC oscillator w/ PLL (Fcy=23031250Hz,Tcy=43.419ns)
_FOSCSEL(FNOSC_FRCPLL);	

// Watchdog timer off
_FWDT(FWDTEN_OFF);

int main()
{
	// Configure RB15 as digital output
	TRISB = 0b0111111111111111;

	// Flash LED on RB15
	while (1)
	{
		_LATB15 = 1 - _LATB15; // toggle RB15
		__delay32(23031250); // 1 second delay
	}
	
	return 0;
}

I don’t use MPLAB – I just write the C code in Notepad++, then use the XC16 compiler to produce a hex file that I download onto the dsPIC with my PICkit2 USB programmer. This is the contents of “build.bat” the build script (batch file) that I used to build the example:

xc16-gcc main.c -mcpu=33FJ32MC202 -Wl,--script=p33FJ32MC202.gld
if errorlevel 0 xc16-bin2hex a.out

The way I manage my dsPIC programs is to create a separate folder for each program I write. I save “main.c” and “build.bat” into that folder, then run “build.bat” in a normal command window to build the program. I them use the PICkit 2 application to transfer the hex file onto the dsPIC.

This entry was posted in Uncategorized and tagged , , , , , , , , , . Bookmark the permalink.

13 Responses to Simple dsPIC33FJ32MC202 example

  1. Pingback: dsPIC33FJ32GP202でLED点滅 | 不規則な信号

  2. Eduardo says:

    Can i use this circuit to program dsPIC33fj128GP202?

    • batchloaf says:

      Hi Eduardo,

      I haven’t tried it, but I think it should be fine for the dsPIC33FJ128MC202. That chip just seems to be an upgrade of the dsPIC33FJ32MC202 with more program memory (128KB rather than 32KB – hence the name change) and RAM. All of the pins used in the circuit shown here are the same for both chips, so hopefully it should work exactly the same.

      Ted

      • Eduardo says:

        Yes, i see on both datasheet, is identical all pins. Sorry for it but u can help me in other question? I dont have this pickit 2, mine is an old simple board, i have the same connector ICSP but my board is 5V. I use this 5V to program the device 3.3v?

        Thanks for your fast answer

  3. Eduardo says:

    Ted, i try program my 3.3v device with pickit2 without sucess. Do u have any idea? The pickit2 doesnt recognize my device, so, i dont connect the vdd pin and connect my devide with my 3.3v source. Now the programmer recognize it, but i cant write the code.

    Do u have an idea for me? Thanks for all

    • batchloaf says:

      Hi Eduardo,

      Sorry, I don’t understand. Did you not say previously that you don’t have a PICkit 2? Are you talking about the PICkit 2 software this time (rather than the hardware device)?

      Please specify which hardware programmer exactly you’re using.

      Also, if you could share a high resolution photo of your circuit with the programmer connected, that might help to track down the problem. A screenshot of whatever errors you’re seeing would be very useful too. You can grab pictures of whatever you’re seeing on the screen using the built in Snipping Tool in Windows.

      You won’t be able to attach images to your comment here, but you can share them on google drive or dropbox or whatever and post the links here.

      Thanks,
      Ted

  4. dinesh says:

    how to led blinking in 1 sec on 1 sec off 2sec on 2 sec off 2sec on 3sec off in microcontroller program..can u tell me the coding..

    • batchloaf says:

      Hi Dinesh,

      Sorry for the delay in responding. With the circuit shown above, the code would be something like this (assuming you want to repeat the sequence you described indefinitely?):

      //
      // main.c - Blinking LED example for dsPIC33FJ32MC202
      // written by Ted Burke
      // last updated 2-10-2014
      //
       
      #include <xc.h>
      #include <libpic30.h>
       
      // Internal FRC oscillator w/ PLL (Fcy=23031250Hz,Tcy=43.419ns)
      _FOSCSEL(FNOSC_FRCPLL);
       
      // Watchdog timer off
      _FWDT(FWDTEN_OFF);
       
      int main()
      {
          // Configure RB15 as digital output
          TRISB = 0b0111111111111111;
       
          // Flash LED on RB15
          while (1)
          {
              _LATB15 = 1;         // LED on
              __delay32(23031250); // 1 second delay
              _LATB15 = 0;         // LED off
              __delay32(23031250); // 1 second delay
              _LATB15 = 1;         // LED on
              __delay32(23031250); // 1 second delay
              __delay32(23031250); // 1 second delay
              _LATB15 = 0;         // LED off
              __delay32(23031250); // 1 second delay
              __delay32(23031250); // 1 second delay
              _LATB15 = 1;         // LED on
              __delay32(23031250); // 1 second delay
              __delay32(23031250); // 1 second delay
              _LATB15 = 0;         // LED off
              __delay32(23031250); // 1 second delay
              __delay32(23031250); // 1 second delay
              __delay32(23031250); // 1 second delay
          }
           
          return 0;
      }
      
  5. mshalash1000 says:

    Pin 19 should be connected to the VSS possibly is a mistake in the draawing!

    • batchloaf says:

      No it’s not a mistake. In an actual production circuit I would certainly ground that pin, but in this very rough prototype, I connected Vss to pin 8 which connects to pin 19 through the internal circuit of the dsPIC. I was therefore able to ground one leg of the capacitor on pin 19. It’s a little bit lazy, but it worked perfectly for me.

  6. mshalash1000 says:

    You could use PGD to pin 14 and PGC to pin 15 as these are 5V tolerant pins.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s