esp32 GPIO 分别用5种中断类型控制LED

打印 上一主题 下一主题

主题 1612|帖子 1612|积分 4836

下面步伐分别用ANYEDGE   POSEDGE    NEGEDGE    HIGH_LEVEL    LOW_LEVEL
中断类型控制GPIO  0 脚的电平。此步伐的重点是用延时消除按键产生的无用中断信号
 
硬件 1. led  接0脚和地
         2.  按钮接gpio 1脚 和地或3.3v 脚
图片

 
步伐
  1. #include "driver/gpio.h"
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "esp_log.h"
  5. #include "esp_timer.h"
  6. #define AN   1  // 按钮接在 GPIO 1 上
  7. #define LED   0     // LED 接在 GPIO 0 上
  8. #define DELAY 500     //200ms    延时消除按键抖动
  9. static int led_state = 0;
  10. static int64_t last_time = 0;   
  11. // 中断处理函数
  12. void IRAM_ATTR handler(void* arg) {
  13.     int64_t time = esp_timer_get_time();    //此次中断到开机的微秒数
  14.    
  15.     // 检查时间间隔是否大于消抖时间
  16.     if ((time - last_time) > DELAY * 1000) {
  17.         // 切换 LED 状态
  18.         led_state = !led_state;
  19.         gpio_set_level(LED, led_state);
  20.         // 更新最后一次有效中断时间
  21.         last_time = time;
  22.     }
  23. }
  24. void app_main(void) {
  25.     // 配置 LED GPIO 为输出模式   gpio 0 脚为1,led 亮
  26.     gpio_config_t io_conf;
  27.     io_conf.intr_type = GPIO_INTR_DISABLE;
  28.     io_conf.mode = GPIO_MODE_OUTPUT;
  29.     io_conf.pin_bit_mask = (1ULL << LED);
  30.     io_conf.pull_down_en = 0;
  31.     io_conf.pull_up_en = 0;
  32.     gpio_config(&io_conf);
  33.     // 下降沿触发  按一下亮,再按一下灭 按钮接地
  34. /*   io_conf.intr_type = GPIO_INTR_NEGEDGE;  )
  35.     io_conf.mode = GPIO_MODE_INPUT;
  36.     io_conf.pin_bit_mask = (1ULL << AN);
  37.     io_conf.pull_down_en =0;
  38.     io_conf.pull_up_en = 1;  
  39.     gpio_config(&io_conf);
  40.        
  41.             // 上升沿触发 按一下亮,再按一下灭,按钮接3.3v
  42.     io_conf.intr_type = GPIO_INTR_POSEDGE;
  43.     io_conf.mode = GPIO_MODE_INPUT;
  44.     io_conf.pin_bit_mask = (1ULL << AN);
  45.     io_conf.pull_down_en =1;
  46.     io_conf.pull_up_en = 0;  
  47.     gpio_config(&io_conf);
  48.           
  49.     //高电平触发 按一下亮,再按一下灭,按钮接3.3v
  50.     io_conf.intr_type = GPIO_INTR_HIGH_LEVEL;
  51.     io_conf.mode = GPIO_MODE_INPUT;
  52.     io_conf.pin_bit_mask = (1ULL << AN);
  53.     io_conf.pull_down_en =1;
  54.     io_conf.pull_up_en = 0;  
  55.     gpio_config(&io_conf);
  56.        
  57.          //低电平触发 按一下亮,再按一下灭,按钮接地
  58.     io_conf.intr_type = GPIO_INTR_LOW_LEVEL;
  59.     io_conf.mode = GPIO_MODE_INPUT;
  60.     io_conf.pin_bit_mask = (1ULL << AN);
  61.     io_conf.pull_down_en =0;
  62.     io_conf.pull_up_en = 1;  
  63.     gpio_config(&io_conf);
  64. */
  65.     //上升沿下降沿触发,按键按下led 亮,松开灯灭
  66.     io_conf.intr_type = GPIO_INTR_ANYEDGE;
  67.     io_conf.mode = GPIO_MODE_INPUT;
  68.     io_conf.pin_bit_mask = (1ULL << AN);
  69.     io_conf.pull_down_en =1;
  70.     io_conf.pull_up_en = 0;  
  71.     gpio_config(&io_conf);
  72.        
  73.     // 安装 GPIO 中断服务
  74.     gpio_install_isr_service(0);
  75.     gpio_isr_handler_add(AN,handler,NULL);
  76.     // 初始化 LED 状态为关闭
  77.     gpio_set_level(LED, 0);
  78. }
复制代码
 
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

用户国营

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表