This code example shows how to configure the timer function using TCPWM (Timer and Counter) block and describes its operation and initial settings.
The device used in this code example (CE) is:
The board used for testing is:
The PWM timer is running in up-count mode, and this code example allows you to measure the period of any timer and simultaneously outputting USER LED1 in the interrupt handler, allowing you to visually check the period.
TCPWM block in PSoC™ 4 HV (High Voltage) implements a 16bit 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 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:
- PSoC™ 4 HV (High Voltage) Series
This Code example has been developed for:
-
GPIO pins list
Board USER_LED Note KIT_PSoC4-HVMS-128K_LITE
P0[7] None KIT_PSoC4-HVMS-64K_LITE
P0[7] None
The PWM timer is running in up-count mode, and this code example allows you to measure the period of any timer what you decided by Device Configurator and simultaneously inverted output to USER LED1 in the interrupt handler, allowing you to visually check the period. When the counter reached not only period value but also compare value, Interrupt handler of Timer will be occurred, but at the compare value is skipped in this interrupt handler.
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
By using Device Configurator, user can easy to make voluntary value timer, and also to confirm period of interrupt handler with USER LED outputting.
- The configurations regarding the USER_LED and the clock used by TCPWM are reflected in cybsp_init() automatically
- The interrupt handler Timer_Handler() is registered by Cy_SysInt_Init(), and calling NVIC_SetPriority() and NVIC_EnableIRQ() to enable the interrupt
- Then Cy_TCPWM_Counter_Init() enables the configured TCPWM functionality
- And finaly to Enables the counter by Cy_TCPWM_Counter_Enable() and triggers a software start by Cy_TCPWM_TriggerStart()
Regarding Interrupt handler in this code example
- 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_TC, it means reached terminal count, so the function calls Cy_GPIO_Inv() to inverse output port signal
- Although otherwise the interrupt reason is CY_TCPWM_INT_ON_CC, no action in this function.
Miscellaneous settings
- Nothing in particular
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 Application 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 LED port pin(0_7) to check the period timing and also can confirm it by the visibility of the LED ON/OFF.
Figure: Timer waveform
- 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.
Relevant Application notes are:
- AN0034 - Getting started with PSOC™ 4 HV MS MCUs in ModusToolbox™
ModusToolbox™ is available online:
Associated PSoC™ 4 HVMS 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 PSoC™ 4 HVMS Forum: