# [SOLVED]PIC16F887 Timer Confusion

Status
Not open for further replies.

#### tahir4awan

##### Full Member level 4
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

#### horace1

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.
Code:
volatile long unsigned int mSecCounter=0, 	// counts millisecond interrupts
mSecDelayCounter=0;         // used in the millsecond delay function

/* interrupt service routine for Timer1 ISR*/
{
int i;
/* Interrupt Service Routine code goes here */
IFS0bits.T1IF = 0; 							//Reset Timer1 interrupt flag
mSecDelayCounter--;
mSecCounter++;
}

// delay a sepcified number of mSec
void mSecDelay(const long unsigned int delay)
{
mSecDelayCounter=delay;
while(mSecDelayCounter);
}
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:
tahir4awan

### tahir4awan

points: 2

#### wp100

Hi,

For your simpler projects why not uses a slower clock speed - the internal oscillator can be selected down to 31K hz.

tahir4awan

### tahir4awan

points: 2

#### tahir4awan

##### Full Member level 4
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.

#### bjuric

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.

tahir4awan

### tahir4awan

points: 2

#### tahir4awan

##### Full Member level 4
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

#### bjuric

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.

tahir4awan

### tahir4awan

points: 2

#### tahir4awan

##### Full Member level 4
Thanks bjuric

Status
Not open for further replies.