CPU Flow and Position Decoding

In the code accompanying this application note, the CPU is utilized only for mapping the TCA data to an actual position value, in addition to updating the direction setting of the TCA upon direction changes. The position mapping is done in a basic polled fashion from the main loop, while the direction updates are executed by a pin change interrupt. The reason for not including a more elegant way of mapping the position value is to keep the example focused on the actual quadrature decoding setup. Additionally, the rate and fashion of this mapping is considered to be highly specific for the final application, and thus this part of the code most likely needs to be modified.

The figure below illustrates how the CPU is utilized.

Figure 1. CPU Execution Flow Outline

This decoding scheme is based on only incrementing or decrementing the counter value on quadrature pulses not corresponding to a direction change. For this reason the counter value alone indicates only that the physical position is in the interval corresponding to [counter -1, counter +1]. This gives an uncertainty, or precision, of two times the distance between two adjacent encoder pulses. By inspecting also the direction value, the uncertainty is reduced by half as it will identify in which of the two intervals, [counter - 1, counter] or [counter, counter + 1], the actual position resides. The figure below outlines the decoding principle with a mapping between the quadrature waveforms and the relative motion between the reader and the encoder disc of a rotary encoder.

Figure 2. Physical Position Decoding Example, Rotary Encoder Segment

In the code example, updating of the counter value and the direction bit are kept separated in the sense that only the direction bit is updated for each physical direction change. This minimizes the CPU cycles needed when the direction change interrupt is triggered. The value of the direction bit is, therefore, taken into account by subtracting it from the counter value before the counter value is mapped to its corresponding physical position.