Some C code for the dsPIC30F4011

This is the C code from my main.c file:

#include <p30f4011.h>


int main()
	long int n;

	TRISD = 0b11111110;
	LATD = 0;	

	for(n=0 ; n<5000 ; ++n)
		if (_LATD0) _LATD0 = 0;
		else _LATD0 = 1;
		if (_LATD0) _LATD0 = 0;
		else _LATD0 = 1;
	return 0;

The End

This entry was posted in PIC. Bookmark the permalink.

11 Responses to Some C code for the dsPIC30F4011

  1. Help Needed using Dspic30f4011!!! ,

    Hello Sir ,
    I m working on my senior project and I m in real need for help with programming dspic30f4011. I need to know how do I write a code for capturing volatge ands current from battery of 12 V.

    I have already deisgn the circuitry to sense battery voltage and Current

    My circuit has 2 output sugnals (current and voltage) which connect to the ADC channels of DSPIC ..

    I also need to set the duty cycle and the output should be at PWM port of DSPIC which inturn is connected to the Mosfet gate driver..

    If any one has feed backs or sample code to start with I would highly appreciate the effort ..

    Thank You
    An engineer in Need !!!

    • batchloaf says:

      Hi Engineer In Need,

      I would need to know a little bit more about what you’re trying to do before I can offer much useful advice. However, here are a couple of general points:

      1. The dsPIC cannot directly measure analog input voltages above its own supply voltage. Since its maximum supply voltage is 5.5V (as far as I recall), it won’t be able to measure a voltage anywhere near 12V. However, you can use a simply potential divider circuit to provide the dsPIC’s analog input with a voltage that is a fixed fraction of the battery voltage. For example, you could connect your potential divider as follows: Positive battery terminal -> 50kOhm resistor -> 10kOhm resistor -> Ground. Then just connect the dsPIC’s analog input to the point between the two resistors and it will be seeing one sixth of the battery voltage. Obviously, the accuracy of the resistor values will affect how close to one sixth it really is.
      2. The dsPIC’s analog inputs measure voltage, but not current. In order for the dsPIC to measure a current, it must be converted into a voltage. The simplest way of doing this is to use a current sensing resistor, which is a low value resistor connected in series with the load in which you are trying to measure the current. Because you know the resistor value, you can simply measure the voltage across it and use Ohm’s Law to determine the current that’s flowing. The appropriate value of current sensing resistor depends on the application (size of current, required accuracy of measurement, etc). It may or may not be appropriate for your purposes. If you do use one, you would probably just connect it between the load and ground. If you’re switching the load current on and off with PWM, that will complicate matters a bit.

      Can you give some more information and maybe a rough circuit diagram? What type of load is connected to the battery and what exactly are you trying to control with the PWM signal?

      • Thank You very much sir for prompt reply.
        if its ok I really want some more help from You . Coz My university doesn’t provide much help with senior project.

        This is the diagram for my first stage :

        I have Connect a logic output of DsPIC 30f4011 through the gate driver IC, to drive the gate of the power MOSFET
        then I have to Program the DsPIC 30f4011 to produce a suitable duty cycle signal.

        stage 2:
        Then I have to modify the programming of the Dspic to produce a signal that is proportional to a voltage
        applied to an A/D input of the Dspic. I also have to connect a potentiometer to provide a 0–2.5 V signal at this input, and verify that Dspic produces a signal with controllable duty cycle at its logic output.
        This is my Main circut Diagram :

        To mention, I have some working experince in Dspic basic but not with Duty Cycle or PWM..

        It would be really great if u can help me

        I am xavier Pereira
        Thank You very much

    • batchloaf says:

      Oh yeah, I should also have mentioned that you can find a few useful examples of analog input and PWM control for the dsPIC30F4011 on my robotics blog,

      Here’s the exact page you should look at:

    • batchloaf says:

      Hi Xavier. Did you look at the page I suggested on my Robotics blog? Here’s the link again:

      In the section on PWM, there is a complete code example for controlling PWM duty cycle with a potentiometer (read via an analog input). Actually, it reads the voltages from two potentiometers and controls the duty cycles of two PWM outputs. The code I provided in this example is for the C30 C compiler, which you can download from if you don’t have it already. The download link you want is the one labeled as “MPLAB C Compiler for PIC24 and dsPIC”. It’s free to download for academic use, but you might have to register on the website to get access to download it.

      The example code does almost exactly what you’re trying to do, although you may need to change the period of the PWM signal or modify the relationship between input voltage and duty cycle. Before you worry about those details though, try compiling the code exactly as it is and then see if the dsPIC is doing what it’s supposed to. Once you have that working, I can provide some further info if you need help adjusting the code to the specific requirements of your application.

  2. thank you very much Sir, I will start Compling the porgram ,and Yes I have read your link and i will try to understand it properly and start with the parameters which I need ..
    I will get back to you , If I have any more problems and when I m ready with my project.

    Its was really Kind of you helping a stranger like me..
    Thank You once more

  3. Marguerite says:

    Hello batchloaf,

    i would also like to measure AC current from a PFC using an transducer and dspic33fj32mc204.
    I have make some measurement of DC current before which is much easier.
    Please can you help me? I will be grateful.

    • batchloaf says:

      Hi Marguerite,

      In most cases where I’ve seen people monitoring AC current using a microcontroller, some kind of current transducer component has been used. The kind of thing I’m thinking of are like the ones here:

      If you can’t wait to get your hands on one of those, there are other ways you can do it, but they’re probably gong to be a little more complicated. For example, you could use a very low-resistance (e.g. < 1 Ohm) current-sensing resistor in series with the current you want to measure, which will produce a time-varying voltage which is proportional to the current. You can therefore measure the current by connecting the resistor voltage to an analog input. The drawback is that if your current is AC, then the resistor voltage varies above and below zero, which will be a problem for analog input. You might need some signal conditioning (e.g. using an op-amp) to level shift the signal.

      Do you just want the RMS amplitude of the current, or do you need to input it to the dsPIC as an AC signal? i.e. Do you need to measure the current varying up and down at 50/60 Hz, or do you just want to know how big it is?


  4. ARCHANA says:

    I have read the reply given by you to Xavier’s question posted on August 24,2011.
    We are combining voltage sensor output and current sensor output and giving it as an input to ADC pin of the DSP processor TMS320F2808 which generates a PWM wave with the required duty cycle which controls the gate of the MOSFET. Please help us in giving a code .

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s