PWM configuration using MPLAB Code Configurator

Apr 13, 2016 By Mayank Shukla

PWM configuration using MPLAB Code Configurator

There are many parameters in an IC which needs to be configured very precisely so that the desired output can be observed. For the same you might have to go through lot of meticulous details from the data sheet and few details become become very confusing for person who is still trying to grab the concept. The image below gives some insight into the datasheet we need to go through to get our peripherals running correctly

Introduction

Thanks to assisting tools like MPLAB Code Configurator these meticulous tasks can be reduced and let the software take control of configuring the bits properly. This also reduces chances of manual error, so I would highly recommend using it even if you know how to do it manually. I have chosen the PIC16F1824 IC for the demo purpose since this IC was readily available with me (You can choose whichever IC you like, MPLAB Code Configurator will work with most of processors).

You should first create a sample project which is explained in this page, where you select the IC and some basic initialization code (like oscillator source, etc) which again can be done using MPLAB code configurator.

There are many configurations in PWM mode itself, but for start we will stick to simply running PWM with fixed duty cycle of 50% and observing the same on oscilloscope.

Once you are done with creating new project go to Tools -> Embedded -> MPLAB code configurator (In case you don't see this tool in your embedded toolbox then you have to download the tool after installing MPLAB).

Code Configurator selection

Starting with MPLAB code configurator

I have selected internal oscillator as clock source at frequency of 500kHz.

Now select the PWM peripheral from the options available in Device resources module as shown in image below (just double click on the module you want to select).

The resources required for configuring the PWM are TMR2::Timer and the CCP3:PWM modules. As you can see in the image below, just by changing the values we can see the result that we can expect after running the code.

You can simply enter the desired timer period and the code configurator will do the hard work in calucating the actual values that will be placed into its registers (this is the biggest headache that is taken care by configurator). All of the details you can observe in snapshot below.I have not changed the pres-calar since we are just testing it. If you want you can tweak it to get the PWM with desired time period.

Right now I am configuring the PWM to run at 500Hz (ie:- HIGH for 1msec and LOW for 1msec).

Getting hands on code

You are done with configuring all the registers and your PWM code is ready to be tested. Just click on generate code and you will be able to see MCC generated folder added to source files.

Now just few lines of code and we are done.

Till now what we have done is generated functions which will configure the PWM registers, but we need to call those functions in main file for configuration to complete.

Now go to main.c file

and add these functions in main function (this will set all the registers)

OSCILLATOR_Initialize();
PIN_MANAGER_Initialize();
TMR2_Initialize();
PWM3_Initialize();

These functions were auto generated when you pressed the generate button. Now simply burn the code on chip (i have used Pickit 3 for the same) and you will observe the waveform on screen as soon as the timer starts running.

Finally the code should look something like this

void main(void) {
//main function

OSCILLATOR_Initialize();
PIN_MANAGER_Initialize();
TMR2_Initialize();
PWM3_Initialize();

//Write your additional code here
}

Looking at Hardware

When we chose particular peripheral (in our case PWM) the output pin is always fixed by the manufacturer of IC. In our case its pin no 11. If you look at the pin manager box of code configurator you will notice that its turned green and a lock appears on it, that means that now we cannot use this pin for any other than for PWM. The pin actually shows up with the label CCP3 in green to show that the CCP3 peripheral now controls the I/O pin.

Now, Build the code and upload it into the IC & viola! You have successfully configured the IC to run PWM at 500Hz with 50% duty cycle. Make sure that you have the programmer supplying power to your PIC.

Output

Now you should be able to see a nice waveform on your CRO (I have used a logic sniffer in my case).

Final Thoughts

MPLAB Code Configurator reduces a lot of effort and pain that programmers used to face. I am not exaggerating when I say it reduces the work of days to minutes. Not to forget the reduction in error.

Comments