PIC 16F877A PWM

Microcontroller Topics
Post Reply
NTSH1996
Sergeant
Sergeant
Posts: 19
Joined: Sat Jan 02, 2010 12:57 am

PIC 16F877A PWM

Post by NTSH1996 » Fri Apr 09, 2010 10:21 am

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. )
User avatar
Magneto
Major
Major
Posts: 430
Joined: Wed Jul 15, 2009 1:52 pm
Location: London

Re: PIC 16F877A PWM

Post by Magneto » Fri Apr 09, 2010 12:49 pm

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.
NTSH1996
Sergeant
Sergeant
Posts: 19
Joined: Sat Jan 02, 2010 12:57 am

Re: PIC 16F877A PWM

Post by NTSH1996 » Fri Apr 09, 2010 2:20 pm

Thanks
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: PIC 16F877A PWM

Post by Neo » Fri Apr 09, 2010 6:01 pm

Post Reply

Return to “Microcontrollers”