This web site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more.

The CCP module (PWM)

Last update: 31/07/2015

As we already know the CCP module can be set to generate an hardware PWM (pulse width modulation) signal.
This means that the microcontroller starts to generate a signal of fixed period (frequency) where we can at any moment change the duty cicle.

16f877 16f887


For our samples we are going to use a very common microcontroller: the pic 16F877. At the moment it can be replaced by the 16F887, cheaper and with extended modules.

We can see that we have a couple of CCP pins: 16 and 17 where we can obtain a pwm signals.

Using very common languages as microbasic or mikroc we can set and manage this module/function very quickly and simply:

TRISC = 0x00                     ' sets the portc as output)
PWM1_Init(70000)             ' Initialize PWM1 module at 70KHz  
PWM2_Init(70000)             ' Initialize PWM2 module at 70KHz
PWM1_Start()                     ' start PWM1 
PWM2_Start()                     ' start PWM2
PWM1_Set_Duty(127)        ' Set current duty for PWM1 at 50%
PWM2_Set_Duty(64)          ' Set current duty for PWM2 at 25%

As a result we'll obtain a couple of fixed pwm on pin 16 and 17 according to the following picture:

PWM Picmicro  

At any moment, during the programming procedure, it is possible to change the duty cicle of the pwm signal or we can stop it using a simple command (mikrobasic):


The implementation of a Pwm signal can be considered a quite easy matter; just very few instructions and the module is working properly.

Using another very popular language, Mikroc, the job to be done is almost equivalent:


void main() {
PORTC = 00; 
TRISC = 0; 

Stopping the pwm signal with a:


whenever we desire to stop it.

The use of high level languages simplifies considerably all the procedures to obtain such a results. Normally, using lower level languages (or sometime freeware compilers), the user have to set all registers values necessary to set the module and to obtain the wished periods and duty cicles.

 In this case, in fact, we must:

  • choose the period of the signal setting the register PR2
  • setting the module to act as pwm generator (T2CON)
  • enable the timer of the microcontroller and set the prescaler value (CCP1CON)
  • set the duty cicle of the signal (CCPR1L)

 Another interesting characteristic of some microcontrollers (valid only for CCP1 pins) is the presence of an enhanced PWM module. As per the fact that very often the pwm signal is used to control half or full bridges driving stepper or dc motors. Setting properly the PWMCON1 register, the pwm signals are generated in a shifted mode (a small delay is inserted). Switching off both branches of the bridges is given enough time to prevent concurrent flows of current on both branches and short circuits.

Diritti riservati 2008-2015. Tutti i marchi riportati sono detenuti dai legittimi proprietari. Tutto il materiale e` liberamente utilizzabile ma non per scopi commerciali. Non si assume alcuna responsabilita` sul materiale pubblicato per eventuali danni diretti o indiretti che possano derivare dall`utilizzo di schemi, progetti o altro materiale presente nel sito. Inviare una mail a: (contenuti) (webserver) per domande e/o suggerimenti.