The answer to both your questions is the use of interrupts. The PBP manual doesn't mention much about interrupts. Darrel has made this a lot easier with his routines. Here is an example of a timer:

http://darreltaylor.com/DT_INTS-18/elapsed.html
http://darreltaylor.com/DT_INTS-18/home.html

Your counter could be based on an interupt on change of pin. Maybe INT0 INT1 or INT2?