Quadrature signals are the standard output waveforms of incremental encoders that come with motors. They provide information about motor direction and shaft position. There are two types of encoders - linear and rotary - and each can have two types of measurement: Absolute and incremental. There are also different technologies used for encoders: Magnetic, optical, inductive, capacitive, and laser.
The main categories of encoders are absolute and incremental. A rotary absolute encoder outputs a unique code for each position of the motor's shaft. If the power of this encoder is lost and the shaft is rotated, the encoder can return the correct position of the shaft. The rotary incremental encoder generates pulses while the shaft is rotated. If any pulse is missed, then the shaft position cannot be determined correctly anymore. This application will use a rotary incremental encoder to show how to decode quadrature signals.
The following image highlights the Clockwise (CW) and Counterclockwise (CCW) conditions on the quadrature signals A and B. When signal A leads signal B (on rising or falling edge), it will be a clockwise detection, otherwise, when signal B leads signal A (on rising or falling edge), it will be a counterclockwise detection.
The quadrature decoder uses the Configurable Custom Logic (CCL) on the AVR64DD32 to detect CW and CCW pulses. The following picture shows the output signals of the CCL implementation, CW and CCW, based on the quadrature signals A and B.
Using the Event System (EVSYS) peripheral, the CW and CCW signals are linked to two TCB instances: One counts the CW pulses, the other one counts the CCW pulses.
More details and code examples on the AVR64DD32 can be found at the following links:
- Interfacing Quadrature Encoder using CCL with TCA and TCB
- Getting Started with CCL
- AVR64DD32 Product Page
- AVR64DD32 Code Examples on GitHub
- MPLAB® X IDE 6.00 or newer
- MPLAB® XC8 2.36 or a newer compiler
- AVR-Dx Series Device Pack v2.1.152 or newer
- MPLAB® Code Configurator Melody core 2.1.13 or newer
- MPLAB® Code Configurator Melody Drivers
- MPLAB® Data Visualizer v1.2.1160 or newer
- Logic analyzer software
- AVR64DD32 Curiosity Nano Development Board is used as a test platform.
Note: The setup can be made using rotary encoder or motor with encoder. In this example we use the rotary encoder.
To program the Curiosity Nano board with this MPLAB® X project, follow the steps provided in the How to Program the Curiosity Nano Board chapter.
The following peripheral and clock configurations are set up using MCC Melody for the AVR64DD32:
- System Clock - 4 MHz (default)
- TCA0:
- System Clock/64
- Timer Mode 16 bit
- Count Direction - UP
- Overflow Interrupt Enable - every 1 second
- Waveform Generation Mode - Normal Mode
- TCB0:
- Clock Select - Event
- Timer Mode: INT
- Enable Asynchronous
- Overflow Interrupt Enable
- TCB1:
- Clock Select - Event
- Timer Mode: INT
- Enable Asynchronous
- Overflow Interrupt Enable
- UART0:
- Baudrate: 115200
- Parity: None
- Enable USART Transmit and Receive
- Printf Support Enable
- EVSYS:
- Configuration described in the below schematic
- CCL:
- Configuration described in the below schematic
The following pin configurations must be made for this project:
Pin | Configuration | Function |
---|---|---|
PA4 | Input | Phase A |
PA5 | Input | Phase B |
PC0 | Output | USART TX |
PC1 | Input | USART RX |
PA6 | Output | CW detection |
PD3 | Output | CCW detection |
The internal connections and signals are presented in the following pictures:
In this demo, the function QEI_GetVelocity
is called every second and the returned value is printed on the serial terminal. The hardware setup is presented in the image below:
Every second, the device prints a message with the counter value through the USART interface:
Note: Follow the steps in the How to use MPLAB® Data Visualizer section to set up the Data Visualizer so that it can correctly show the text messages through USART0.
A snapshot of the different signals from the logic analyzer can be seen below. The CW and CCW pulses correspond to the counter values 8 and -8.
The quadrature decoder can detect and count clockwise and counterclockwise pulses on rising and falling edges of the quadrature signals. Read more about the implementation at https://www.microchip.com/en-us/about/blog/design-corner/quadrature-decoder.
This section illustrates how to use the MPLAB® X Data Visualizer to send commands and receive information, but prior to programming the AVR64DD32 Curiosity Nano Board. This can be applied to any other projects.
-
Open the software terminal in MPLAB® X IDE. Left click on the Data Visualizer button.
-
Select the Input Source corresponding to the AVR64DD32 in the right side.
-
Identify the coresponding COMx that is represented by the AVR64DD32 Curiosity Nano Board and select it in the Connections tab in the left side.
-
Set the specific Baud Rate, in this case 115200.
-
Press Start streaming COMx button. The text messages will appear in the Terminal tab.
This chapter shows how to use the MPLAB® X IDE to program an AVR® device with an Example_Project.X. This can be applied for any other projects.
-
Connect the board to the PC.
-
Open the Example_Project.X project in MPLAB X IDE.
-
Set the Example_Project.X project as main project.
-
Clean and build the Example_Project.X project.
-
Select the AVRxxxxx Curiosity Nano in the Connected Hardware Tool section of the project settings:
-
Program the project to the board.