QDEC Phase Detection

This sample code guide is designed to help users easily and comprehensively understand QDEC sample. This sample uses QDEC to detect the direction and count of wheel turns.

Requirements

For hardware requirements, please refer to the Requirements.

Wiring

Connect P1_0 to the PHA of the wheel, connect P1_1 to the PHB of the wheel.

The hardware connection of QDEC sample code is shown in the figure below.

../../../_images/QDEC_Demo_Hardware_Connection_Diagram.png

QDEC Sample Code Hardware Connection Diagram

Configurations

  1. The following macros can be configured to modify pin definitions.

    • #define QDEC_Y_PHA_PIN          P1_0

    • #define QDEC_Y_PHB_PIN          P1_1

  2. The entry function is as follows, call this function in main() to run this sample code. For more details, please refer to the Initialization.

    qdec_demo();
    

Building and Downloading

For building and downloading, please refer to the Building and Downloading.

Experimental Verification

Preparation Phase

Press the Reset button on the EVB, when QDEC Y-axis detects new data, it triggers an interrupt and prints the interrupt message, the direction, and the number of scroll wheel rolls.

qdec_handler: dir xx, y_axis xx

Code Overview

This section introduces the code and process description for initialization and corresponding function implementation in the sample.

Source Code Directory

  • For project directory, please refer to Source Code Directory.

  • Source code directory: sdk\src\sample\io_demo\qdec\qdec_demo.c.

Initialization

The initialization flow for peripherals can refer to Initialization Flow.

QDEC initialization flow is shown in the following figure.

../../../_images/QDEC_Operation_Flow_Chart.png

QDEC Initialization Flow Chart

  1. Call Pad_Config() and Pinmux_Config() to initialize the pin.

    static void board_qdec_init(void)
    {
       /* Qdecoder pad config */
       Pad_Config(QDEC_Y_PHA_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_UP, PAD_OUT_DISABLE,
                   PAD_OUT_LOW);
       Pad_Config(QDEC_Y_PHB_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_UP, PAD_OUT_DISABLE,
                   PAD_OUT_LOW);
    
       /* Qdecoder pinmux config */
       Pinmux_Config(QDEC_Y_PHA_PIN, QDEC_PHASE_A_Y);
       Pinmux_Config(QDEC_Y_PHB_PIN, QDEC_PHASE_B_Y);
    }
    
  2. Call RCC_PeriphClockCmd() to enable the QDEC clock and function.

  3. Initialize the QDEC peripheral:

    1. Define the QDEC_InitTypeDef type qdecInitStruct, and call QDEC_StructInit() to pre-fill qdecInitStruct with default values.

    2. Modify the qdecInitStruct parameters as needed. The QDEC initialization parameter configuration is shown in the table below.

    3. Call QDEC_Init() to initialize the QDEC peripheral.

    QDEC Initialization Parameters

    QDEC Hardware Parameters

    Setting in the qdecInitStruct

    QDEC

    Scan Clock

    QDEC_InitTypeDef::ScanClockKHZ

    50

    Debounce Clock

    QDEC_InitTypeDef::DebonceClockKHZ

    5

    Y-axis Enable

    QDEC_InitTypeDef::axisConfigY

    ENABLE

    Y-axis Debounce Enable

    QDEC_InitTypeDef::debounceEnableY

    Debounce_Enable

    Y-axis Debounce Time

    QDEC_InitTypeDef::debounceTimeY

    5 * 5

  4. Call QDEC_INTConfig() to enable the Y-axis counter interrupt QDEC_Y_INT_NEW_DATA.

  5. Call NVIC_Init() to enable NVIC of QDEC.

  6. Call QDEC_Cmd() to enable the QDEC.

Functional Implementation

Interrupt Handle

QDEC interrupt handle flow is shown in the following figure.

../../../_images/QDEC_Interrupt_Handle_Flow_Chart.png

QDEC Interrupt Handle Flow Chart

when QDEC Y-axis counter value changed, it will trigger the Y-axis counter interrupt:

  1. Call QDEC_GetFlagState() to check QDEC_FLAG_NEW_CT_STATUS_Y interrupt status flag.

  2. Call QDEC_INTMask() to mask QDEC_Y_CT_INT_MASK interrupt.

  3. Call QDEC_GetAxisDirection() to read direction.

  4. Call QDEC_GetAxisCount() to read count value.

  5. Call QDEC_ClearFlags() to clear QDEC_CLR_NEW_CT_Y interrupt.

  6. Call QDEC_INTMask() to unmask QDEC_Y_CT_INT_MASK interrupt.