First of all, to get direction, you may have a quadrature encoder [2 light sources, 4 pin interface]
There are lots of sample codes and discussions about this topic over this forum. you could use them as a base for your creativity.
One option is to use PULSIN. I haven't used that method.
Other more powerfull option (I use) is to play with Interrupts to access PIC's hardware Timers. Timers (timer0) works in two modes:
a) as Timer (T0CS=0). TMR0 variable counts instruction cycles (as time reference) between capture period. Real time depends on OSC & bits settings and crystal speed.
b) as Counter (T0CS=1). TMR0 variable counts pulse's rising edge (T0SE=0) or falling edge (T0SE=1)
Read datasheet for the other timer's info.
One of the most beautifull of this is that this can work in background while you run other code.
At least that's what I understood from datasheet.
Bookmarks