130
社区成员
发帖
与我相关
我的任务
分享
【试读开放时间】2022/1/21 - 2021/1/24
【试读文章内容】7日玩转ESP32——(第2日) GPIO输入,按键的长按和短按
【试读文章链接】https://blog.csdn.net/kangweijian/article/details/122497443
崩溃的年底,忙的焦头烂额,赶紧抽空来打卡
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#define GPIO_INPUT_IO_0 9
#define GPIO_INPUT_PIN_SEL (1ULL<<GPIO_INPUT_IO_0)
#define ESP_INTR_FLAG_DEFAULT 0
int index = 0;
void key_scan(){
if(gpio_get_level(GPIO_INPUT_IO_0)==0){
while(gpio_get_level(GPIO_INPUT_IO_0)==0){
vTaskDelay(100 / portTICK_RATE_MS);
index++;
if(gpio_get_level(GPIO_INPUT_IO_0)==1){
break;
}
}
if(index>30)
printf("按键长按\r\n");
else
printf("按键短按\r\n");
}
}
void app_main(void)
{
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_POSEDGE;
io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_up_en = 1;
gpio_config(&io_conf);
while(1) {
key_scan();
}
}
打卡打卡
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#define GPIO_INPUT_IO_0 9
#define GPIO_INPUT_PIN_SEL (1ULL<<GPIO_INPUT_IO_0)
#define ESP_INTR_FLAG_DEFAULT 0
void app_main(void)
{
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_POSEDGE;
io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_up_en = 1;
gpio_config(&io_conf);
while(1) {
vTaskDelay(10 / portTICK_RATE_MS);
if(gpio_get_level(GPIO_INPUT_IO_0)==0){
uint32_t count = 0;
printf("按键...\r\n");
while(gpio_get_level(GPIO_INPUT_IO_0)==0){
vTaskDelay(10 / portTICK_RATE_MS);
count++;
// printf("按键 count=%d\r\n", count);
if(gpio_get_level(GPIO_INPUT_IO_0)==1){
break;
}
}
if(count>300)
printf("按键长按\r\n");
else
printf("按键短按\r\n");
}
}
}
学习打卡:
试了一下午进程创建,在进程里printf总是会触发系统崩溃重启,还以为是不能在进程中printf
最后发现是尝试用%s把int型变量打印出来,我也崩溃了
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <driver/gpio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
const char* TAG = "DBG";
static int key_count = 0;
#define GPIO_INPUT_IO_0 9
#define GPIO_INPUT_PIN_SEL (1ULL<<GPIO_INPUT_IO_0)
static void key_scan(void *arg)
{
static int32_t time_count=0;
while(1)
{
//该函数用来读取按键状态
//为0按下
if(!gpio_get_level(GPIO_NUM_9))
{
time_count = 0;
//等待用户放手
while(!gpio_get_level(GPIO_NUM_9))
{
time_count++;
vTaskDelay(10/portTICK_RATE_MS); //不延迟会阻塞
}
if(time_count<500)
{
key_count++;
printf("按键短按, 当前键值:%d\r\n", key_count);
}
else
{
key_count = 0;
printf("按键长按,当前键值:%d\r\n", key_count);
}
}
}
}
void app_main(void)
{
esp_err_t err;
//与32类似,也使用结构体来配置GPIO
//gpio_types.h里面能挖出这个结构体
gpio_config_t gpio_config_struct = {};
gpio_config_struct.intr_type = GPIO_INTR_DISABLE; //不中断
gpio_config_struct.mode = GPIO_MODE_INPUT; //设定IO为输入且不是开漏
gpio_config_struct.pin_bit_mask = GPIO_INPUT_PIN_SEL;
gpio_config_struct.pull_down_en = 0; //关闭下拉
gpio_config_struct.pull_up_en = 0; //关闭上拉,因为核心板上已经有上拉了
err = gpio_config(&gpio_config_struct); //与32类似
if(err != ESP_OK)
{
printf("GPIO初始化失败!\r\n");
printf("err: %d\r\n", err);
while(1);
}
printf("GPIO初始化成功!\r\n");
xTaskCreate(key_scan,
"key_scan",
2048,
NULL,
10,
NULL);
while(1)
{
vTaskDelay(10/portTICK_RATE_MS); //触发任务切换
}
}