1 Reply Latest reply on Aug 18, 2020 1:35 AM by AH_96

    CapSense Cp value check

    taas_3144281

      Hi.

       

      This sample uses CY8CKIT-149.

      This sample displays the Cp values of each buttons to the serial terminal at start up.

      After that button press(es) will be reflected to the corresponding LEDs.

      I hope this sample helps in evaluating CapSense.

       

       

       

      schematics

       

       

       

      Pin list

       

       

       

      main.c

      ====================================================================

       

      /*******************************************************************************

      *   Included Headers

      *******************************************************************************/

      #include "CapSense.h"

      #include <project.h>

      #include "stdio.h"

       

       

      /*****************************************************************************

      * MACRO Definitions

      *****************************************************************************/  

      #define LED_ON      (0u)        /* Pin state to turn ON an active-LOW LED */

      #define LED_OFF     (1u)        /* Pin state to turn OFF an active-LOW LED */

       

       

      /* Finite state machine for device operating states

          SENSOR_SCAN - Sensors are scanned in this state

          WAIT_FOR_SCAN_COMPLETE - CPU is put to sleep in this state

          PROCESS_DATA - Sensor data is processed, LEDs are controlled,

                         and I2C buffer is updated in this state */

      typedef enum

      {

          SENSOR_SCAN = 0x01u,              

          WAIT_FOR_SCAN_COMPLETE = 0x02u,   

          PROCESS_DATA = 0x03u,             

      } DEVICE_STATE;

       

       

       

       

      /*****************************************************************************

      * Function Prototypes

      *****************************************************************************/

      void TestSensorCapacitance(void) ; /* same with CapSense_RunSelfTest(CapSense_TST_SNS_CAP) ; */

      void cls() ;

      void print(char *str) ;

      #define STR_LEN 64

      char str[STR_LEN+1] ;

       

       

      int main()

      {

                

          /* Variable to hold the current device state

          *  State machine starts with Sensor_Scan state after power-up

          */

          DEVICE_STATE currentState = SENSOR_SCAN;

               

           /* Enable global interrupts. */

          CyGlobalIntEnable;

        

          //sizeof(CapSense_dsRam),

          //(uint8_t *)&(CapSense_dsRam));

          CapSense_Start(); /* Initialize Component */

        

          /*CapSense Cp value check */

          CapSense_RunSelfTest(CapSense_TST_RUN_SELF_TEST_MASK) ;

        

          UART_Start() ;

        

          print("CapSense cp\n ") ;  

          snprintf(str, STR_LEN, "BTN0(%d) BTN1(%d) BTN2(%d) \n\r",

               CapSense_BTN0_SNS_CP0_VALUE , CapSense_BTN1_SNS_CP0_VALUE, CapSense_BTN2_SNS_CP0_VALUE ) ;

          print(str) ;

        

          for(;;)

          {

              /* Switch between SENSOR_SCAN->WAIT_FOR_SCAN_COMPLETE->PROCESS_DATA states */

              switch(currentState)

              {

                  case SENSOR_SCAN:

                  /* Initiate new scan only if the CapSense block is idle */

                      if(CapSense_NOT_BUSY == CapSense_IsBusy())

                      {

                          #if ENABLE_TUNER

                              /* Update CapSense parameters set via CapSense tuner before the

                                 beginning of CapSense scan

                              */

                              CapSense_RunTuner();

                          #endif

                        

                          /* Scan widget configured by CSDSetupWidget API */

                          CapSense_ScanAllWidgets();

                                            

                          /* Set next state to WAIT_FOR_SCAN_COMPLETE  */

                          currentState = WAIT_FOR_SCAN_COMPLETE;

                      }

                      break;

       

       

                  case WAIT_FOR_SCAN_COMPLETE:

       

       

                      /* Put the device to CPU Sleep until CapSense scanning is complete*/

                      if(CapSense_NOT_BUSY != CapSense_IsBusy())

                      {

                          CySysPmSleep();

                      }

                      /* If CapSense scanning is complete, process the CapSense data */

                      else

                      {

                          currentState = PROCESS_DATA;

                      }

                      break;

            

                  case PROCESS_DATA:

                                   

                      /* Process data on all the enabled widgets */

                      CapSense_ProcessAllWidgets();

                    

                      /* Controls LEDs Status based on the result of Widget processing. */

                      LED_13_Write(CapSense_IsWidgetActive(CapSense_BTN0_WDGT_ID) ? LED_ON : LED_OFF );

                      LED_12_Write(CapSense_IsWidgetActive(CapSense_BTN1_WDGT_ID) ? LED_ON : LED_OFF );

                      LED_11_Write(CapSense_IsWidgetActive(CapSense_BTN2_WDGT_ID) ? LED_ON : LED_OFF );

                          

                    

                      /* Set the device state to SENSOR_SCAN */

                      currentState = SENSOR_SCAN;

                      break;

                 

                  /*******************************************************************

                   * Unknown power mode state. Unexpected situation.

                   ******************************************************************/  

                  default:

                      break;

              }

          }

      }

       

       

      void cls(void)

      {

          print("\033c") ; /* reset */

          CyDelay(20) ;

          print("\033[2J") ; /* clear screen */

          CyDelay(20) ;

      }

       

       

      void print(char *str)

      {

          UART_UartPutString(str) ;

      }

       

       

      /* [] END OF FILE */

       

      ====================================================================

       

       

       

      Thanks,

      Asanuma

        • 1. Re: CapSense Cp value check
          AH_96

          Hello Asanuma-san,

           

          Thank you for sharing your example project with the community.

           

          I see that you are using CapSense V5.0 in this project, the latest CapSense version is CapSense V7.0 and we recommend using this for new applications.

           

          Best regards,
          Hari