Here is QEI module count set to count from 0 - 999 .
x is number of cyrcle's of QEI module up and down.
QEI variable is final counter..etc
Code:
loop: 
     z = 0
     PosHigh = POSCNTH                           'Get high byte of counter
     PosLow = POSCNTL                            'Get low byte of counter
     PosTemp = POSCNTL                           'Get low byte again  
     If PosLow - PosTemp = 0 then Goto Done      'Compare, if equal we're done.
     PosHigh = POSCNTH                           'If not, get values again.
     PosLow = POSCNTL
     z = 1 
Done: 
     Position = POSHIGH * 256 + PosLow           'Put high and lowbyte together.
     if position > 1000 then position = 1000
     if position < 1 then position = 0 
     h = QEICON.5                                'QEICON.5 for right = 1 for left = 0 
     if PIR3.2 = 1 and h =1 then x = x+ 1
     if x <> 0 and PIR3.2 = 1 and h =0 then x = x- 1
     PIR3.2 = 0                                  'PIR3.2 maxcount transition down or up was detected
     h = 0   
     QEI =  (1000 * x) + position                    ' increment  total counter
     if z = 1 and h = 0 then QEI = QEI -(1000-position) ' dectement  total counter 
     if h = 1 and QEI = 0 then
     QEI = 65535
     endif
     if QEI > 65535 then
     x = 0
     QEI = 0
     endif
     if porta.0 = 0 then setpoint = setpoint + 1
     if setpoint > 65535 then
     setpoint = 0 and QEI = 0 and x = 0
     endif
Butwhen I count to minus after QEI variable com to 0 if I move shaft of motor more to CCW real QEI conter will continue 999,998 and if my setpoint stay at 0 I am in big problem...
Any suggest please
Regards Robert