You are on page 1of 1

32 bit Position Encoder For PID Controllers Using The C167's GPT1

In most servo drives, the angular position of a shaft is derived from a simple chopper arrangement that supplies a certain number of edges per revolution, plus a signal which indicates the direction of rotation. The change in position per unit time is of course the angular velocity and by examining the number of edges that have passed every 1ms, for example, the speed can be calculated. To get a sufficient dynamic range to allow the position and speed to be measured from the stall condition up to maximum speed often requires the use of 32-bit counts. The C16x's GPT1 can easily be configured to perform a 16bit measurement but the implementation of a 32-bit version requires a little more ingenuity! In this example, a servo motor shaft was supplying edges at a rate of 0Hz to 50kHz. A PID controller interrupt function was running every 1ms which tracked the change in position (and thence speed) by reading the number of counts recorded by GPT1 every time. The user was working with the C165 but this solution will work on any C166 derivative. The solution here relies on concatenating two GPT1 timers. Thus T2 is driven by the overflowing of T3, to form a single 32 bit timer. The encoder signal is fed to T3IN pin to clock T3 on every rising edge. T2 is clocked by output from T3 via the intriguingly named timer 3 output toggle latch (T3OTL). The direction signal from the encoder is fed to T3EUD and T2EUD pins (Tx External UP/DOWN control). T3 and T2 represent the 32 bit position of the encoder. To allow this value to be used by a PID controller sampling the 32 bit count at a rate of 1ms, some form of 32-bit input capture of the value is required. As this not possible on the C165, a special method has been developed. The T2/T3 value is captured in an interrupt service routine, also driven by the encoder signal. This means that T2 and T3 can be guaranteed to be stable as the encoder edge has already just occurred and will not change again for a significant time. In practice, the encoder input also fed to the T4IN pin so that the capture interrupt occurs on each positive edge of signal. The value of T2 and T3 (the encoder position) are placed into the high and low words of a two word array that is an union with a 32 bit long. At any one time, this long contains the encoder position. This example illustrates again how ganging together C16x peripherals and CPU pins can allow the realisation of useful functions with zero hardware cost and just a little software.

A working example is available on request that will run with the free Keil PK166LITE evaluation package, written with Siemens DaVe tool can be requested from Hitex UK.