Using the LS7166 Quadrature Counter
Revision as of 00:51, 22 April 2010
The LS7166 is a powerful quadrature decoder/counter chip. A motor's encoder can be connected directly to the LS7166, which will decode and count pulses as the encoder channels change. Using a parallel data bus, a microcontroller can simply read the current count value from the chip, allowing a project to interface with motor encoders without using any additional timers.
In this document, the LS7166 is interfaced with a PIC32, but the code could be ported to work with a PIC18.
Overview of the LS7166
The LS7166 is a 24 bit quadrature counter. The A and B inputs normally serve as quadrature inputs, like those which come from a quadrature encoder like those on most motors. The A and B inputs can also server as up / down counter inputs, so that the LS7166 can be used for other applications as well. The internal counter can count in binary, BCD, or in time units (hours/minutes/seconds). In any motor-related application, binary counting is necessary. The chip can also operate in divide-by-n mode, which loads the count register with a fixed value on rollover. This is useful for motor applications, since the fixed value can be set to the number of pulses in a full rotation, and then whenever the count value is read, the exact position of the motor can be known no matter the amount of time that has passed since the last read. The chip has several other features that can be read about in the datasheet. The code in this document only covers divide-by-n binary quadrature mode, and for more advanced features the support code will need to be modified.
The LS7166 communicates with a microcontroller via a bi-directional 8-bit data bus and several control bits. Four of the contorl bits, /RD (read), /WR (write), C/D (control or data), and /CS (chip select), are required for communication. Two other control lines, LCTR/LLTC and ABGT/RCTR are for more complex features and can usually be tied to Vdd or GND as needed. There are also two pins which output flags when programmed events occur, such as an overflow. For more info on these features, see the datasheet. Note that most pins are active low, and thus must be kept at high at all times unless in use. If multiple chips are used (for interfacing to multiple motors), all the communication lines can be reused except /CS (chip select).
The following is a list of registers and the general use of each, even though they are already handled by the code written in a later section.
- MCR- Master control register. This register controls functions like resetting the chip and register transfer operations. (Write only)
- CNTR- Counter. This register contains the current count value. CNTR cannot be read to or written from directly. Instead PR or OR must be used.
- PR- Preset register. This register contains a value that can be written to CNTR by software control or on underflow in the divide-by-n mode. As such, it generally will contain the maximum encoder value. (Write only)
- ICR- Input control register. This register sets up input writing modes. (Write only)
- OSR- Output status register. This register contains some status bits. (Read only)
- OCCR- Output control register. This register sets up output modes. (Write only)
- QR- Quadrature register. This register sets up the quadrature mode. (Write only)
- OL- Output latch. This register is used to read the value of CNTR. A write of 0x03 to MCR will transfer CNTR to OL, and then reading OL will obtain the CNTR value. (Read only)
The LS7166, as mentioned above, connects to the PIC via an 8-bit data bus, 4 control bits, 2 optional control bits, and 2 optional flags. In the code below and example schematic, pins B0-B7 are used for the data bus. These pins can be changed, but the data bus read and write functions will need to be changed to interface with bit lines instead of bytes. Since this was put in the separate function, other code won't have to be changed, other than TRIS setups.
Other than PIC connections, the LS7166 also needs power (3.3v or 5v), ground, and connections to the A and B inputs of the encoder. Also note that in the case of this example, the optional control bit /ABGT is tied to ground, and the optional control bit /LCTR is tied to the flag /CY.
As mentioned above, multiple LS7166 can be used which are connected to the same data and control lines, except for chip select, which needs to be separate for each chip. The code will also have to be modified to toggle specific /CS lines during reads.