Can't you use the gated timers or interrupts to do what you want?