1.快速配置引脚输出
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "esp_err.h"
#include "hal/gpio_types.h"
#define LED_PIN GPIO_NUM_1 //定义LED引脚
//初始化GPIO函数
void gpioInit(void){
//此处使用gpio_set_direction函数快速配置gpio口的方向,第一个参数为GPIO口,第二个参数为GPIO口的模式,此处选择输出模式
ESP_ERROR_CHECK(gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT));
//使用gpio_set_level函数设置LED_PIN引脚状态,0为低电平,1为高电平
ESP_ERROR_CHECK(gpio_set_level(LED_PIN, 1));
}
void app_main(void)
{
gpioInit();
}
2.结构体配置GPIO输出
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "esp_err.h"
#include "hal/gpio_types.h"
#define LED_PIN GPIO_NUM_1 //定义LED引脚
//初始化GPIO函数
void gpioInit(void){
//初始化GPIO结构体
gpio_config_t gpioInitCfg = {
.mode = GPIO_MODE_OUTPUT, //配置GPIO口为输出模式
.pin_bit_mask = 1ULL << 1, //掩码方式配置GPIO口输出,48可改为自己的GPIO
.intr_type = GPIO_INTR_DISABLE, //不使用引脚中断
.pull_down_en = GPIO_PULLDOWN_DISABLE, //不使能下拉
.pull_up_en = GPIO_PULLUP_DISABLE //不使能上拉
};
//配置GPIO
ESP_ERROR_CHECK(gpio_config(&gpioInitCfg));
//设置GPIO的电平为高电平
ESP_ERROR_CHECK(gpio_set_level(LED_PIN, 1));
}
void app_main(void)
{
gpioInit();
}
3.LED闪烁
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "esp_err.h"
#include "hal/gpio_types.h"
#include "portmacro.h"
#define LED_PIN GPIO_NUM_1 //定义LED引脚
//初始化GPIO函数
void gpioInit(void){
//初始化GPIO结构体
gpio_config_t gpioInitCfg = {
.mode = GPIO_MODE_OUTPUT, //配置GPIO口为输出模式
.pin_bit_mask = 1ULL << 1, //掩码方式配置GPIO口输出,48可改为自己的GPIO
.intr_type = GPIO_INTR_DISABLE, //不使用引脚中断
.pull_down_en = GPIO_PULLDOWN_DISABLE, //不使能下拉
.pull_up_en = GPIO_PULLUP_DISABLE //不使能上拉
};
//配置GPIO
ESP_ERROR_CHECK(gpio_config(&gpioInitCfg));
}
void ledBlink(void){
//初始化GPIO
gpioInit();
while (1) {
//设置GPIO的电平为高电平
ESP_ERROR_CHECK(gpio_set_level(LED_PIN, 1));
vTaskDelay(1000 / portTICK_PERIOD_MS); //延时1s
ESP_ERROR_CHECK(gpio_set_level(LED_PIN, 0));
vTaskDelay(1000 / portTICK_PERIOD_MS); //延时1s
}
}
void app_main(void)
{
ledBlink();
}