Quadrature Decoder (TCPWM)

group group_tcpwm_quaddec

Driver API for Quadrature Decoder.

The functions and other declarations used in this part of the driver are in cy_tcpwm_quaddec.h. You can also include cy_pdl.h to get access to all functions and declarations in the PDL.

A quadrature decoder is used to decode the output of a quadrature encoder. A quadrature encoder senses the position, velocity, and direction of an object (for example a rotating axle, or a spinning mouse ball). A quadrature decoder can also be used for precision measurement of speed, acceleration, and position of a motor’s rotor, or with a rotary switch to determine user input.

The Quadrature Decoder has the following features:

  • 16- or 32-bit Counter.

  • Period Register. This is only available in TCPWM Version 2.

  • Range 0, Range 0 Compare, Range 1 capture, Range 1 Compare Quadrature modes. This is only available in TCPWM Version 2.

  • Counter Resolution of x1, x2, x4, and up/down rotary the frequency of the phiA (Count) and phiB (Start) inputs. Up/Down rotary resolution is only available in TCPWM Version 2

  • Interrupt on Terminal Count and Compare or Capture 0. Interrupt on Compare or Capture 1 is only available in TCPWM Version 2.

  • Index Input to determine absolute position

  • A positive edge on phiA increments the counter when phiB is 0 and decrements the counter when phiB is 1

  • Two programmable trigger outputs, trig_out0 and trig_out1 that generate a trigger on one of the following events Overflow, Underflow, Terminal Count, and Compare Match 0/1. The available triggers are dependent on the QuadDec mode. All these features are only available in TCPWM Version 2.

  • TC (cc_match) trigger output is only available in TCPWM Version 1.

  • Second Compare Match (CC_MATCH1) event is only available in TCPWM Version 2.

Configuration Considerations

The QuadDec configuration can be divided to number of sequential steps listed below:

  • Configure Quadrature

  • Assign Clock Divider

  • Enable Quadrature Decoder

  • Start Quadrature Decoder

Configure Quadrature

To configure Quadrature, provide the configuration parameters in the cy_stc_tcpwm_quaddec_config_t structure. The Configuration structure can be modified through software, but if the configurator in ModusToolbox is used then the configuration structure will be updated with the users input. To initialize the driver, call Cy_TCPWM_QuadDec_Init function providing a pointer to the populated cy_stc_tcpwm_quaddec_config_t structure.

For TCPWM V1 Configuration

    /* Scenario: there is need to initialize
     * the first (index = 0) quadrature decoder of the TCPWM0 block
     * with the below configuration settings
     */
    cy_stc_tcpwm_quaddec_config_t tcpwm_v1_qd_config =
    {
        /* .resolution        = */ CY_TCPWM_QUADDEC_X1, /* One count during one encoder cycle */
        /* .interruptSources  = */ CY_TCPWM_INT_ON_TC,  /* Simple example without index input */
        /* .indexInputMode    = */ CY_TCPWM_INPUT_RISINGEDGE, /* The default value (0UL) */
        /* .indexInput        = */ CY_TCPWM_INPUT_0, /* The index input is not used */
        /* .stopInputMode     = */ CY_TCPWM_INPUT_RISINGEDGE, /* The default value (0UL) */
        /* .stopInput         = */ CY_TCPWM_INPUT_0, /* The stop input is not used, the block will be stopped by SW */
        /* .phiAInput         = */ CY_TCPWM_INPUT_TRIG_0, /* The phiA input is connected to the trigger multiplexer #0 of the Trigger Group #3 */
        /* .phiBInput         = */ CY_TCPWM_INPUT_TRIG_1,  /* The phiB input is connected to the trigger multiplexer #1 of the Trigger Group #3 */
    };

    #define MY_TCPWM_CNT_NUM   (0UL)

    if (CY_TCPWM_SUCCESS != Cy_TCPWM_QuadDec_Init(TCPWM0, MY_TCPWM_CNT_NUM, &tcpwm_v1_qd_config))
    {
        /* Handle possible errors */
    }
    /* Enabled the initialized quadrature decoder */
    Cy_TCPWM_QuadDec_Enable(TCPWM0, MY_TCPWM_CNT_NUM);

    /* Then start the decoder */
    Cy_TCPWM_TriggerStart_Single(TCPWM0, MY_TCPWM_CNT_NUM);
For TCPWM V2 Configuration
    /* Scenario: there is need to initialize
     * the first (index = 0) quadrature decoder of the TCPWM0 block
     * with the below configuration settings
     */
    cy_stc_tcpwm_quaddec_config_t tcpwm_v2_qd_config =
    {
        /* .resolution        = */ CY_TCPWM_QUADDEC_X1, /* One count during one encoder cycle */
        /* .interruptSources  = */ CY_TCPWM_INT_ON_TC,  /* Simple example without index input */
        /* .indexInputMode    = */ CY_TCPWM_INPUT_RISINGEDGE, /* The default value (0UL) */
        /* .indexInput        = */ CY_TCPWM_INPUT_0, /* The index input is not used */
        /* .stopInputMode     = */ CY_TCPWM_INPUT_RISINGEDGE, /* The default value (0UL) */
        /* .stopInput         = */ CY_TCPWM_INPUT_0, /* The stop input is not used, the block will be stopped by SW */
        /* .phiAInput         = */ CY_TCPWM_INPUT_TRIG_0, /* The phiA input is connected to the trigger multiplexer #0 of the Trigger Group #3 */
        /* .phiBInput         = */ CY_TCPWM_INPUT_TRIG_1,  /* The phiB input is connected to the trigger multiplexer #1 of the Trigger Group #3 */
        /* .phiAInputMode     = */ CY_TCPWM_INPUT_LEVEL, /* Pass thorugh (no edge detection) */
        /* .phiBInputMode     = */ CY_TCPWM_INPUT_LEVEL, /* Pass thorugh (no edge detection) */
        /* .quadmode          = */ CY_TCPWM_QUADDEC_MODE_RANGE0, /* Quad mode */
        /* .period0           = */ 0UL, /* Period 0 Value */
        /* .captureOnIndex    = */ CY_TCPWM_QUADDEC_CAPTURE_ON_INDEX, /* Capture On Index event */
        /* .compare0          = */ 0Ul, /* Sets the value for Compare 0 */
        /* .compareBuf0       = */ 0UL, /* Sets the value for the buffered Compare 0 */
        /* .enableCompare0Swap = */ false, /*  If enabled, the compare 0 values are swapped on the terminal count */
        /* .compare1          = */ 0UL, /* Sets the value for Compare 1 */
        /* .compareBuf1       = */ 0UL, /* Sets the value for the buffered Compare 0 */
        /* .enableCompare1Swap = */ false, /*  If enabled, the compare 0 values are swapped on the terminal count */
        /* .capture0OrIndex1InputMode = */ CY_TCPWM_INPUT_RISINGEDGE, /* Configures how the capture0/index1 input behaves.*/
        /* .capture0OrIndex1Input = */ CY_TCPWM_INPUT_0, /* Selects which input the capture0/index1 uses. The inputs are device-specific. */
        /* .capture1InputMode = */ CY_TCPWM_INPUT_RISINGEDGE, /* Configures how the capture1 input behaves. */
        /* .capture1Input     = */ CY_TCPWM_INPUT_0, /* Selects which input the capture1 uses. The inputs are device-specific. */
        /* .trigger0Event     = */ CY_TCPWM_CNT_TRIGGER_ON_DISABLED, /* Disable output trigger0 event generation*/
        /* .trigger1Event     = */ CY_TCPWM_CNT_TRIGGER_ON_DISABLED, /* Disable output trigger1 event generation*/
    };

    #define MY_TCPWM_CNT_NUM   (0UL)
    
    if (CY_TCPWM_SUCCESS != Cy_TCPWM_QuadDec_Init(TCPWM0, MY_TCPWM_CNT_NUM, &tcpwm_v2_qd_config))
    {
        /* Handle possible errors */
    }
    /* Enabled the initialized quadrature decoder */
    Cy_TCPWM_QuadDec_Enable(TCPWM0, MY_TCPWM_CNT_NUM);
    
    /* Then start the decoder */
    Cy_TCPWM_TriggerStart_Single(TCPWM0, MY_TCPWM_CNT_NUM);

Assign Clock Divider

The clock source must be connected to proper working. Any of the peripheral clock dividers could be used. Use the SysClk (System Clock) driver API to do that.


    /* Scenario: Assign the first 8-bit peripheral clock divider with
     * divide ratio = 100 to the first counter of the TCPWM0 block:
     */

    Cy_SysClk_PeriphAssignDivider(PCLK_TCPWM0_CLOCKS0, CY_SYSCLK_DIV_8_BIT, 0UL);
    Cy_SysClk_PeriphSetDivider(CY_SYSCLK_DIV_8_BIT, 0UL, 99UL);
    Cy_SysClk_PeriphEnableDivider(CY_SYSCLK_DIV_8_BIT, 0UL);

Enable Quadrature Decoder

Quadrature has to be enabled before starting

    /* Scenario: Enable Quadrature Decoder */
    #define MY_TCPWM_QUADDEC_NUM   (0UL)

    Cy_TCPWM_QuadDec_Enable(TCPWM0, MY_TCPWM_QUADDEC_NUM);

Start Quadrature Decoder

Quadrature has to be started

    /* Scenario: Start Quadrature Decoder operation */
    #define MY_TCPWM_QUADDEC_NUM   (0UL)

    Cy_TCPWM_TriggerReloadOrIndex_Single(TCPWM0, MY_TCPWM_QUADDEC_NUM);