esp8266 gpio 控制(led与key使用)
现在介绍上个工程添加led控制与key控制,必须学会如何控制led,和key,我们才能把他应用到我们的想做的项目中去。。
关系到gpio,则需要看硬件原理图:
由上图可知,我们需要想操作led或者key,只需要配置gpio 0 ,gpio1即可。
现在先创建一个gpio 处理任务
void app_main(void)
{//modify cdb 2019-12-19// printf("SDK version:%s\n", esp_get_idf_version());// Initialize NVSesp_err_t ret = nvs_flash_init();int i=0;if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {ESP_ERROR_CHECK(nvs_flash_erase());ret = nvs_flash_init();}ESP_ERROR_CHECK(ret);//初始化wifi,并且联网initialise_wifi();//创建mqtt 处理线程ret = xTaskCreate(&mqtt_client_thread,MQTT_CLIENT_THREAD_NAME,MQTT_CLIENT_THREAD_STACK_WORDS,NULL,MQTT_CLIENT_THREAD_PRIO,NULL);if (ret != pdPASS) {DBG_C("mqtt create client thread %s failed\n", MQTT_CLIENT_THREAD_NAME);}//创建gpio led key 处理任务ret = xTaskCreate(&gpio_task,GPIO_THREAD_NAME,GPIO_THREAD_STACK_WORDS,NULL,GPIO_THREAD_PRIO,NULL);if (ret != pdPASS) {DBG_C("qpio thread %s failed\n", GPIO_THREAD_NAME);}//程序将进入死循环,防止退出,并且打印运行时间log,单位s,也开启rtos任务调度while(1){DBG_C("the system is runing,runtime:%d\n",i++);vTaskDelay(1000 / portTICK_RATE_MS);}}
同样的再component目录新建gpio文件,些user_gpio.c 和user_gpio.h
下面为代码
user_gpio.c
/* gpio exampleThis example code is in the Public Domain (or CC0 licensed, at your option.)Unless required by applicable law or agreed to in writing, thissoftware is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES ORCONDITIONS OF ANY KIND, either express or implied.
*/#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"#include "esp_log.h"
#include "esp_system.h"#define DBG_C(...) printf("[%s,%d]",__FUNCTION__,__LINE__);printf(__VA_ARGS__)/*** Brief:* This test code shows how to configure gpio and how to use gpio interrupt.** GPIO status:* GPIO15: output* GPIO16: output* GPIO4: input, pulled up, interrupt from rising edge and falling edge* GPIO5: input, pulled up, interrupt from rising edge.** Test:* Connect GPIO15 with GPIO4* Connect GPIO16 with GPIO5* Generate pulses on GPIO15/16, that triggers interrupt on GPIO4/5**/#define GPIO_OUTPUT_IO_0 4 //led gpio 4
//#define GPIO_OUTPUT_IO_1 5
#define GPIO_OUTPUT_PIN_SEL (1ULL<<GPIO_OUTPUT_IO_0)
#define GPIO_INPUT_IO_0 0 //key gpio 0
//#define GPIO_INPUT_IO_1 5
#define GPIO_INPUT_PIN_SEL (1ULL<<GPIO_INPUT_IO_0) static xQueueHandle gpio_evt_queue = NULL;static void gpio_isr_handler(void *arg)
{uint32_t gpio_num = (uint32_t) arg;xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
}static void gpio_task_example(void *arg)
{uint32_t io_num;for (;;) {if (xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {DBG_C("---------------GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num));}}
}void gpio_task(void)
{//配置gpio 4 ledgpio_config_t io_conf;//关闭中断io_conf.intr_type = GPIO_INTR_DISABLE;//设置为输出模式(led)io_conf.mode = GPIO_MODE_OUTPUT;//bit mask of the pins that you want to set,e.g.GPIO15/16io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;//disable pull-down modeio_conf.pull_down_en = 0;//disable pull-up modeio_conf.pull_up_en = 0;//设置gpio4gpio_config(&io_conf);//配置gpio 0 key boot按键//开启中断(key)io_conf.intr_type = GPIO_INTR_POSEDGE;//bit mask of the pins, use GPIO4/5 hereio_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;//设置为输入模式io_conf.mode = GPIO_MODE_INPUT;//enable pull-up modeio_conf.pull_up_en = 1;//设置gpio 0gpio_config(&io_conf);//change gpio intrrupt type for one pingpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE);//设置中断触发方式(上升沿或者下降沿或者两种,本例中两种)gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));//start gpio taskxTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);//install gpio isr servicegpio_install_isr_service(0);//hook isr handler for specific gpio pingpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void *) GPIO_INPUT_IO_0);//hook isr handler for specific gpio pin//gpio_isr_handler_add(GPIO_INPUT_IO_1, gpio_isr_handler, (void *) GPIO_INPUT_IO_1);//remove isr handler for gpio number.gpio_isr_handler_remove(GPIO_INPUT_IO_0);//hook isr handler for specific gpio pin againgpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void *) GPIO_INPUT_IO_0);int cnt = 0;//led闪烁操作while (1) {DBG_C("cnt: %d\n", cnt++);vTaskDelay(1000 / portTICK_RATE_MS);gpio_set_level(GPIO_OUTPUT_IO_0, cnt % 2);if(cnt%2)printf("led off\n");elseprintf("led on\n");// gpio_set_level(GPIO_OUTPUT_IO_1, cnt % 2);}
}
user_gpio.h
#ifndef _ESP8266_MY_GPIO_H_
#define _ESP8266_MY_GPIO_H_void gpio_task(void);#endif
修改component.mk
#
# Component Makefile
#COMPONENT_SRCDIRS := mqtt gpio
COMPONENT_ADD_INCLUDEDIRS += mqtt/include
COMPONENT_ADD_INCLUDEDIRS += gpio/include
编译
make clean
make
烧录运行
按键能够触发终端打log,开发板led也再1s闪烁。