2.9.1.8 - Quadrature Input (Mode 8) [U3 Datasheet]
Requires both timers, where Timer0 will be quadrature channel A, and Timer1 will be quadrature channel B. The U3 does 4x quadrature counting, and returns the current count as a signed 32-bit integer (2’s complement). The same current count is returned on both timer value parameters.
Writing a value of zero to either or both timers performs a reset of both. After reset, a read of either timer value will return zero until a new quadrature count is detected. If a timer is reset and read in the same function call, the read returns the value just before the reset.
4X Counting
Quadrature mode uses the very common 4X counting method, which provides the highest resolution possible. That means you get a count for every edge (rising & falling) on both phases (A & B). Thus if you have an encoder that provides 32 PPR, and you rotate that encoder forward 1 turn, the timer Value register will be incremented by +128 counts.
Z-phase Support
Quadrature mode supports Z-Phase. When enabled this feature will set the count to zero when the specified IO line sees a logic high.
Z-phase is controlled by the value written to the timer during initialization. To enable z-phase support set bit 15 to 1 and set bits 0 through 4 to the DIO number that Z is connected to. EG: for a Z-line on EIO3 set the timer value to 0x800B or 32779. This value should be sent to both the A and B timers.
Note that the LabJack will only check Z when it sees an edge on A or B.
Z-phase support requires Firmware 1.30 or later.
2's Complement
Other timer modes return unsigned values, but this timer mode is unique in that it returns a signed value from -2147483648 to +2147483647. That is, a 32-bit 2's complement value. When you do a timer value read and get back a single float from the UD driver, the math is already done and you get back a value from -2147483648.0 to +2147483647.0, but when using the special channels 20x/23x/224 you get the LSW and MSW separately and have to do the math yourself. Search for 2's complement math for your particular programming language.
In a language such as C++, you start by doing using unsigned 32-bit variables & constants to compute Value = (MSW * 65536) + LSW
. Then simply cast Value
to a signed 32-bit integer.
In a language such as Java that does not support unsigned integers, do everything with signed 64-bit variables & constants. First calculate Value = (MSW * 65536) + LSW
. If Value < 2147483648
, you are done. If Value >= 2147483648
, do ActualValue = -1 * (4294967296 - Value)
.
Edge Rate Limits
This edge-detecting timer mode requires processing resources as an interrupt is required to handle each edge. See more about edge rate limits in Section 2.9.2.
Can I Use a Simple Counter Instead?
If you need to track absolute position with changing direction (forward & reverse), you need to use the timer feature described above to decode quadrature signals, as the relationship between edges on the A & B phases indicates direction. However, if movement is only in one direction, or you always know the direction of movement, you can just connect one phase (A or B) to a hardware counter (2 available on the U3) or firmware counter (2 available on the U3). Hardware counters can handle much faster edge rates (up to 8 MHz per Appendix A). Firmware counters have the same edge rate limits as quadrature, but the firmware counter is counting 1/4th the edges of a quadrature timer and thus can handle a signal with 4x the frequency, and also is only using 1 timer rather than 2.