** Pulse Width Modulation** (

**) is a technique in which the width of a pulse is modulated keeping the time period of the wave constant. The ON time and OFF time can have any different values in the wave cycles, but the sum of the ON time and OFF time remains same for the entire cycles. PWM is a digital wavethat can be generated using digital circuits which are not capable of generating analog voltages. With the help of the modulation of the width of a pulse in a period of the wave, they can generate any required voltage with the help of a proper filter circuits. The filter circuits are used for generating the voltage corresponding to a modulated wave. Hence the PWM wave is always associated with a filter circuit.**

**PWM**This feature of the PWM wave is making use in so many digital systems like DC motor control, audio devices, simple decoration light controls etc. The PIC18F4550 has an inbuilt PWM module which can generate continuous PWM waves. This project explores the PWM module of the PIC18F4550 and tries generating a sine wave with the help of a filter circuit.Generating a sine wave has a great deal of importance since the sine wave is the most natural waveform and all other kind of waves can be generated as a combination of sine waves with different frequencies and amplitude.

The PIC18F4550 has four PWM output channels and they are P1A, P1B, P1C and P1D. All of them are capable of generating PWM waves at a time. In this project only one of the PWM channels are using. The P1A is the PWM channel in this particular project. A simple example of the of waves generated at this pin is shown below;

As shown in the above figure the ‘Period’ of all the wave remains the same but their ‘Duty-cycle’ varies.

**The period of the wave is the sum of the ‘ON time + OFF time’. Duty-cycle is the percentage of time period for which the logic1 voltage exists in a cycle (ON time), starting from the beginning of the cycle.**

**The PWM is that kind of a wave in which the ON time and OFF time can vary in a cycle but the sum of ‘ON time + OFF time’ remains constant for every cycle.**

The period and the duty cycle for a PWM wave can be calculated generally using the following equations;

*Period = ON time + OFF time*

*Duty-cycle = ON time / (ON time + OFF time) = ON time / Period*

As shown in the figure, the first PWM wave has a Duty-cycle of 50% which means the on-time is exactly half of that the period of the wave. The second wave has 10% Duty-cycle and the third wave has 90% Duty-cycle.

According to the modulation of the width of a pulse in a period of the wave, the PWM can generate any required voltage with the help of a proper filter circuits. The filter circuits are used for generating the voltage corresponding to a modulated wave. Hence the PWM wave is always associated with a filter circuit. When applied to a proper filter the 50% duty cycle can produce half of the maximum voltage of the pulse. If the maximum voltage (logic 1 voltage) of the pulse is 5V then the 50% duty cycle wave can produce a continuous 2.5V. The 10% duty cycle wave can produce nearly 0 voltages and the 90% duty cycle wave can produce nearly 5V.Thus the wave when applied to a filter can continuously produce a voltage which is the average of the voltage in a single period.

**Increasing the Duty-cycle will increase the voltage at the filter device’s output and decreasing the Duty-cycle will decrease the voltage as well**

The filter could be any device which can generate the equivalent voltage of a PWM wave. It normally consists of Low-pass filters, amplifiers, load drivers etc. They generate the voltage corresponding to the pulse width modulation in the wave and can fed that voltage to the load device. The registersassociated with the PWM modules like CPR1, PR2 and TMR2 etc. can be used to set the required Period and Duty-cycle in a PWM wave.

The voltage generated by the PWM wave at a filter device can vary by simply varying the Duty-cycle of the PWM wave. Increasing the Duty-cycle will increase the voltage at the filter device’s output and decreasing the Duty-cycle will decrease the voltage as well. In this particular project the sine wave samples are generated periodically by re-writing the value of the CCPR1 register to vary the Duty-cycle. It is done by generating interrupts periodically with another timer module timer0 and changing the CCPR1 value when the code is inside the timer0’s ISR. It is done as shown in the following figure;

**The voltage generated by the PWM wave in the interval between two interrupts will be a constant value and this time period can be called ‘sampling period’.**

**The values of voltage that appears at each sampling period are simply called ‘samples’.**

A sampling time can have 10 to 500 PWM cycles normally and depends on the particular output device requirement.

**The more the number of PWM cycles in a Sampling time, more stable the output voltage will be**

The Duty-cycle of the PWM wave is increased in a sinusoidal way each times the timer0 fires and to perform such a thing one has to configure the timer0 properly. The consecutive samples that should be generated at each sampling time should follow a sinusoidal pattern. These samples should have the values as if they have been taken by sampling a real sine wave. An example of the sine wave samples is shown following figure in which 10 samples are used to resemble a sine wave.

These values when applied to a filter circuit can generate the sine wave at its output by smoothing the step size. The output of the filter which is fed with the sine wave samples is shown in the following figure. The brown line shows the actual sine wave constructed by the filter circuit.

The values that should be assigned to the CCPR1 register to generate such consecutive samples are actually taken from a look-up table. The look-up table with 50 samples which is used in this particular project to generate the sine wave is shown in the following;

When these values are assigned to the CCPR1 at consecutive sampling periods, it can generate a sine wave at the output of the filter circuits. A zoomed in image of the sine wave that has been generated by the filter used in this project using the above look-up table is given in the following. The sample step value creating the sine wave can be seen in this image.

The steps are not very clear and it should not be since the capacitor in the filter circuit smoothed out the steps to create the sine wave.

__Time period calculations:__

__Sampling time:__

In this project the TMR0 is set to zero and the timer0 is configured as an 8 bit timer with pre-scale value 1:2. Now the timer will count from 0x00 to 0xFF with a clock frequency of FOSC/4 with a pre-scale of 1:2.

The CPU clock frequency is set as 48MHz and hence the time period of a single clock TOSC can be calculated as

TOSC = 1/ FOSC = (1 / (48*10^-6) = 0.02 * 10^-6 seconds.

The time period of the clock which the timer0 module receives can be calculated as

1 / (FOSC/4) = TOSC * 4 = (0.02 * 10^-6) * 4 = 0.08 * 10^-6 seconds.

The timer0 has a pre-scale value of 1:2 and hence the time period should be again multiplied by 2.

Hence the time period at which the value in the 8 bit timer increases = (0.08 * 10^-6) * 2 = 0.16*10^-6 seconds.

The timer0 value increases from 0x00 to 0xFF with a time period of 0.16*10^-6 seconds.

Hence the total time taken for the timer to overflow and cause an interrupt can be calculated as

0.16 * 10^-6 * 255 which gives a value almost equal to 40 micro-seconds.

*The Sampling time = 40us*

__PWM period:__

In this particular project the PR2 is written to a very small value so as to generate small time periods and hence to get more number of PWM cycles per sampling period.

PR2 = 22

Using the equation for calculating the Period of the PWM wave given as;

PWM Period = [(PR2) + 1] * 4 * TOSC * (TMR2 Pre-scale Value)

The TMR2 Pre-scalar value is selected as 1 and the TOSC is already calculated as 0.02 * 10^-6 seconds.

PWM period = [22 + 1] * 4 * (0.02 * 10^-6) * 1

This can give a value almost equal to 2 micro-seconds.

*The PWM Period = 2us*

Thus in a Sampling time number of PWM cycles can be calculated as 40/2 = 20 PWM cycles.

*Number of PWM cycles per Sample = 20*

** Thus the timer0 will generate an interrupt after every 20 PWM cycles**.

This much number of PWM cycles per sample is good enough to generate a stable output sample voltage from a filter device. For the first 20 PWM cycles the value assigned to the CCPR1 register will be the first value in the look-up table and for the next 20 PWM cycles the value assigned to the CCPR1 register will be the second value in the look-up table and so on. Since a single sine wave is made up of 50 samples and the sampling time is 40us, the time required to generate one sine wave can be calculated as;

Time period of one sine wave = 40 * 10^-6 * 50 = 2ms.

Since the frequency is the reciprocal of the time period the frequency of the sine wave can be calculated as;

Frequency = 1 / Time period = 1 / 2 * 10^-3 = 0.5 * 10^3 Hz

*The frequency of the sine wave = 500 Hz*

__PWM Duty-cycle:__

The Duty-cycle in this particular project is varied according to the look-up table whenever a timer0 interrupt occurs. The maximum Duty-cycle (100%) is 2us only since it is the value of the PWM period. The Duty-cycle generated by the microcontroller depends on the value in CCPR1 register and for a maximum value of 2us the value that should be written in the CCPR1 register can be calculated using the equation for PWM Duty-cycle;

PWM Duty Cycle = (CCPR1 * TOSC * (TMR2 Pre-scale Value)

Using the above equation,

The maximum value of CCPR1 = PWM duty cycle max / (TOSC * (TMR2 Pre-scale Value))

The TMR2 Pre-scalar value is selected as 1 and the TOSC is already calculated as 0.02 * 10^-6 seconds.

CCPR1 = 2 * 10^-6 / 0.02 * 10^-6 = 100

*The CCPR1 can be written with any value between 0 and 100*

This look-up table used in this project satisfies this condition since the minimum value in the look-up table is 14 and the maximum value is 86.

__The filter design:__

The following figure shows a microcontroller generating PWM wave which is then used to generate the corresponding analog voltage with the help of a filter circuit.

Source: How to Generate Sine Wave using PWM with PIC Microcontroller- (Part 19/25)