1. 安装STM32 Core支持包
2. 选择对应的板
3. 打开例程测试
4. 自由发挥
- int UserButton = PC13;
- bool NeedBlink = false;
- int KeyPreStatus = HIGH; // 默认为未按下
- int PressCnt = 0;
- int TimeCnt = 0;
- void setup() {
- // put your setup code here, to run once:
- pinMode(LED_BUILTIN, OUTPUT);
- pinMode(UserButton, INPUT);
- }
- void loop() {
- // 按键控制变频闪烁
- if(NeedBlink == true) {
- if(digitalRead(LED_BUILTIN) == HIGH) {
- digitalWrite(LED_BUILTIN, LOW);
- }
- else {
- digitalWrite(LED_BUILTIN, HIGH);
- }
- NeedBlink = false;
- }
-
- if(digitalRead(UserButton) == KeyPreStatus) // 按键扫描,消抖
- {
- if(KeyPreStatus == LOW) {
- PressCnt++;
- if((PressCnt % 50) == 0) { // 50ms
- NeedBlink = true;
- }
- }
- else {
- TimeCnt++;
- if(TimeCnt > 1000) { // 1s
- TimeCnt = 0;
- NeedBlink = true;
- }
- }
- }
- else {
- KeyPreStatus = digitalRead(UserButton);
- PressCnt = 0;
- }
-
- delay(1);
- }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
- int UserButton = PC13;
- bool NeedBlink = false;
- int KeyPreStatus = HIGH; // 默认为未按下
- int PressCnt = 0;
- int TimeCnt = 0;
- float time;
- void setup() {
- // put your setup code here, to run once:
- pinMode(LED_BUILTIN, OUTPUT);
- pinMode(UserButton, INPUT);
- Serial.begin(115200, SERIAL_8N1);
- }
- void loop() {
- // 按键控制变频闪烁
- if (NeedBlink == true) {
- if (digitalRead(LED_BUILTIN) == HIGH) {
- digitalWrite(LED_BUILTIN, LOW);
- }
- else {
- digitalWrite(LED_BUILTIN, HIGH);
- }
- NeedBlink = false;
- }
- if (digitalRead(UserButton) == KeyPreStatus) // 按键扫描,消抖
- {
- if (KeyPreStatus == LOW) {
- PressCnt++;
- if ((PressCnt % 50) == 0) { // 50ms
- NeedBlink = true;
- }
- }
- else {
- TimeCnt++;
- if (TimeCnt > 1000) { // 1s
- TimeCnt = 0;
- NeedBlink = true;
- }
- }
- }
- else {
- if((KeyPreStatus == HIGH) && (digitalRead(UserButton) == LOW)) {
- time = millis() / 1000.0f;
- Serial.print(time);
- Serial.println(" Key pressed.");
- }
- else if((KeyPreStatus == LOW) && (digitalRead(UserButton) == HIGH)) {
- time = millis() / 1000.0f - time;
- Serial.print(time);
- Serial.println(" Key released.");
- }
- KeyPreStatus = digitalRead(UserButton);
- PressCnt = 0;
- }
- delayMicroseconds(500);
- }
复制代码一周热门 更多>