8572 views|0 replies

291

Posts

5

Resources
The OP
 

[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: After completion, you can see a blank schematic diagram, and the project list on the left also has only the main function, as shown below: We can open the main function and take a look, and compile it first, 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: There is a user button (3.7) and a user LED (3.4) on the board, which are assigned to the pin configuration: 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 the program first, and an error will be reported. To increase the output, you need to connect something, as shown below: We don't need to connect the LED on the schematic, so we can remove this pin, double-click it, and uncheck the HW connection, as shown below: 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.
  1. #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: Compile and test again, the test is OK. No video was recorded, and the two test pictures are as follows:

This post is from MCU
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list