 Hello, Myself Ravindra Chauhan, Assistant Professor, Department of Electronics Engineering, Walsh Chandist of Technology, Solapur. So in this session, we will see the PWM module in peak 16F877 microcontroller. At the end of this session, student can demonstrate the operation of PWM in peak 16F877. Student can implement the PWM functionality in different applications. The outline of this session, the first, the special function registers associating with the PWM module, the how PWM module operates and how PWM module can be set up for the different applications. PWM module in peak 16F877, the PWM stands for the pulse width modulation. So in PWM mode, the PWM output produces on CCPX pin. Now this X can be either 1 or 2, because in peak 16F877, the two PWM modules are provided. The one is called CCP1, which stands for capture compare PWM mode, means the CCP module can operate in three different modes, that is the capture mode, compare mode and PWM mode. So in this session, we will concentrate only on the PWM mode. So two PWM modules can be defined, that is the CCP1 and CCP2. So as far as these two modules are concerned, the working of both, that is CCP1 and CCP2, is same. So as an example, we will consider the CCP1 module. So the CCP1 pin is multiplexed with the port C pin called RC2 slash CCP1. So in PWM mode, it is producing the output on pin RC2. So it is very necessary to configure the RC2 pin as output. So to configure the RC2 as an output pin, three two bit must be cleared. Then here the one of the PWM output is shown and in the peak 16F877, the two parameters are defined for this PWM output. The one is duty cycle, which is defined as the on period of the output. So duty cycle is equal to the on time of the wave, while period of the output is defined as on time plus off time. Now the registers associating with the PWM module, the first is CCP1 con register for CCP1 module. Similarly, there is CCP2 con register for CCP2 module, but the working of both the modules are same. So the CCP1 con register is to be used to control the operation of the CCP1 module. So CCP1x and the CCP1y, these two bits are used to store the two LSB bit of the PWM duty cycle. Now to get the PWM output, there is need to define the two parameters. One is the duty cycle for that the 10 bits are provided and for period 8 bits are provided. So 10 bits of the duty cycle has to put in the CCPR1L where it holds the higher 8 bits and the lower 2 bits must be kept in CCP1x and CCP1y bit of the CCP1 con register. And the lower 4 bits are called the mode select bits, CCP1 mode select bits which are CCP1M3 to CCP1M0. Now to operate the CCP module in PWM mode, make CCP1M3 and CCP1M21. So when these bits are 11, xx means the bit M1 and M0, whatever it may be either 1 or 0 doesn't matter. If CCP1M3 and CCP1M2 both are 1, then it will select the PWM mode. Now the PWM period is specified by writing to the PR2 register. Now how to calculate the PWM period? So to calculate the PWM period use the formula shown here which is PWM period is equal to the value stored in PR2 plus 1 multiplied by 4, why 4? Because the external clock that is the f oscillator is gets divided internally by 4, that's why this multiplying factor 4 is there multiplied by the time period of clock frequency and the pre scale value decided for the timer too. So using this one can calculate the PWM period, but normally the PWM period is the known quantity. So to get the required PWM period one can find the required value to be put in the PR2 register, that is nothing but the period of the PWM. Similarly, the PWM duty cycle is specified by writing to the register CCPR1L and the CCP1 con fifth and fourth bits. So duty cycle is too specified by making the use of 10 bits and the CCPR1L contains the 8 most significant bits and the CCP1 con 5 and 4 contains the 2 least significant bits. So equation to be used to calculate the PWM duty cycle in time is shown here, PWM duty cycle is equal to the CCPR1L which holds the higher 8 bits and the CCP1 con fifth and fourth bit which holds the 2 LSB bits. So the value stored in CCPR1L and CCP1 con fifth and fourth bit multiplied by the oscillator frequency time multiplied by the timer to pre scale value. Again now the duty cycle is always known quantity, so to get the required duty cycle the value to be put in CCPR1L and CCP1 con can be calculated by making this formula. Now this is the PWM block diagram which explains the operation of the PWM module in the peak 16F877. Now the two blocks are shown here, the one is CCPR1L and CCP1 con fifth and fourth and the PR2 register. So PR2 register is used to decide the period of the PWM output, the period is equal to on time and off time. And the duty cycle is to be specified in this CCPR1L and fifth and fourth bits of CCP1 con register. Now internally whatever the value stored here that is the duty cycle value that will be gets internally lashed in the CCPR1H and the timer 2 it is internally used to get the required time delay. So here timer 2 and the value in the CCPR1H will be continuously compared by making the use of the comparator. So when the both the value means the timer 2 value and CCPR1H value are matched means both are same then this comparator will resets this flip lock and Rc2 slash CCP1 pin will go at the logic 0. At the same time the another comparator compares the value stored in the period register that is the PR2 and the timer 2. So when the value in timer 2 and the value in PR2 matches means when both are equal then the comparator will set this flip lock. This again after sometime this Rc2 pin will go to the high. So periodically this Rc2 and Rc2 pin will go to 0 and 1 and so on. Now how to use the PWM for the different applications? So how PWM module can be configured? So for that first set the PWM period by writing to the PR2 register. Now the values stored to be stored in the PR2 registers can be used by using the formula for PWM period which is equal to PR2 plus 1 multiplied by 4 multiplied by oscillator time multiplied by timer 2 prescale value. Next is you set the PWM duty cycle. Now how will you set the PWM duty cycle? For that use the register ccpr1l and the lower 2 bits of ccp1 con fifth and fourth bits. Then make the ccp1 pin as an output by clearing the trace 2 bit. Set the timer 2 prescale value and enable the timer 2 by writing to t2 con register. And then configure the ccp1 module for PWM operation. Now you just think that how will you make the ccp1 pin as output pin and how will you configure the ccp1 module for PWM operation? Now to make the ccp1 pin as output clear the trace 2 bit. To configure the ccp1 module for PWM operation use ccp1 control register and make ccp1 m3 to ccp1 m0 is equal to 11xx. These are the references used for this session. Thank you.