[CY8CKIT-149 PSoC 4100S Evaluation] + Create a blank project (taking Key-LED as an example)[Copy link]
The previous explorations were all based on modifications to the official examples. This time we create a blank project and add components to write the project. We will also introduce a simple example of controlling an LED with a button. First, create a blank project, as shown below:
Drag an input Pin and an output Pin into the schematic diagram. The input is used as the input of the button, and the output is used to control the LED on and off, as shown below:
Take a look at the user button and user LED on the board schematic diagram. When the button is pressed, it is grounded, and the LED needs a high level to light up:
Compile again and it's OK. We can write the main function. The function we want to achieve is that when the button is pressed, the state of the LED will flip, that is, press it once to turn on, and press it again to turn off. Write a delay function for key debounce. Define a bool variable to control the state of the LED. The while(Pin_Key_Read()==0){} statement is to wait for the button to be released.
#include "project.h" void delay_ms(uint16 t) { uint16 a,b,c; for(a=100;a>0;a--) for(b=114;b>0;b--) for(c=t;c>0;c--); } int main(void) { CyGlobalIntEnable; /* Enable global interrupts. */ Pin_LED_Write(0); /* Place your initialization/startup code here (eg MyInst_Start()) */ _Bool STATE=1; for(;;) { /* Place your application code here. */ if(Pin_Key_Read()==0) { delay_ms(10); if(Pin_Key_Read()==0) { Pin_LED_Write(STATE); STATE=!STATE; while(Pin_Key_Read()==0){} } } } }
复制代码
After compiling and downloading, the LED is always on and the button is invalid? ? ? After careful analysis, I forgot to add a pull-up to the button, because the button pin is in a floating state at this time. I studied the functions in the key and found that there seems to be no function that can be used to configure the pull-up mode. Finally, I found it in the schematic interface. Double-click Pin_Key, select Open drain and drives high for the drive mode (HW connection needs to be unchecked), and select High for the initial state, as shown below: