1.接ESP32创建工程01,快捷键Ctrl+Shift+p
2.输入组件的名称,然后按Enter键
3.创建结果如下图所示
目录结构如下
一级 component --文件夹
二级 led ---文件夹
led.c c文件
CMakeList.txt txt文件
三级 include 文件夹
led.h c头文件
4.主要代码
//led.c
#include <stdio.h>
#include "led.h"
#include "driver/gpio.h"
#define GPIO_5 5
void ledInit(void)
{
gpio_config_t mgpio_config;
mgpio_config.intr_type=GPIO_INTR_DISABLE;
mgpio_config.mode=GPIO_MODE_INPUT_OUTPUT;
mgpio_config.pin_bit_mask=(unsigned long)1<<GPIO_5;
mgpio_config.pull_down_en=GPIO_PULLDOWN_DISABLE;
mgpio_config.pull_up_en=GPIO_PULLUP_DISABLE;
gpio_config(&mgpio_config);
}
void ledSet(void)
{
gpio_set_level(GPIO_5,1);
}
void ledReset(void)
{
gpio_set_level(GPIO_5,0);
}
//led.h
#ifndef __LED_H
#define __LED_H
void ledInit(void);
void ledSet(void);
void ledReset(void);
#endif
//CMakeList.txt
idf_component_register(SRCS "led.c"
INCLUDE_DIRS "include"
REQUIRES driver)
//main.c
#include <stdio.h>
#include "led.h"
void app_main(void)
{
int i=0;
ledInit();
ledSet();
for(i=0;i<10;i++)
{
printf("hello world\r\n");
}
while(1);
}
//CMakeList.txt
idf_component_register(SRCS "main.c"
INCLUDE_DIRS ".")
//CMakeList.txt
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(Day0HelloWorld)
5.运行结果
本文ESP-IDF版本为 v5.2.2