# Timer one interrupts

I'm using timer 1 in free running mode to count a given value.

An interrupt is generated when a rising edge is detected on an external pin. Now I wish to generate another interrupt, if no pulse is received in a certain amount of time.

Example: Timer 1, has a valid counting range from 0 to 65535 and my sensor valid range goes from 0 to 32000. Typically a signal occurs before the timer counts to 32000, but lets assume a fault situation, with no trigger present. I wish to have an additional interrupt if no rising edge is detected and the counting goes above 40000.

Is this possible?

Now I wish to generate another interrupt, if no pulse is received in a certain amount of time.

How? An interrupt is something that an external event triggers or a timer triggers.

I don't understand why you are using a timer for this. Some event causes you to start looking for a rising edge interrupt. Record when that happens. Periodically, check how long it has been since that happened. If the event time is greater than 0 and enough time has passed since that event, do something. Reset the time (to 0) when the other signal arrives.

PaulS:

Now I wish to generate another interrupt, if no pulse is received in a certain amount of time.

How? An interrupt is something that an external event triggers or a timer triggers.

I don’t understand why you are using a timer for this. Some event causes you to start looking for a rising edge interrupt. Record when that happens.

Correct, but if the event doesn’t happen within a given time frame, I want to do some other action, like trigger a fault.

Correct, but if the event doesn’t happen within a given time frame

What starts that time frame? THAT is when you record the start time.

PaulS:

Correct, but if the event doesn't happen within a given time frame

What starts that time frame? THAT is when you record the start time.

Under normal conditions the counter starts from zero at startup and waits for a trigger within a given time frame. This trigger an interrupt, saving the counter value and reseting the TCNT1 register back to zero.

Under a fault condition, if the counter is not reset within a given time frame I want to stop the code and output an error condition. An interrupt would be ideal, as it would avoid a overflow in case of the CPU being busy.

It seems like this is a kind of watchdog timer function.

You set up a timer, with an interrupt routine scheduled to run when the time expires. This interrupt is usually an error condition or reset code of some type.

You have some other loop or some other event that happens periodically, telling you that the code is functioning properly. Every time it happens, you reset the watchdog timer. Start over.

If your processor gets hung or goes to sleep or goes into some undefined error condition and doesn't reset the watchdog timer, it will expire and fire the interrupt. You can try to figure out why the normal event didn't happen, handle whatever error you get, or just reset the entire processor.

You can actually use the watchdog timer that is standard in the microcontroller, or you can use one of the other timers and roll your own. This is more than possible. It's not uncommon practice.