Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] PIC16F887 Timer Confusion

Not open for further replies.


Full Member level 4
Nov 29, 2010
Reaction score
Trophy points
Activity points
I don't understand the purpose of timer. We often use timers as delay function for example I want a process to be done for five minutes or to be done after five minutes. Its mean the timers must be operate in seconds if we are not doing some machine instructions execution.
But talking of PIC timers they work in a very strange manners.First of all if you are using internal clock it will be divided by 4 then prescaler then staring number of timer then you will get a delay which is less then a second.
I mean why it is so complex why don't we simply select timer in seconds and minutes. I don't think in simple projects we need a timer operating in milli in micro seconds.
I would be thankful if you answer my question

often in microcontroller applications one needs to time events in microseconds or millseconds, e.g. to read an ADC 10000 samples/second requires a 0.1mSec timer

if you need longer events use a counter incremented on every timer interrupt, e.g.
volatile long unsigned int mSecCounter=0, 	// counts millisecond interrupts
    mSecDelayCounter=0;         // used in the millsecond delay function

/* interrupt service routine for Timer1 ISR*/
void __attribute__((__interrupt__, __shadow__)) _T1Interrupt(void)
int i;
/* Interrupt Service Routine code goes here */
IFS0bits.T1IF = 0; 							//Reset Timer1 interrupt flag	

// delay a sepcified number of mSec
void mSecDelay(const long unsigned int delay)
one calls the mSecDelay() function with the number of milliseconds to delay, e.g.
5*60*10000 to delay 5 minutes
if you want to do other things while timing an event, one can initialise a variable such as mSecCounter to 0, incremenet it on every timer interrupt and kept checking it for the elapsed time required.
Last edited:

For your simpler projects why not uses a slower clock speed - the internal oscillator can be selected down to 31K hz.
Ok thanks one last question. PIC16F887 has three timers. TMR0 TMR1 and TMR2.I have confusion regarding their initialization.
TMR0 is simply started with TMR0 module register and there is no TMR0 control register. While TMR2 has both module register and control register.
People started TMR0 with module register but they started TMR2 with TMR2ON which is in control register.
I mean what is the difference of starting Timers with module register and through control register.
Please explain.

The TMR0 control is inside the OPTION_REG. I guess it's because all MCUs have at least TMR0. And support for TMR1 and TMR2 are added on top of that.
TMR0 is always ON. It only depends if it increments on instruction cycle or a counter input. So when you want to start TMR0, you actually only reset its value.
What I understand whenever we type TMR0 it get started.
But for TMR1 and TMR2 first we have to enable them by typing TMR1ON = 1 or TMR2ON = 1
and then type TMR1 or TMR2 to get started

Hmmm. Than I don't know what to tell you. I used more hands-on approach doing every thing manually.
I guess it depends on the compiler (check the reference). I guess when you type TMR0, TMR1 or TMR2, your compiler just clears the corresponding timer register.
And since TMR0 is always ON, there's no ON/OFF switch like for the others.
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to