One tuning-fork cantilever integrated in a home-built atomic force microscope (AFM) has been investigated to be used as an encoder for real-time forward or backward displacement measurement when paired with 1D grating. The decoding principle is based on direct count of integer periods plus calculation of two fractional parts in encoded signal at the beginning and at the actual position corresponding to a segment of displacement. Cross-correlation technique has been employed to filter 1D grating encoded signal in real time. The encoder has been used to measure forwards or backwards displacement and to monitor the scanning stage, during motion, to control its displacement. It can also be used for the measurement of in-plane rotation angle between1D grating orientation and micro moving stage within 90° range. The criteria for the angle measurement are explained and the measurement data are shown.