Problems with Microcontroller Projects? You may face many Problems, but do not worry we are ready to solve your Problems. All you need to do is just leave your Comments. We will assure you that you will find a solution to your project along with future tips. On Request we will Mail you the Codes for Registered Members of this site only, at free service...Follow Me.

Pulse Width Modulation or PWM Tutorial using PIC 16F877, Proteus and Mikroc



Pulse width Modulation or PWM is one of the powerful techniques used in todays control systems.
It is used for speed control of motors, used for measurement , communication and power control.

Pulse-width Modulation is achieved with the help of a squarewave whose duty cycle (ON time vs OFF time) is changed to get a varying voltage output as a result of average value of waveform. See Picture

Consider a square wave shown in the figure above.

TON is the time for which the output is high and ToFF is time for which output is low. Let Ttotal be time period of the wave such that, Ttotal = TON +ToFF

Duty cycle of a squarewave is defined as
D = TON / (TON+ToFF)= TON/Ttotal

See output voltage varies with duty cycle

VOUT = D x VIN

VOUT = (TON/Ttotal) x VIN

we see from the final equation the output voltage can be directly varied by varying the TON value.

If TON is Zero , VOUT is also Zero.


Now lets see how to write a code for controlling PWM Module of a PIC Micro Controller using the Mikroc compiler.

MikroC provides a very simple PWM library with 4 functions shown below
  • Pwm_Init
  • Pwm_Change_Duty
  • Pwm_Start
  • Pwm_Stop
The pwm_init used for initializing PWM which takes a arguement of frequency in hertz

To Initialize a PWM module at 2KHz do this :Pwm_Init(2000);

Pwm_Start and Pwm_Stop are used for starting and stopping PWM .
Pwm_Change_Duty Changes PWM duty ratio. Parameter duty takes values from 0 to 255. It can be calculate using equation (Percent*255)/100. See 0 is 0%, 127 is 50%, and 255 is 100% duty ratio.

Take a look at the following simple program


// microcontroller : P16F877A
// PWM module is set on RC2 Pin No 17.

unsigned short i;

void main() {
PORTC = 00; // Set PORTC to $FF
TRISC = 0; // PORTC is output
Pwm_Init(5000); // Initialize PWM module

Pwm_Start(); // Start PWM

while (1) { // Endless loop
for(i=0;i<=255;i++)
{ Pwm1_Change_Duty(i);
delay_ms(10); }
} }

This program creates a pwm on pin17 of PIC16F877A .Hope you found this tutorial useful .If you have any doubts about the program .Leave a comment . click to enlarge figure. set CRO channel A Volts to 1 V and time/div to 50us

Download DSN for Proteus

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Recent Comments

Popular Projects

Give Support

Give Support
Encourage Me through Comments

Microcontroller Projects

Total Pageviews