PSOC E8XXGP Device Support Library

| PSOC E8XXGP Device Support Library

API Reference | Macros
Quadrature Decoder (TCPWM)
PDL API Reference » TCPWM (Timer Counter PWM)

## General Description

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_TriggerReloadOrIndex_Single(TCPWM0, MY_TCPWM_CNT_NUM);
cy_stc_tcpwm_quaddec_config_t
Quadrature Decoder configuration structure.
Definition: cy_tcpwm_quaddec.h:124
CY_TCPWM_SUCCESS
@ CY_TCPWM_SUCCESS
Successful.
Definition: cy_tcpwm.h:402
Cy_TCPWM_TriggerReloadOrIndex_Single
__STATIC_INLINE void Cy_TCPWM_TriggerReloadOrIndex_Single(TCPWM_Type *base, uint32_t cntNum)
Triggers a software reload event (or index 0 in QuadDec mode) on selected TCPWM.
Definition: cy_tcpwm.h:916
Cy_TCPWM_QuadDec_Enable
__STATIC_INLINE void Cy_TCPWM_QuadDec_Enable(TCPWM_Type *base, uint32_t cntNum)
Enables the counter in the TCPWM block for the QuadDec operation.
Definition: cy_tcpwm_quaddec.h:309
Cy_TCPWM_QuadDec_Init
cy_en_tcpwm_status_t Cy_TCPWM_QuadDec_Init(TCPWM_Type *base, uint32_t cntNum, cy_stc_tcpwm_quaddec_config_t const *config)
Initializes the counter in the TCPWM block for the QuadDec operation.
Definition: cy_tcpwm_quaddec.c:58
CY_TCPWM_INPUT_RISINGEDGE
#define CY_TCPWM_INPUT_RISINGEDGE
A rising edge triggers the event (Capture, Start, Reload, etc..)
Definition: cy_tcpwm.h:266
CY_TCPWM_INPUT_0
#define CY_TCPWM_INPUT_0
Input is tied to logic 0.
Definition: cy_tcpwm.h:228
CY_TCPWM_INPUT_TRIG_1
#define CY_TCPWM_INPUT_TRIG_1
Input is connected to the trigger input 1.
Definition: cy_tcpwm.h:231
CY_TCPWM_INPUT_TRIG_0
#define CY_TCPWM_INPUT_TRIG_0
Input is connected to the trigger input 0.
Definition: cy_tcpwm.h:230
CY_TCPWM_INT_ON_TC
#define CY_TCPWM_INT_ON_TC
Interrupt on Terminal count(TC)
Definition: cy_tcpwm.h:306
CY_TCPWM_QUADDEC_X1
#define CY_TCPWM_QUADDEC_X1
X1 mode.
Definition: cy_tcpwm_quaddec.h:205

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 through (no edge detection) */
/* .phiBInputMode = */ CY_TCPWM_INPUT_LEVEL, /* Pass through (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_TriggerReloadOrIndex_Single(TCPWM0, MY_TCPWM_CNT_NUM);
CY_TCPWM_INPUT_LEVEL
#define CY_TCPWM_INPUT_LEVEL
The event is triggered on each edge of the TCPWM clock if the input is high.
Definition: cy_tcpwm.h:272
CY_TCPWM_CNT_TRIGGER_ON_DISABLED
#define CY_TCPWM_CNT_TRIGGER_ON_DISABLED
Output trigger disabled.
Definition: cy_tcpwm.h:297
CY_TCPWM_QUADDEC_CAPTURE_ON_INDEX
#define CY_TCPWM_QUADDEC_CAPTURE_ON_INDEX
Capture on Index.
Definition: cy_tcpwm_quaddec.h:216
CY_TCPWM_QUADDEC_MODE_RANGE0
#define CY_TCPWM_QUADDEC_MODE_RANGE0
Counter Range between 0x0000 and 0xFFFF / 0xFFFFFFFF (for GRP_CNT_WIDTH = 16 / 32)
Definition: cy_tcpwm_quaddec.h:191

## 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.

## 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);

## API Reference
| Macros
| Functions
| Data Structures

## Macros
#define | Cy_TCPWM_QuadDec_GetCapture Cy_TCPWM_QuadDec_GetCapture0Val
#define | Cy_TCPWM_QuadDec_GetCaptureBuf Cy_TCPWM_QuadDec_GetCapture0BufVal