Page 1 of 1

PIC 16F877A PWM

Posted: Fri Apr 09, 2010 10:21 am
by NTSH1996
Hi guys ,
I need to control two separate motors using PIC16F877a. I am using PWM module to control the speed. Can I use both CCP module at the same time ? ( same frequency but different duty cycle ). If can how to do it ?. (Since Both CCP modules use same TMR2 register, I cant understand how to do it. )

Re: PIC 16F877A PWM

Posted: Fri Apr 09, 2010 12:49 pm
by Magneto
Dear Friend ,

You can use both CCP modules in PIC16F877A , at the same tim , for generating two PWM signals. But in the
generated PWM siganals , frequency ( i.e. period value) is same as both of them share Timer2. But you can
have different duty cycles. Look at the flowing equations , which is in the PIC16F87xa data sheet.

PWM Period =[(PR2) + 1] * 4 * Tosc * TMR2 prescaller value

Here PR2 = timer 2 period register , Tosc = oscillator period time , TMR2= timer 2

So you can clear see , you can not generate two separate frequencies ( period values) , from each CCP module and
Period value of PWM module is purely function of Timer2.

Now look at the folowing equation for Duty Cycle , which is in the PIC16F87xa data sheet.

Duty Cycle = (CCPRxL:CCPxCON<5:4>) * Tosc * TMR2 pre scaller

Here CCPRx = CCPR1 or CCPR2

So you can clearly see , Duty cycle of PWM signals is a function of both Timer 2 and CCPRx module (CCPR1 or CCPR2)

So you can have two PWM signals for your two motors with same period but with different duty cycle values.


Here are the necessary steps for configuring a CCP module for PWM outputs.

1. Set the PWM period by writing to the PR2 register
2. Set the PWM duty cycle by , writing to the CCPRxL register CCPxCON<5:4> bits.
( CCPxCon <5:4> contain 2 LSB's of 10 bit duty cycle value and 8 MSB's has to be written CCPRxL register)
3. Make the CCPx pin as an output
4. Set the TMR2 prescaller value and enable Timer2 by writing to T2CON
5. Configure the CCPx module for PWM operation as mention in the data sheet.

It is good practice to read the data sheet of the micro controller , if you have any problem. Most of the time
it will give you solutions.

Re: PIC 16F877A PWM

Posted: Fri Apr 09, 2010 2:20 pm
by NTSH1996
Thanks

Re: PIC 16F877A PWM

Posted: Fri Apr 09, 2010 6:01 pm
by Neo