上一章我们搭建好了开发环境,并且用串口输出了hello world, 接下来我们先点灯
参考资料: 官方入门教程: https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32/get-started/index.html#get-started-start-project
将esp-idf\examples\peripherals\gpio文件夹下的generic_gpio文件夹复制到某一个路径下(不能有空格),我的在 F:\duke_work\ESP_IDF下, 下面是我的文件夹结构:
打开一个新项目后,应首先设置“目标”芯片 idf.py set-target esp32。注意,此操作将清除并初始化项目之前的编译和配置(如有)。 您也可以直接将“目标”配置为环境变量(此时可跳过该步骤)。更多信息,请见 选择目标芯片。CMD下面输入:
F:\duke_work\ESP_IDF\esp-idf\tools\idf.py set-target esp32
idf.py的路径在esp-idf\tools下, 请将你的路径替换掉
编译命令:
F:\duke_work\ESP_IDF\esp-idf\tools\idf.py build
F:\duke_work\ESP_IDF\esp-idf\tools\idf.py -p COM9 flash
请将idf的路径替换掉, 还有串口号(COM9)也替换掉
烧录成功!!!
我的硬件是esp32-wroom-32
根据原理图可知,可编程GPIO为GPIO2, 并且为高电平触发
代码参考:gpio_example_main.c
/* GPIO Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include
#include
#include
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
/**
* Brief:
* This test code shows how to configure gpio and how to use gpio interrupt.
*
* GPIO status:
* GPIO18: output
* GPIO19: output
* GPIO4: input, pulled up, interrupt from rising edge and falling edge
* GPIO5: input, pulled up, interrupt from rising edge.
*
* Test:
* Connect GPIO18 with GPIO4
* Connect GPIO19 with GPIO5
* Generate pulses on GPIO18/19, that triggers interrupt on GPIO4/5
*
*/
#define GPIO_OUTPUT_IO_0 2
#define GPIO_OUTPUT_IO_1 19
#define GPIO_OUTPUT_PIN_SEL GPIO_SEL_2
static xQueueHandle gpio_evt_queue = NULL;
static void gpio_task_example(void* arg)
{
uint32_t io_num;
for(;;) {
printf("turn off led0!\r\n");
gpio_set_level(GPIO_OUTPUT_IO_0, 0);
vTaskDelay(1000 / portTICK_RATE_MS);
printf("turn on led0!\r\n");
gpio_set_level(GPIO_OUTPUT_IO_0, 1);
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
void app_main(void)
{
gpio_config_t io_conf;
//disable interrupt
io_conf.intr_type = GPIO_INTR_DISABLE;
//set as output mode
io_conf.mode = GPIO_MODE_OUTPUT;
//bit mask of the pins that you want to set,e.g.GPIO18/19
io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
//disable pull-down mode
io_conf.pull_down_en = 0;
//disable pull-up mode
io_conf.pull_up_en = 0;
//configure GPIO with the given settings
gpio_config(&io_conf);
//start gpio task
xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);
printf("Minimum free heap size: %d bytes\n", esp_get_minimum_free_heap_size());
int cnt = 0;
while(1) {
printf("cnt: %d\n", cnt++);
vTaskDelay(1000 / portTICK_RATE_MS);
}
}