To avoid a floating pin declare it as "Resistive pull up", initial drive mode "high". So the pin will get shortened to ground whenthe button is pressed.
SW2 connects to a button, so its drive mode should not be configured to Strong mode, connect a strong 1 to external GND directly may damage the silicon because you short internal vddio and external gnd together.
Please configure the pin to resister-pull-up drive mode, you code don't need to change.
I tried what Bob-san wrote by using CY8CKIT-145-40XX.
The config of SW is
uint8_t pin_state ;
CyGlobalIntEnable; /* Enable global interrupts. */
/* Place your initialization/startup code here (e.g. MyInst_Start()) */
pin_state = SW_Read() ;
LED_Write( pin_state ) ;
When I don't touch the SW, LED is off, when I push the SW, LED turns on.
sw_test_4045.cyprj.Archive01.zip 365.8 K
TKS everyone , it's ok ,tks;