Search Projects

Blogroll

Final year degree project for engineers DAC0800 based project circuit diagram and interfacing with pic mirocontroller Final year projects based on microcontrollers on programmable peripheral interface 8255
Microcontroller based different projects abstract and source code
PIC 16f877 microcontroller projects Motion control
PIC16f877 motor controller and other light projects
Tutorials of microcontroller PIC16f877, A step by step easy to use self learning rapidex

Saturday, July 4, 2009

PWM output from PIC 16f877

MikroC gives us a very simple PWM library with 4 functions shown below:
  • Pwm_Init()
  • Pwm_Change_Duty()
  • Pwm_Start()
  • Pwm_Stop()
The expalanation of these function is as; The pwm_init() is used for initializing PWM which takes a argument of frequency in hertz
To Initialize a PWM module at 4KHz do this :Pwm_Init(4000), in this project it is used to get frequency of just 100 HZ at 1Mhz crystal. So i do this by putting value:Pwm_Init(400),
Pwm_Start() and Pwm_Stop() are used for starting and stopping PWM from micro-controller PIC16f877 or PIC 16f877A .pwm_init() to get two ouputs in 16f877
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 used for 0%, 127 is used for 50%, and 255 is used for 100% duty ratio. In this project i am using two PWM outputs from PIC16f877, for user digital inputs to change the duty cycle ratio on real time, i am using two ports of micro-controller. It is just a simple project to understand the operation of PWM, and to understand its working. In this project to measure frequency real time, two counter timer are also used. To monitor the wave form of the pulses real time oscilloscope is also used. As shown in the figures below.
Pulse width modulation PWM code written in c language and to be compiled in MikroC is as below.pwm_init() to get 2 ouputs in 16f877
void main() {

OPTION_REG = 0x80;
// pull up resistors
PORTD = 255;
TRISD = 255;
PORTB = 255;
TRISB = 255;
PORTC = 0x0;
TRISC = 0;
// initialize PWM1 module
PWM1_Init(400);
// initialize PWM2 module
PWM2_Init(400);
PWM1_Start();
PWM2_Start();
while(1)
{
// set new PWM1 pattern ratio,
PWM1_Change_Duty(PORTb);
PWM2_Change_Duty(PORTD);
// set new PWM2 pattern ratio,
}
}



15 comments:

Anonymous said...

will both PWM1 and PWM2 work only sunchronously... is there anyway to desynchronise them.. please help me

Dr.Rana said...

you can have different frequencies and duties cycle of PWM1 and PWM2.
You can have any delay between their output. So , you have freedom to do a lot with PWM channels.

IF your question is different and got not answeer, then write in detail, what you want.

jeevithan said...

a program that will control two LEDs using PWM. Every time the external interrupt button is pushed the program reads the analogue input and uses the value to set up the duty cycle of one of PWM LEDs (hint--> you must use A/D module). Thus, the external interrupt toggles between the two PWM LEDs. The program also uses other two LEDs for signalling which PWM LED is the active output.

do u have any idea how to initiate the pwm ,,i did like u said,but its not working,,, can u help me please,,,

Dr.Rana said...

yes i can help you. Do you need whole program or just a routine of this? Whats you e-mail id?

yazan said...

Dr Rana my name is yazan from iraq mechatronics ENG i want UR email for helping if u can?

Dr.Rana said...

@ YAZAN
My e-mail id is
rghkk@hotmail.com
and
chatting id is
rghkk1@yahoo.com

come and discuss your projects problem with me using any way.
1. write here in comments
2. e-mail me your project with circuit diagram and code
3. live discuss the microcontroller related projects for mini projects or full final year degree projects based on microcontroller , control and data logging.
regards

Anonymous said...

Mr rana can u help me i need to generate a pwm with varying duty cycle can u send me the cde my mail id is suhana22@ymail.com

lina PWM PIC16f877 microcontroller said...

dear Dr.Rana



im lina..



i have a question to ask u. How to shift 2nd pwm in the certain time at 1st pwm. It means when t1=4.415us, pwm 2 is turn on and turn off at the same value of time pwm 1 which is t1=6.159us.The frequency is same (105KHz) but duty cycle for pwm 1 is 64% and pwm 2 is 47%. The period time is 9.523us. I used PIC 16F877A.



According to my progmming below, i just get 1 pwm. but the problem is the 2nd pwm i dont know how to shift it. or maybe u can give me some idea to solve my problem.





void main () {

portC = 0x00
Pwm2_Init(105000);
Pwm2_Start();
do(
Pwm2_Change_Duty(164.73);
}While(1);
}



please help me..



thank you..



kindly, lina

Dr.Rana said...

@ lina PWM PIC16f877 microcontroller :-
See PIC 16f877 have two PWM channels seperate.
You can use them independently without any problem.
the code will be like
Pwm1_Init(105000);
Pwm2_Init(105000);
Pwm1_Start();
Pwm2_Start();
duty_cycle_value_for_PWM1 = 68;
duty_cycle_value_for_PWM2 = 47;
Pwm1_Change_Duty(duty_cycle_value_for_PWM1);
Pwm2_Change_Duty(duty_cycle_value_for_PWM2);
do(

if (change_of_duty_cycle_is_required) {
Pwm1_Stop();
Pwm2_Stop();
Pwm1_Change_Duty(duty_cycle_value_for_PWM2);
Pwm2_Change_Duty(duty_cycle_value_for_PWM1);
Pwm1_Start();
Pwm2_Start();
}
else
{
Pwm1_Stop();
Pwm2_Stop();
Pwm1_Change_Duty(duty_cycle_value_for_PWM1);
Pwm2_Change_Duty(duty_cycle_value_for_PWM2);
Pwm1_Start();
Pwm2_Start();
}
}While(1);
}

I hope this algorithm will help you in the coding of actual project.
if you still feel any problem plz ask.

mdh741 said...

nice

natts said...

HI friends,pls help me if you can...
i m going to control the dc motor using pic16f877a.i want source code for htc compiler as well real time hardware connection..what are all steps i need to follow...please help me to do my project

natts said...

my e-mail is nataraj87@gmail.com

for pwm details

pmbsip said...

i m balan

sir i m doing project in PIC16f877 , for that i need code for comparing the normal voltage with reference voltage please help me in this problem

pmbsip said...

my email is
pmbsip33@gmail.com

roy blessing chibwe said...

hie .. sir i am doing an sms based alarming system whereby any anormally that happens in the plant has to be communicated by sms to several supervisors. i now have a general idea of my hardware set up. but i cant seem to write a code that will link the plant sensors to the microcontroller and to the gsm300 module for sending the various sms to the targeted peopl. may u please help me with the codes. i will really appreciate it. my email is roybch@gmail.com

Post a Comment

Hi: This is PICINF.blogspot.com comments section. Here you are requested to write your question or problems in detail. Write the question in such a way that, it have all necessary information, sothat i can understand and reply.If you want to send pictures and codes then e-mail me (rghkk@hotmail.com).Thanks.