当前位置: 首页 > news >正文

ESP32C3 系列实战(1) --点亮小灯

文章目录

    • 准备工作
      • 参考文档
      • 工具
      • 安装 ESP-IDF
      • ESP32-C3 开发板
    • 项目结构
      • ./CMakeLists.txt
      • ./sdkconfig.defaults
      • ./main/Kconfig.projbuild
      • ./main/CMakeLists.txt
      • ./main/blink.c
    • 消除波浪线引用报错
    • 编译烧录
    • 程序运行结果

准备工作

参考文档

  1. 乐鑫官网文档参考 https://docs.espressif.com/projects/esp-dev-kits/zh_CN/latest/esp32c3/resources.html

  2. 合宙ESP32C3开发板文档参考 https://wiki.luatos.com/chips/esp32c3/board.html

  3. ESP-IDF 官网文档 https://docs.espressif.com/projects/esp-idf/zh_CN/stable/esp32c3/get-started/index.html

工具

  1. CH343的驱动 https://www.wch.cn/downloads/CH343SER_EXE.html

  2. IDE 开发工具:vscode + esp-idf 插件 + c/c++ 插件 + cmake 插件

安装 ESP-IDF

esp-idf 安装教程参考 ESP-IDF 官网文档 https://docs.espressif.com/projects/esp-idf/zh_CN/stable/esp32c3/get-started/index.html,稍微有点小麻烦,网上已有很多教程,例如: https://naiva.blog.csdn.net/article/details/142030279, 此处就不再赘述。

ESP32-C3 开发板

我买的是合宙ESP32-C3 开发板,文档在 ESP-IDF 官网文档,

准备好了上述的工作,就开始进入激动人心的编程了,点亮一个 LED 灯!

项目结构

主要用到的文件如下:

.
├── CMakeLists.txt
├── main
│   ├── CMakeLists.txt
│   ├── Kconfig.projbuild
│   └── blink.c
└── sdkconfig.defaults

./CMakeLists.txt

声明环境

# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(blink)

./sdkconfig.defaults

空文件,保持默认,编译的时候会生成 sdkconfig 文件,用的就是默认的参数

#

./main/Kconfig.projbuild

project configuration menu ,idf.py menuconfig 用到, 比如这里设置端口为GPIO12, 会根据此配置生成 sdkconfig 文件,里面就有 #define CONFIG_BLINK_GPIO 12

menu "Example Configuration"config BLINK_GPIOint "Blink GPIO number"range 0 21default 12helpGPIO number (IOxx) to blink on and off.Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to blink.endmenu

./main/CMakeLists.txt

定义 src 目录 与 include 目录

# 定义 src 目录 与 include 目录
set(srcs "blink.c")
set(include_dirs ".")
idf_component_register(SRCS "${srcs}" INCLUDE_DIRS "${include_dirs}")

./main/blink.c

点亮小灯主程序

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "sdkconfig.h"/* Can use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,or you can edit the following line and set a number here.
*/
#define BLINK_GPIO CONFIG_BLINK_GPIOstatic const char *TAG = "blink";void app_main(void)
{int i = 0;ESP_LOGE(TAG, "app_main");/* Configure the IOMUX register for pad BLINK_GPIO (some pads aremuxed to GPIO on reset already, but some default to otherfunctions and need to be switched to GPIO. Consult theTechnical Reference for a list of pads and their defaultfunctions.)*/gpio_reset_pin(BLINK_GPIO);/* Set the GPIO as a push/pull output */gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);while(1) {ESP_LOGI(TAG, "[%02d] Hello world!", i++);/* Blink off (output low) */printf("Turning off the LED\n");gpio_set_level(BLINK_GPIO, 0);vTaskDelay(1000 / portTICK_PERIOD_MS);ESP_LOGI(TAG, "[%02d] Hello world!", i++);/* Blink on (output high) */printf("Turning on the LED\n");gpio_set_level(BLINK_GPIO, 1);vTaskDelay(1000 / portTICK_PERIOD_MS);}
}

程序刚开始有很多报错,因为引用的是 ESP-IDF sdk 中的文件,因此要将其加入进path
在这里插入图片描述

消除波浪线引用报错

先删除.vscode 文件夹,用ESP-IDF 插件添加 vscode 配置文件夹,会增加几个文件,会把 esp-idf 的路径给添加进去

.vscode
├── c_cpp_properties.json
├── launch.json
└── settings.json

在这里插入图片描述

c_cpp_properties.json:

{"configurations": [{"name": "ESP-IDF","compilerPath": "${config:idf.toolsPath}/tools/riscv32-esp-elf/esp-14.2.0_20241119/riscv32-esp-elf/bin/riscv32-esp-elf-gcc","compileCommands": "${config:idf.buildPath}/compile_commands.json","includePath": ["${config:idf.espIdfPath}/components/**","${config:idf.espIdfPathWin}/components/**","${workspaceFolder}/**"],"browse": {"path": ["${config:idf.espIdfPath}/components","${config:idf.espIdfPathWin}/components","${workspaceFolder}"],"limitSymbolsToIncludedHeaders": true}}],"version": 4
}
{"version": "0.2.0","configurations": [{"type": "gdbtarget","request": "attach","name": "Eclipse CDT GDB Adapter"},{"type": "espidf","name": "Launch","request": "launch"}]
}

settings.json
其中 idf.port 会把设备连接的端口记录进去,为后面的烧录准备

{"C_Cpp.intelliSenseEngine": "default","idf.port": "/dev/tty.wchusbserial59090649341","files.associations": {".env*": "dotenv","freertos.h": "c","task.h": "c"},"idf.openOcdConfigs": ["board/esp32c3-builtin.cfg"],"idf.customExtraVars": {"IDF_TARGET": "esp32c3"}
}

编译烧录

把配置分别按实际情况配置上,点编译,会生成 build 目录, 然后再点烧录,程序就烧录进开发板中了。
在这里插入图片描述

程序运行结果

这里就成功点亮 LED 小灯了

ESP32C3 点亮小灯

http://www.xdnf.cn/news/1411993.html

相关文章:

  • Wi-Fi技术——物理层技术
  • 使用Cadence工具完成数模混合设计流程简介
  • LangChain核心抽象:Runnable接口深度解析
  • leetcode_48 旋转图像
  • FFMPEG学习任务
  • 第 14 篇:K-Means与聚类思维——当AI在没有“标准答案”的世界里寻宝
  • 【C2000】C2000的硬件设计指导与几点意见
  • 开源知识抽取框架 推荐
  • 京东获取商品评论指南,实时关注用户反馈
  • 官方 API 与网络爬虫的技术特性对比及选型分析
  • Unity学习----【数据持久化】二进制存储(三)--文件夹操作
  • OpenStack 01:介绍
  • 暄桐林曦老师关于静坐常见问题的QA
  • 基于GA遗传优化的双向LSTM融合多头注意力(BiLSTM-MATT)时间序列预测算法matlab仿真
  • windows系统中的docker,xinference直接运行在容器目录和持载在宿主机目录中的区别
  • isat将标签转化为labelme格式后,labelme打不开的解决方案
  • MyBatis 黑马 辅助配置,数据库连接池
  • 柔性数组与不定长数据
  • 【秋招笔试】2025.08.31饿了么秋招笔试题
  • SPMTE 2022概述
  • 线程池常见面试问答
  • 一次解决 Elasticsearch 两大难题: 掌握去重和深分页的最佳实践
  • Day19_【机器学习—线性回归 (1)】
  • PerfectSquares.java
  • c++程序员日常超实用工具(长期记录更新)
  • 疯狂星期四文案网第56天运营日记
  • 创意无界:云渲染如何让视觉创作触手可及
  • python如何下载svg图片
  • 【LeetCode - 每日1题】解数独
  • 虚幻引擎技术开放日!facecar分享3D HMI设计与UE开发经验