Watch the Video Tutorial

The CCP/ECCP is a multipurpose peripheral module inside most PIC Microcontroller devices. CCP which stands for Capture, Compare and Pulse width modulation while ECCP stands for Enhanced Capture, Compare and Pulse width modulation. The module includes four unique but related peripherals:

  • Capture mode: The peripheral allows timing of duration of an event.
  • Compare mode: The peripheral constantly monitors a timer counter value and compare it to a value set in the application. When they match, it will trigger an event.
  • Pulse Width Modulation (PWM): The peripheral will produce a variable pulse width signal based on a pulse width and period value set in the applications code.
  • The Enhanced Pulse Width Modulation (EPWM): The peripheral will produce a variable pulse width signal based on a pulse width and period value set in the applications code along with Auto-shutdown, Auto-restart, Dead-band Delay and PWM Steering modes. In addition the EPWM can drive various PWM arrangements: Single PWM, Half-Bridge PWM, Full-Bridge PWM, Forward Mode, Full-Bridge PWM, Reverse Mode, Single PWM with PWM Steering Mode

In this article we will discuss the PWM. Pulse width modulation (PWM) is a technique of controlling the amount of power delivered to an electronic load by switching ON and OFF a digital signal. This is the simplest technique that can be used to produce analog voltages from a digital one.
The fraction of the period for which the signal is ON to the total period is known as the duty cycle. The average DC value of the signal can be varied by varying the duty cycle. The duty cycle can be anywhere between 0 (signal is always off) to 1 (signal is constantly on). Suppose, if the signal has +5 V while it is ON and 0 V during OFF condition, then by changing the duty cycle of the signal, the amount of energy transferred to device can be varied. This method is commonly used for controlling speeds of DC motors, brightness of lamps, Sine wave inverters, Digital to Analog Converter (DAC) etc. As you can see in this figure 1 below, when On time is small and Off time long, the Bulb hardly gets any time to turn ON, As the ON time is increased and OFF time decreased it gets brighter.

Figure 1: Controlling brightness of lamps with PWM signal

PIC18F26K20 has two CCP modules, named as CCP1 on Pin RC2 (Enhanced Capture, Compare and Pulse width modulation) and CCP2 on Pin RC1 (Standard Capture, Compare and Pulse width modulation).

Figure 2: PIC18F26K20 CCP modules

The Capture/Compare/PWM (CCP2 in PIC18F26K20) module is very versatile. The Capture and Compare features integrate closely with the 16-bit TMR1 and the PWM feature uses the third timer, the 8-bit TMR2. The CCP2 module has two 8-bit registers, called CCPR2L and CCPR2H. Together they form a 16-bit register that can be used for capture, compare or to form the duty cycle of a PWM stream. The CCP2 module is controlled by the CCP2CON register.

Figure 3: PIC18F26K20 – CCP2CON Satyandard Capture/Compare/PWM Control Register

In PWM mode, the CCP module produces up to a 10-bit resolution PWM output on the CCPx pin. To operate in PWM mode, the CCPx pin must be configured for output. Figure 4 below shows a simplified block diagram of PWM operation.

Figure 4: PWM Simplified Block Diagram – PIC18F26K20

MikroC Pro for PIC PWM Library Functions

MikroC PRO for PIC provides PWM library which simplifies using PWM HW Module
Below is a quick descriptions of PWM routines and functions For more information, please visit online the mikroC pro for PIC PWM library page.

Notes: 

  • Some MCUs have multiple CCP modules. In order to use the desired CCP library routine, simply change the number 1 in the prototype with the appropriate module number, i.e. PWM2_Start();
  • All PWM modules use Timer2 for its operation, so you can not set different frequencies for different PWM modules.

These are the PWM library routines:

  • PWM1_Init
  • PWM1_Set_Duty
  • PWM1_Start
  • PWM1_Stop

PWM1_Init(const long freq)  
This routine Initializes the PWM module with duty ratio 0. Parameter freq is a desired PWM frequency in Hz (refer to device data sheet for correct values in respect with Fosc). This routine needs to be called before using other functions from PWM Library.

Note: Calculation of the PWM frequency value is carried out by the compiler, as it would produce a relatively large code if performed on the library level. Therefore, compiler needs to know the value of the parameter in the compile time. That is why this parameter needs to be a constant, and not a variable.

Example
PWM1_Init(1000); //Initialize PWM module at 1KHz

PWM1_Set_Duty(unsigned short duty_ratio)
This routine sets PWM duty ratio. Parameter duty takes values from 0 to 255, where 0 is 0%, 127 is 50%, and 255 is 100% duty ratio. Other specific values for duty ratio can be calculated as (Percent*255)/100. Before calling this routine, the microcontroller must have a CCP module and PWM1_Init must be called first.

Example
PWM1_Set_Duty(127); //Set duty ratio to 50%

PWM1_Start(void)
This routine Starts PWM. Before calling this routine, the microcontroller must have a CCP module and PWM1_Init must be called first.
Example
PWM1_Start();   // start PWM

PWM1_Stop(void)
This routine Stops PWM. Before calling this routine, the microcontroller must have a CCP module and PWM1_Init must be called first.

Example 
PWM1_Stop(); // stop PWM

Example

In this example, the PWM duty cycle is changed continually with some values after a delay of 3 seconds. The project circuit is shown here below on figure 5.

Figure 5: an LED connected to PWM pin

Below is 80% duty cycle as displayed on the oscilloscope

Figure 6: 80% duty cycle

You can download the full project files (mikroC source code and Proteus Schematic design) below here.  All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).  

Download mikroC Project: PWM_mikroC_Pro_Project

Download Proteus Schematic: PWM_Proteus_Project