Microchip® Advanced Software Framework

Quick Start Guide for the XMEGA QDEC Driver

This is the quick start guide for the XMEGA Quadrature Decoder (QDEC) , with step-by-step instructions on how to configure and use the driver in a selection of use cases.

The use cases are described with "setup" and "usage" sections, which each have "example code" and "workflow" subsections. This documentation first presents code fragments and function definitions along with instructions on where they can be placed, e.g., into the application C-file or the main() function, then follows up with explanations for all the lines of code.

Basic use case

In this basic use case, QDEC is configured for:

  • a common quadrature encoder with two lines (phase 0 / phase 90)
  • no index line on this

Setup steps

Example code

Add to application C-file:

static void qdec_init(void)
{
qdec_config_phase_pins(&config, &PORTA, 6, false, 500);
qdec_config_revolution(&config, 20);
qdec_enabled(&config);
}

Add to main():

qdec_init();

Workflow

  1. Initialize the clock system:
    • Note
      The QDEC driver requires the system clock driver to be initialized in order to compute the correct QDEC filters.
  2. Create a function qdec_init() to intialize the QDEC:
    • static void qdec_init(void)
      {
      // ...
      }
  3. Allocate configuration structs for the QDEC:
  4. Initialize the configuration structure with the identified as commonly hardware ressources and parameters:
  5. Define the PORT and pins used by QDec. These must be contiguous on PORT and only the first pin phase is request. A filter timing must be given to filter the rebounces:
  6. Define the QDec revolution. It is the number of position for one revolution:
  7. Load and enable the QDec configuration:

Usage steps

Example code

Add to, e.g., main-loop in application C-file:

uint16_t qdec_position = qdec_get_position(&config);
bool qdec_dir = qdec_get_direction(&config);

Workflow

  1. Read the current QDec position:
  2. Read the current QDec direction: