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.

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

18 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.

  7. Aemika says:

    Dear Ted,

    I would like to implement sine wave PWM to drive brushless DC. i have seen your post in which it was mentioned, sine PWM could be genrated using Output compare module.
    I would wish to generate sine PWM( phase shift 60 deg) of 20khz. Currently I did using sine look up table(values 32767 to -32767 of about 4k samples so as to ensure fixed point arithmetic). I am using centre aligned PWM, with Fcy = 10Mhz, 1:1 prescalar. Is there a better way to generate sine wave internally and then get PWM pulses varying sinusoidally to control the inverter bridge to drive motor? At present I am running open loop. Could you please provide me some inputs or any sample code to achieve the same.
    I am using MPLAB X IDE and my controller donot have any LED’s to test this basic application you have posted above.

    Regards,
    Aemika

    • batchloaf says:

      Hi Aemika,

      It sounds like you’re already doing it more or less as I would suggest – i.e. using a sine lookup table. Using the sin() function from math.h, or using another method of generating sine values on the fly is likely to be considerably slower.

      What’s is the status of your current implementation – is it working at all? If not, what aspect of it seems to be the problem? Are you seeing sinusoidal variation in your PWM duty cycle? It can be useful to try putting the PWM output through a simple RC low-pass filter to change it from PWM into a sine wave that you can view on an oscilloscope.

      Ted

  8. Aemika says:

    Dear Ted,

    Sorry for the delayed response. I could generate PWM successfully that are centre aligned. However I am not willing to drive my motor at high speed. The variation of duty cycle seems to be a problem. I used Look UP table for generating the sine wave. Have you by any chance an example or implementation with dsPIC controller for generating sine PWM with phase shift between two? If so kindly let me know.
    Another task parallel though its unrelated to this forum I mention it, I am trying to do is generate PWM and read the PWM in Simulink environment to perform PIL(Processor in Loop). I am not sure how do I read the PWM from the controller in to any GUI based environment. Could you provide me some inputs? Which of communication would be the easiest way to implement?

    Regards,
    Aemika

  9. Aemika says:

    Dear Ted,
    Successfully I generated the PWM and motor runs at 100rpm which is very slow for back emf detection. I would like to ramp from 100rpm to 600rpm with the current implementation. Have you by any chance implemented V/F control or Ramp for open loop start up using the Look up table to run motor in open loop? Even if you have any suggestions or ideas kindly provide them.
    Regards,
    Aemika

  10. Samant says:

    Dear Ted,
    I have developed a sine LUT with 256 samples in dsPIC33f controller. But I am not very sure how to step in throught the to vary the frequency linearly and slowly(not very fast). Increasing step size in steps of 1 creates very high frequency difference.
    Hence I would like to know the easiest way to step in through the LUT(256 size), to read the sine LUT from 10hz to 1000Hz. How can I achieve this ? Please do provide your valuable inputs.
    Regards,
    SM

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