Thank You Richard for helping me. The your corrected code works, the routine counts up and down when I turn the encoder shaft cw or ccw, but if i mark the shaft starting position and turn on the MCU the reading is zero thats OK and when i slowly turn for example half turn and go back to marked position the encoder reading is not zero but less or more than zero. I think the routine not count correctly the encoder pulses, maybe the encoder send incorrect signals, I dont know.