Tuesday, February 10, 2009

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

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

11 comments:

ANYTHINGHERE said...

Respected sir,
I am haja.please send the path link of pcb fabrication in your blog.
yours faithfully
haja

shibuvarkala said...

http://picmicrochip.blogspot.com/2008/09/howto-make-pcb-in-home.html

Anonymous said...

can u plzz tell how i can interface servo motor with my microcontroller
i have to change speed using PWM and also want to change its direction of rotation and want to control my microcontroller with my PC
email: piyush.gig.nitdgp@gmail.com

Unknown said...

Good day Sir. I was wondering if you could help me out. I have written the c code to generate a 120Khz pwm output for the PIC16f877using ccp1 which will then be outputted on RC1. The problem i am having is when i try to run it on Proteus. It runs for a while and then i get a simulation error log with the following: [spice]tran: timestep too small; timestep=8.67362e-19: trouble with node #v:u1:a:1#branch.
Why is this happening and how can i fix the problem. Please help

Unknown said...

Good day Sir. I was wondering if you could help me out. I have written the c code to generate a 120Khz pwm output for the PIC16f877using ccp1 which will then be outputted on RC1. The problem i am having is when i try to run it on Proteus. It runs for a while and then i get a simulation error log with the following: [spice]tran: timestep too small; timestep=8.67362e-19: trouble with node #v:u1:a:1#branch.
Why is this happening and how can i fix the problem. Please help

Unknown said...

Good day Sir. I was wondering if you could help me out. I have written the c code to generate a 120Khz pwm output for the PIC16f877using ccp1 which will then be outputted on RC1. The problem i am having is when i try to run it on Proteus. It runs for a while and then i get a simulation error log with the following: [spice]tran: timestep too small; timestep=8.67362e-19: trouble with node #v:u1:a:1#branch.
Why is this happening and how can i fix the problem. Please help

Unknown said...

PRIYA BUY ORIGINEL PROTUES

seo tutorial said...

oh.. nice article. I had learned something interesting with your post. thanks a lot. keep on updating.

افكار عابرة said...

Thanks a lot
this is post is very useful

SEO Toronto said...

Very Good information on Pulse Width Modulation. thanks for sharing with us.

mahmoud nageb said...

thanx alot for this article, i would ask about how to generate pwm that used as pulse generator in single phase inverte to generate ac source

how to build it using pic16f877 ??