2 Replies Latest reply on Sep 24, 2020 5:52 AM by AH_96

    Capsense example

    MoOu_4768141

      Hello,

       

      I´m trying to get the CapSense module to work. I tried to inspire from the Example, but it has other function namings.

       

      I want to create two Buttons, when one of the Buttons is pressed, turn on an LED

       

      1.

      11.JPG

      2. in the Code I called following functions:

       

         CapSense_Start();

       

         CapSense_InitializeAllBaselines();

       

         CapSense_ProcessAllWidgets();

       

           /* Check whether the scanning of all enabled widgets is completed. */
              if(0u == CapSense_IsBusy())
              {
                  /* Update all baselines */
                  CapSense_UpdateAllBaselines();

                  /* Start scanning all enabled sensors */
                  CapSense_Scan();
              }

              /* Display CapSense state using LED */
           

       

        if (CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID))

          {

             Pin_Write(0);

          }

          else

            Pin_Write(1);

       

       

       

      Could you please let me know what is missing? the Code is compiling, but the LED never turns on.

       

      Thanks and Regards

      M.

        • 1. Re: Capsense example
          MoTa_728816

          Hi,

           

          Reading your question, I remembered that I have done something similar.

          And found one from my workspace, but it was for 23 buttons!

          So I tailored it to 2 buttons version for CY8CKIT-044.

           

          Probably the point is

           

          you need to call

                  if (CapSense_IsSensorActive(CapSense_BUTTON_WDGT_ID,CapSense_BUTTON_SNS0_ID)) { // for button1

          or

                  if (CapSense_IsSensorActive(CapSense_BUTTON_WDGT_ID,CapSense_BUTTON_SNS1_ID)) { // for button2

           

          To support many more buttons, I placed these IDs into an array like below

           

          uint8_t push_button_id[NUM_PUSH_BUTTON] = {

            CapSense_BUTTON_SNS0_ID,

            CapSense_BUTTON_SNS1_ID

          } ;

           

          To light LED modify the function dump_push_button() and replace

          print("B") to light LED and print("-") to deim LED.

           

          Schematic

          002-schematic.JPG

          CapSense Config

          Note: I used only 1 Button which supports 2 buttons (actually it was 23 when I found this)

          003-CapSense-Config.JPG

           

          Pins

          004-pins.JPG

           

          main.c

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

          #include "project.h"

          #include "stdio.h"

           

          #define USE_TUNER 0

          #define USE_UART 1

           

          #define NUM_PUSH_BUTTON   2

          uint8_t push_button[NUM_PUSH_BUTTON] = { 0u } ;

           

          uint8_t push_button_id[NUM_PUSH_BUTTON] = {

            CapSense_BUTTON_SNS0_ID,

            CapSense_BUTTON_SNS1_ID

          } ;

           

          #if USE_UART

          #define STR_LEN 64

          char str[STR_LEN+1] ;

          void print(char *str)

          {

              UART_UartPutString(str) ;

          }

           

          void cls(void)

          {

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

              CyDelay(20) ;

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

              CyDelay(20) ;

          }

           

          void splash(void)

          {

              cls() ;

              snprintf(str, STR_LEN, "CapSense %d buttons test ", NUM_PUSH_BUTTON) ;

              print(str) ;

              snprintf(str, STR_LEN, "(%s %s)\n", __DATE__, __TIME__) ;

              print(str) ;

          }

           

          void dump_push_button(void)

          {

              int i ;

              for (i = 0 ; i < NUM_PUSH_BUTTON ; i++ ) {

                  if (push_button[i]) {

                      print("B") ;

                  } else {

                      print("-") ;

                  }

              }

              print("\n") ;

          }

          #endif

           

          volatile int sec_flag = 0 ;

          volatile uint16_t tick_count = 0 ;

           

          CY_ISR(tick_isr)

          {

              tick_count++ ;

              if (tick_count >= 1000) {

                  sec_flag = 1 ;

                  tick_count = 0 ;

              }

          }

           

          void init_hardware(void)

          {

             CyGlobalIntEnable;

            

             CySysTickStart() ;

             CySysTickSetCallback(0, tick_isr) ;

             

             CapSense_Start() ;

           

             #if USE_TUNER

              EZI2C_Start() ;

              EZI2C_EzI2CSetBuffer1(sizeof(CapSense_dsRam),

                                  sizeof(CapSense_dsRam),

                                  (uint8_t *)&(CapSense_dsRam)) ;

             #endif

             #if USE_UART

              UART_Start() ;

              splash() ;

             #endif

           

           

              CapSense_Start() ;

              CapSense_ScanAllWidgets() ;

          }

           

          uint16_t check_push_button(void)

          {

              uint16_t num_pushed = 0 ;

              int i ;

              for (i = 0 ; i < NUM_PUSH_BUTTON ; i++ ) {

                  if (CapSense_IsSensorActive(CapSense_BUTTON_WDGT_ID, push_button_id[i])) {

                      push_button[i] = 1 ;

                      num_pushed++ ;

                  } else {

                      push_button[i] = 0 ;

                  }

              }

              return(num_pushed) ;

          }

           

          void dump_button(void)

          {

          #if USE_UART

              int i ;

              for (i = 0 ; i < NUM_PUSH_BUTTON ; i++ ) {

                  if (push_button[i]) {

                      print("B") ;

                  } else {

                      print("-") ;

                  }

              }

          #endif

          }

           

          int main(void)

          {

              init_hardware() ;

             

              for(;;) {

                  if (!CapSense_IsBusy()) {

                      CapSense_ProcessAllWidgets() ;

                     

                      check_push_button() ;

                     

          #if USE_TUNER

                      CapSense_RunTuner()  ;

          #endif

                     

                      CapSense_ScanAllWidgets() ;

                  }

           

           

                  if (sec_flag) {

          #if USE_UART           

                      dump_push_button() ;

          #endif

                      sec_flag = 0 ;

                  }

                 

              }

          }

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

           

          Tera Term Log

          001-TeraTerm-log.JPG

           

          moto

          • 2. Re: Capsense example
            AH_96

            Hi MoOu_4768141

             

            I have updated your code with few changes. Please try this out.

             

             CapSense_Start();
            
               //CapSense_InitializeAllBaselines(); -> Not required for latest component. This is done internally
            
               //CapSense_ProcessAllWidgets(); -> Commented this out as it is not required
             while(1) //Added a forever loop to keep scanning the sensor
            {
                 /* Check whether the scanning of all enabled widgets is completed. */
                    if(0u == CapSense_IsBusy())
                    {
                        /* Update all baselines */
                        CapSense_ProcessAllWidgets(); // Modified. This is the updated function for the new component
                        /* Start scanning all enabled sensors */
                        CapSense_ScanAllWidgets(); //Modified
                    }
                    /* Display CapSense state using LED */
                 
            
              if (CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID))
                {
                   Pin_Write(0);
                }
                else
                  Pin_Write(1);
            }
            

             

             

            Best regards,

            Hari