Continue to Site

# How to count how many times a timer overflowed?

Status
Not open for further replies.

#### hobby_85

##### Junior Member level 2
Hey, Im new to microcontrollers so i apologize if the question is trivial.

Im using a 16f690 PIC and its timer to measure the time interval between two pins going high. The pins go high microseconds after each other.

So im using a 4mhz clock, with a prescalar of 16, meaning each count is 16us. So the timer overflows every 16us*255= 4.08 ms. (I think) Im still new to microcontrollers. Am i right so far?

Now, what im doing is when the first signal comes in, i set the timer register to 0, and when the second signal comes in, i use an interupt to read the timer register. So i get counts like 2,50,135 and so on. However, i dont know if the timer has overflowed. a timer register of 50 could mean 256 +50 instead. Is there a way of know how many exact counts have passed?

thanks

Re: timer overflow

Hi,

You use the Tm0 Interrupt flag IntCon T0IF, every time it overflows you increment a separate counter.

You could possibly use the Capture Compare Module - CCP instead.

### hobby_85

Points: 2
Re: timer overflow

You can increment a variable whenever the timer overflows.
--
Amr

### hobby_85

Points: 2
Re: timer overflow

Hey, i am using the CCP function to detect the second HIGH. because im using a 16f690, i only have one CCP.

I am getting some kind of value, but theres alot of fluctuation, and im not sure if im using the timer right. below is a list of values im getting:

64423
9505
64093
9558
64119
9582
64150
9826

These are meant to be the number of timer counts between the pins going high. but for some reason, the timer cant make up its mind.

if you guys have the time, I have attached my short code, so any help would be greatly appreciated.

Code:
#use delay(clock = 4000000)
#use rs232(baud=19200, xmit=PIN_B7, rcv=PIN_B5)

int16 ctr;
int tripped=0;
int16 value=0;

#int_CCP1
void  CCP1_isr(void)
{
value = get_timer1();
disable_interrupts(INT_CCP1);
tripped=1;
}

void main()
{

setup_timer_1(T1_INTERNAL|T1_DIV_BY_2);
enable_interrupts(GLOBAL);
setup_ccp1(CCP_CAPTURE_RE);

for(;;){

contact_slave3();                 //After this function, PINC1 should go high, followed closely by PINC5

for (ctr=0;ctr<32000;ctr++) {     //Wait for PINC1 to go high until timeout
if(input(PIN_C1)) {
set_timer1(0);              //set timer value to 0, start counting
ctr=1;                      //PINC1 went high, set ctr=0, break
break;
}
}

if (ctr==1){                                    //come here once C1 has gone high
enable_interrupts(INT_CCP1);                  //wait for rising edge of pinC5 until timeout

if(tripped) {
printf("Value is: %Lu",value);
tripped=0;
}
else{
printf("\r\nC5 Didnt go HIGH\r\n");
}

}else{                                           //PINC1 did not go high, error
printf("\r\nC1 Didnt go high\r\n");
}

delay_ms(2000);
tripped = 0;

}

}

timer overflow

I do not get what is meant by fluctation. Timer values should not be the same.
--
Amr

### hobby_85

Points: 2
Re: timer overflow

By fluctuation, what i mean is that the values are not similar. I know they should not be the same, but they should be around the same number.

From the values im getting, it is either around 60000 or 9500. Ill be more specific.

When i call the 'contact_slave_3' function, what is happening is PINC1 and PINC5 are meant to go high really quickly one after another. If i put this in a loop, and keep calling the 'contact_slave_3' function, then the values i should be getting should somewhat the same. i dont understand why there is so much of a difference. sometimes 60000 and sometimes 9500.

timer overflow

I cant help you much as code is hidden and do not know what s ur configuration settings.

Status
Not open for further replies.