This code example shows how to configure the capture function using TCPWM (Timer, Counter and Pulse Width Modulator) block and describes its operation and initial settings.
The device used in this code example (CE) is:
The board used for testing is:
- TRAVEO™ T2G Cluster 6M Lite Kit (KIT_T2G_C-2D-6M_LITE)
The example configures TCPWM (Timer, Counter and Pulse Width Modulator) as the Counter mode with User Button2 (USER_BTN2) as input. The button is used as start, reload and stop trigger. In addition, the rising edge made by the button release will be captured by the counter.
TCPWM block in TRAVEO™ T2G implements a 16 or 32-bit timer, counter, pulse width modulator (PWM), pseudo random PWM, shift register, and quadrature decoder functionality.
The TCPWM block has the following features:
-
Supports up to four counter groups (device specific)
-
Each counter group consists up to 256 counters (counter group specific)
-
Each counter can run in one of seven function modes
-
Timer-counter with compare
- Timer-counter with capture
- Quadrature decoding
- Pulse width modulation/stepper motor control (SMC) for pointer instruments
- PWM with dead time/three-phase motor control (Brushless-DC, BLDC)
- Pseudo-random PWM
- Shift register mode
-
16-bit or 32-bit counters (counter group specific)
-
Up, down, and up/down counting modes
-
Clock prescaling (division by 1, 2, 4, ... 64, 128)
-
Up to two capture and compare functions (counter group specific)
-
Double buffering of all compare/capture and period registers
-
Two output trigger signals for each counter to indicate underflow, overflow, and capture/compare events; they can also directly be connected with the line output signal
-
Supports interrupt on:
- Terminal Count - Depends on the mode; typically occurs on overflow or underflow
- Capture/Compare - The count is captured in the capture registers or the counter value equals the value in the compare register
-
Integrate board-level Boolean logic functionality into a port
-
Ability to pre-process high-speed I/O matrix (HSIOM) input signals from the General Purpose I/O (GPIO) port pins
-
Support in all device power modes
-
Integrate closely to the I/O pads, providing shortest signal paths with programmability
More details can be found in:
- TRAVEO™ T2G CYT4DN
This CE has been developed for:
- TRAVEO™ T2G Cluster 6M Lite Kit (KIT_T2G_C-2D-6M_LITE)
Figure 1. KIT_T2G_C-2D-6M_LITE (Top View)
- GPIO pins list
Board USER_BTN TCPWM input pin Note KIT_T2G_C-2D-6M_LITE
User Button2 (P0[3]) P0[3] None
The firmware is implemented in main.c and performs the following functions:
- In this code example, TCPWM counter is configured as down counting timer with the clock 1 kHz.
- The counter will be triggered by falling edge of the User Button2 and the initial counter value 99999 will be reloaded at the same time.
- The timer captures the counter value at rising edge of the button with compare/capture 0 (CC0) Interrupt.
- If the User Button2 is not released 10 seconds, a terminal count (TC) interrupt will occur.
- Both the interrupts will show the status information on the terminal.
ModusToolbox™ Device configurator
This code example project can be created or imported using ModusToolbox™ IDE. To configure the project in the Quick Panel, click on Device Configurator
Figure 2. Select Device Configurator
Figure 4. Configuration User Button2
The basic idea is that the TCPWM will capture and store the counter values to the registers when the User Button2 is released.
- Cy_TCPWM_Counter_Init() reflects the TCPWM configuration to the device
- The configurations regarding the USER_BTN2 and the clock used by TCPWM are reflected in cybsp_init() automatically
- The interrupt handler handle_TCPWM_IRQ() is registered by Cy_SysInt_Init(), and calling NVIC_SetPriority() and NVIC_EnableIRQ() to enable the interrupt
- Then Cy_TCPWM_Counter_Enable() enables the configured TCPWM functionality
This code example asks user to press and release the User button2 to start the counter and to capture the time interval. The function handle_TCPWM_IRQ() handles two types of interrupt. After the TCPWM channel starts counting by a falling edge of USER_BTN2 (means User Button2 push), the function will be called when a rising edge (means User Button2 release) has been detected. Or also when an underflow (means 10 seconds passed without User Button2 release) has been detected.
- Firstly, this function calls Cy_TCPWM_GetInterruptStatusMasked() to get interrupt reason, then calls Cy_TCPWM_ClearInterrupt() to clear it
- If the interrupt reason is CY_TCPWM_INT_ON_CC0, it means rising edge was detected, so the function calls Cy_TCPWM_Counter_GetCapture0Val() to get captured counter value
- Otherwise if the interrupt reason is CY_TCPWM_INT_ON_TC, it means underflow was detected
Miscellaneous settings
-
STDIN / STDOUT setting
- Calling cy_retarget_io_init() function to use UART as STDIN / STDOUT
- Initialize the port defined as CYBSP_DEBUG_UART_TX as UART TX, defined as CYBSP_DEBUG_UART_RX as UART RX (these pins are connected to KitProg3 COM port)
- The serial port parameters become to 8N1 and 115200 baud
- Calling cy_retarget_io_init() function to use UART as STDIN / STDOUT
Before testing this code example:
- Power the board through the dedicated power connector
- Connect the board to the PC through the USB interface
- Build the project using the dedicated Build button
or by right-clicking the project name and selecting "Build Project"
- To program the board, in the Quick Panel, scroll down, and click [Project Name] Program (KitProg3_MiniProg4)
For this example, an oscilloscope is needed to monitor User Button2 port pin to check the correctness of the time interval captured by the software. In this example, Picoscope3000 series is used with SW Pico scope 7 to monitor user button signal.
-
Open the terminal program (example download and install Tera Term select the KitProg3 COM port. Set the serial port parameters to 8N1 and 115200 baud.
-
After programming, the code example starts automatically and asks for User Button2 press and releases.
Figure: Terminal startup
- Press and release the User button2.
- Monitor the time interval of User button2 press/release using oscilloscope and Terminal.
Figure: Terminal and Scope Output
- You can debug the example to step through the code. In the IDE, use the [Project Name] Debug (KitProg3_MiniProg4) configuration in the Quick Panel. For details, see the "Program and debug" section in the Eclipse IDE for ModusToolbox™ software user guide.
Note: (Only while debugging) On the CM7 CPU, some code in main() may execute before the debugger halts at the beginning of main(). This means that some code executes twice: once before the debugger stops execution, and again after the debugger resets the program counter to the beginning of main(). See KBA231071 to learn about this and for the workaround.
Relevant Application notes are:
- AN235305 - Getting started with TRAVEO™ T2G family MCUs in ModusToolbox™
- AN220224 - How to Use Timer, Counter, and PWM (TCPWM) in Traveo II Family
ModusToolbox™ is available online:
Associated TRAVEO™ T2G MCUs can be found on:
More code examples can be found on the GIT repository:
For additional trainings, visit our webpage:
For questions and support, use the TRAVEO™ T2G Forum: