# PWM (Pulse Width Modulation) Overview

PWM (Pulse Width Modulation) – represents a periodic square wave signal with a variable duty cycle. Duty Cycle (D) – represents a fraction between the time when the system is on active state (“logic state 1”) λ, and the period of the system T.

### 1. Introduction

The difference T- λ (figure 1) represents the time when the system is in off state (“logic state 0”).

 Figure 1: PWM Representation

D = λ / T

The duty cycle can have a value in the [0:1] interval. The average power transferred to the load (controlled device) is directly proportional with the value of D. When D=0 the power transmitted is null, as a matter of fact the circuit is off. On the other hand the load can be in full power when D=1.

The PWM efficiency is always compared with the efficiency of a power resistive circuit. For a load which requires 50% level of power a PWM circuit will use exactly 50% of the power, while a resistive circuit will need 70-80% level power because 20-30% will be waste as heating power.
Disadvantages for PWM are cost and complexity of the circuit which generates it, moreover can be a source of radio frequency interference (RFI) on the global circuit, because PWM is a periodic signal.

### 3.Example of PWM Utilization (PIC16F876A )

The CCP module (Capture/Compare/PWM) is responsible for the PWM functionality. One of the functions for the 16-bit register is PWM Master/Slave Duty Cycle (figure 2).

 Figure 2: PWM Block Diagram for PIC 16F987A

The PWM output signal is determined by period (time base) and the time while the output is logic "1". The frequency of the PWM signal is 1/T, where T is the period (Figure 3).

 Figure 3:PWM Output

This is how to program the PWM period and PWM duty-cycle values:

```PWM period = [(PR2) + 1] • 4 • TOSC •(TMR2 prescale value)
PWM duty cycle =(CCPR1L:CCP1CON<5:4>) •TOSC • (TMR2 prescale value)```

Defining the function for PWM module initialization:

```void InitPWM()	/* init PWM mode */
{
CCP1CON=0;		/*  reset CCP1 module */
CCP2CON=0;		/*  reset CCP2 module  */
T2CON=0b00000010;  	/* postscaler=1:1; timer2=OFF; prescaler=1:16 */
TMR2=0;	              /* reset Timer2
PR2=0xFF;		/* Period: 256 x 16 x 4 x 0.05us=0.8192ms
/* PWM period=[(PR2) + 1]x(TMR2 prescale value)x4xTOSC
CCP1CON=0b00001100; 	/* select PWM mode */
CCPR1L=0x00;		/* PWM duty cycle=(CCPR1L:CCP1CON<5:4>)xTOSCx(TMR2 prescale value) */
TMR2ON=1;                     /* Timer2 ON */
}
void PWM1(unsigned int period, unsigned char factor)
{
if(period <= 255) {
T2CKPS1=0;
T2CKPS0=0;	/* prescaler=1 */
PR2=(unsigned char)period;
} else if(perioad>255 && period<=1023) {
T2CKPS1=0;
T2CKPS0=1;	/* prescaler=4 */
PR2=(unsigned char)(period/4);
} else if(period>1023 && period<=4095) {
T2CKPS1=1;	/* prescaler=16 */
PR2=(unsigned char)(period/16);
}
CCPR1L=factor;
}
void main(void)
{
TRISC=TRISC & 0b11111001;
PORTC=0xFF;
InitPWM();	      /* init PWM mode */
PWM1(4000,100);
}
```

### 4.PWM Application

PWM is very often used for DC motor controlling or light intensity. The control is made with a MOSFET transistor because, unlike to bipolar transistors, it has a low on-resistance and high current carrying capability. A more professional solution is the dedicated MOSFET driver because it is easier to use and also have an internal protection against shoot-through current (figure 4 and 5).

 Figure 4: PWM lamp Controller (Mosfet)
 Figure 5: PWM Cooler Controller

Based on text written by RarCod