Atomic force microscope cantilevers have been investigated for their use as the encoder for real-time high-resolution displacement measurements, when paired with a 1D sinusoidal grating of well-known pitch. For a known one-directional (forward or backward) displacement measurement, the decoding algorithm is based on directly counting the integer periods of the grating and calculating the fractional parts at the beginning of the displacement and at the actual position by using one cantilever. Using two cantilevers arranged in the quadrature phase shift positions on the grating makes the measurement of two-directional (forward and backward) displacements possible. The decoding algorithm directly unwraps the phase between two encoded signals. Cross-correlation filtering and the differentiation process of two encoded signals are found to be very successful to guarantee the implementation of real-time displacement measurements by suppressing noise and reducing the offset and tilt of the encoded signals.