Setting up PWM (ECCP) on PIC

Status
Not open for further replies.

Slowmo

New Member
I'm using PIC16F886 and there is one thing I don't quite understand in setting hardware PWM up. In datasheet there are detailed instructions on how to do this, but maybe somebody can explain why I first need to disable the PWM pin output driver. Then after everything is set up, it should be enabled after an interrupt

Here are the steps from the datasheet:

In my understanding, Timer2 even shouldn't overflow because it's value is constantly compared to PR2 register whose value in most cases will be smaller than maximum value of Timer2. And as soon as PR2 value is reached, TMR2 is cleared so no overflow.
 
Last edited:
In that situation a timer overflow is a guaranteed fault notice. Can be used for that even if you can't predict what could have caused it.
 
The timer2 interrupt flag is not set when timer2 overflows, it is set when timer2 matches pr2.

Mike.
 
I'm programming in MPASM.

Ok, still, if an overflow flag set on TMR2 = PR2, why skip the first pulse then? In this situation I will have to wait for the first overflow somewhere in main loop or implement an overflow interrupt handler just for this purpose.
 
Normally the PWM module is run at many kHz and so waiting for a new period takes less than a milli second and only has to be done once at initialization. What speed are you intending the PWM to run at? Why can't you wait 1mS when your code first starts?

Mike.
 
Is that a copy straight from the datasheet?

For the PWM function? Did not work with that one but sounds strange to me. Could you scan the relevant section?
 
Last edited:
Yes, F will be couple of kHz so not very long to wait, I agree. But the question still remains unanswered - should these steps be performed at all? All examples I have seen just make CCPx pin an output and then all the rest setup is performed regarding Timer and CCP registers. Unfortunately datasheet, which is very recent by the way, does not mention anything regarding this subject.
 
My guess would be that this is just a precautionary setup. It is to ensure the pin is in a known state during setup. If your external circuitry won't be harmed by a random pulse at startup then I wouldn't worry about it.

Mike.
 
If initial duty cycle is set to 0%, TMR2IF flag will be set immediately, right? In that case I can probably enable the CCPx pin output driver right after starting the Timer2 (this operation replaces step 6).
 
TMR2IF will not be set until timer2=PR2. So you could set PR=1, wait for TMR2IF and then set it to the correct value.

Mike.
 
Ahh... yes. PR2 defines period not duty cycle. Prescaler will have to be set to 1 as well, so I think it is not worth doing this. I'll just wait for the first overflow in PWM setup routine by polling the TMR2IF bit. It will probably be only 3 additional instructions.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…