How to Interface PWM with dsPIC30F4011

Interfacing PWM with dsPIC30F4011

Tags: Generating PWM with dsPIC30F4011, Circuit Diagram to GENERATE PWM USING dsPIC30F, C Program to generate PWM USING dsPIC30F4011, Testing the PWM with dsPIC30F4011,
Overall rating
DsPIC30F4011 Primer Board

The dsPIC30F4011 Primer board is specifically designed to help students to master the required skills in the area of embedded systems. The kit is designed in such way that all the possible features of the microcontroller will be easily used by the students. The kit supports in system programming (ISP) which is done through USB port.

Microchip’s dsPIC30F (dsPIC30F4011), Primer Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of High speed 16-bit Microcontrollers.

PWM (Pulse Width Modulation)

Pulse width modulation (PWM) is a powerful technique for controlling analog circuits with a processor's digital outputs. PWM is employed in a wide variety of applications, ranging from measurement and communications to power control and conversion.

Interfacing PWM

Figure 1 shows four different PWM signals. One is PWM output at a 25% duty cycle. That is, the signal is on for 25% of the period and off the other 75%. Next shows PWM output at 50%, 75% and 100% duty cycles, respectively. These three PWM outputs encode three different analog signal values, at 10%, 50%, and 90% of the full strength.


Fig. 1 PWM Outputs

Interfacing PWM with dsPIC30F4011

We now want to generate a PWM in dsPIC30F Primer Board at a particular frequency. Pulse Width Modulation is a technique for getting analog results with digital means.

Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width.

Pin Assignment with dsPIC30F4011


Circuit Diagram to Interface PWM with dsPIC30F


Source Code

The Interfacing PWM with dsPIC30F4011 program is very simple and straight forward, which generates a pulse pattern in a particular frequency. An ADC signal is used to varying the duty cycle of PWM signal. The C program is written in Mplab software & it executed with Microchip C30 C compiler.

C Program to generate PWM in dsPIC30F4011


Title : Program to generate PWM




#include <P30F4011.h>







#define      Min_Freq      4000          // 4KHz.

#define      Max_Freq      15000         // 15KHz.


#define      FOSC          5000000

#define      FREQ_val      (FOSC/Min_Freq)

#define      Offset        (FREQ_val/2)


#define      BAUDRATE      9600

#define      BRGVAL        (((FOSC/BAUDRATE)/16)-1)


void readADC();


unsigned long int freq_value=1000,freq_val,



int main()


    PTCON = 0x8002;

    PTPER = Offset; // Minimum 5Hz to Maximum 500KHz.


    PWMCON1 = 0x0010;

    PDC1 = Offset;



    ADPCFGbits.PCFG0   =      0;

    TRISBbits.TRISB0   =      1;


    ADCON1             =      0x80EC;

    ADCON2             =      0x0004;

    ADCON3             =      0x0004;

    ADCHS             =       0x0000;


    IFS0bits.ADIF       =       0;

    IEC0bits.ADIE       =       0;





       freq_value = adc1_val;

       freq_val = (long)(FOSC) / (freq_value);


       PTPER = freq_val/2;

       PDC1 = freq_val/2;





void readADC()


    ADCHS = 0x0000;

    ADCON1bits.SAMP = 1;


    ADCON1bits.SAMP = 0;




    adc1_val = 4000+(ADCBUF0*(float)(10.752688172044));



    ADCON1bits.DONE = 0;




To compile the above C code you must need the Mplab software & Microchip C30 C Compiler. They must be properly set up and a project with correct settings must be created in order to compile the code. To compile the above code, the C file must be added to the project.

In Mplab, you want to develop or debug the project without any hardware setup. You must compile the code for generating HEX file. In debugging Mode, you want to check the port output without dsPIC30F Primer Board.

The PICKIT2 software is used to download the hex file into your microcontroller IC dsPIC30F4011 through USB.

Testing the PWM with dsPIC30F4011

Give +9V power supply to dsPIC30F Primer Board; the PWM port line is connected in dsPIC30F Primer Board. When the program is downloading into dsPIC30F4011 in Primer Board, the PWM output is generating at a particular frequency.

If you are not reading any PWM output, then you just check the jumper connections. Otherwise you just check it with debugging mode in Mplab. If you want to see more details about debugging just see the videos in below link.

  • How to Create & Debug a Project in Mplab using dsPIC30F.

General Information

  • For proper working use the components of exact values as shown in Circuit file. Wherever possible use new components.
  • Solder everything in a clean way. A major problem arises due to improper soldering, solder jumps and loose joints.
  • Use the exact value crystal shown in schematic.
  • More instructions are available in following articles,
       - User Manual of dsPIC30F Primer Board.
       - Create & Debug a project in Mplab using dsPIC30F.

Join the World's Largest Technical Community

we respect your privacy.