Measuring Duration of CapSense Button Press Using SysTickTimer - KBA226863

Author: ShanmathiN_06           Version: **

 

Question:

How can the duration when CapSense® sensor is active be measured using SysTick Timer?

 

Answer:

Follow these steps to measure the duration of button press using SysTick timer:

  1. Place a CapSense Component in top design and add a button widget.
  2. 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.
  3. Find an unused callback slot and assign the callback using CySysTickSetCallback() API.
  4. Increment the counter value if an interrupt is generated (every 1 ms) and the sensor is active.
  5. 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.