Measuring Duration of CapSense Button Press Using SysTickTimer
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
How can the duration be measured when CapSense® sensor is active, using SysTick Timer?
Follow these steps to measure the duration of button press using SysTick timer:
- Place a CapSense Component in top design and add a button widget.
- In the main.c file, make sure that both the CapSense Component and the SysTick timer are initialized using the CapSense_Start() and CySysTickStart() APIs.
- Find an unused callback slot and assign the callback using CySysTickSetCallback() API.
- Increment the counter value if an interrupt is generated (every 1 ms) and the sensor is active.
- When sensor is released, process the counter value which is the duration for which the sensor was active in milliseconds.
Here is an example code for main.c:
#include <project.h>
#include <stdio.h>
// Macros
#define TRUE 1
#define FALSE 0
/* Global Variables */
//Counter value that determines the duration of button press in milliseconds
uint32 counterVal;
//Boolean variable to determine the touch status of sensor
_Bool button_active;
/* Function Prototypes */
//SysTick ISR callback function
void SysTickISRCallback(void);
int main()
{
uint32 i;//for loop variable
char msg[30];//Message to print in UART
/* Initialize variables. */
counterVal=0;
button_active=FALSE;
/* Enable global interrupts. */
CyGlobalIntEnable;
CySysTickStart();//Start SysTick timer
UART_Start();//Start the UART component
CapSense_Start();//Start the CapSense component
/* Find unused callback slot and assign the callback. */
for (i = 0u; i < CY_SYS_SYST_NUM_OF_CALLBACKS; ++i)
{
if (CySysTickGetCallback(i) == NULL)
{
/* Set callback */
CySysTickSetCallback(i, SysTickISRCallback);
break;
}
}
//Scan all the CapSense widgets
CapSense_ScanAllWidgets();
for(;;)
{
if(!CapSense_IsBusy())//If CapSense is not busy
{
//Process button 0 widget
CapSense_ProcessWidget(CapSense_BUTTON0_WDGT_ID);
if(CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID))//If the button 0 widget is active
{
button_active=TRUE;
}
else //If button 0 widget is not active
{
button_active=FALSE;
}
//If the button is released, that is, lift-off condition
if((!button_active)&&(counterVal!=0))
{
sprintf(msg,"Duration = %ld ms\r\n", counterVal);
//Print the duration for which button was pressed through UART
UART_UartPutString(msg);
counterVal=0;//Reset counter
}
//Scan all the CapSense widgets
CapSense_ScanAllWidgets();
}
}
}
//Systick ISR callback function
void SysTickISRCallback(void)
{
if(button_active)//If the button is pressed
{
counterVal++;// Increment counter
}
}
Note: This method is not limited to buttons; you can use this method for any CapSense widget.
Author: ShanmathiN_06 Version: **
Translation - Japanese: SysTickTimerを使用してCapSenseボタンを押す時間を測定する - KBA226863 - Community Translated (JA)
- Tags:
- button press duration
- capsense
- CapSense Button Press
- capsense component
- capsense_csd
- firmware
- long press
- measuring duration
- psoc
- systick
- systick timer
- SysTickTimer