Skip to content

Guide for using Core Independent Quadrature Decoder with AVR64DD32 Microcontroller using MCC Melody

License

Notifications You must be signed in to change notification settings

microchip-pic-avr-examples/avr64dd32-quadrature-decoder-mplab-mcc

Repository files navigation

MCHP

Core Independent Quadrature Decoder using the AVR64DD32 Microcontroller with MCC Melody

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.

Related Documentation

More details and code examples on the AVR64DD32 can be found at the following links:

Software Used

Hardware Used

  • AVR64DD32 Curiosity Nano Development Board is used as a test platform.




  • Oscilloscope/Logic Analyzer

  • Rotary encoder

Note: The setup can be made using rotary encoder or motor with encoder. In this example we use the rotary encoder.

  • Motor with encoder


Operation

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.

1. Setup

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:



2. Demo

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.

3. Summary

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.

How to use MPLAB® Data Visualizer

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.

Operation

  1. Open the software terminal in MPLAB® X IDE. Left click on the Data Visualizer button.

  2. Select the Input Source corresponding to the AVR64DD32 in the right side.

  3. Identify the coresponding COMx that is represented by the AVR64DD32 Curiosity Nano Board and select it in the Connections tab in the left side.

  4. Set the specific Baud Rate, in this case 115200.

  5. Press Start streaming COMx button. The text messages will appear in the Terminal tab.

How to Program the Curiosity Nano board

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.

    • Right click on the project in the Projects tab and click Set as Main Project.
  • Clean and build the Example_Project.X project.

    • Right click on the Example_Project.X project and select Clean and Build.
  • Select the AVRxxxxx Curiosity Nano in the Connected Hardware Tool section of the project settings:

    • Right click on the project and click Properties
    • Click on the arrow under the Connected Hardware Tool
    • Select the AVRxxxxx Curiosity Nano (click on the SN), click Apply and then click OK:
  • Program the project to the board.

    • Right click on the project and click Make and Program Device.