Blink示例
本節以Blink示例程序爲例,一步步地完成一個簡單的ESP-IDF示例程序編譯。需要注意的是,本節是通過GPIO的高/低電平來驅動LED的。對於WS2812指示燈,需要使用特殊通信協議,請參考 esp-idf/examples/peripherals/rmt/led_strip 中的示例程序。
TIPS
WS2812是一顆數字LED燈珠,採用單總線通訊,每顆燈珠支持24bit的顏色控制,也即RGB888,信號線通過DIN輸入,經過一顆燈珠之後,信號線上前24bit數據會被該燈珠鎖存,之後將剩下的數據信號整形之後通過DOUT輸出
WS2812B RGB彩燈相關資料
實驗最終效果如下圖所示:
材料準備
材料 | 數量 |
---|---|
ESP32-C3 開發板(這裏使用的型號是:ESP32-C3-DevKitM-1) | 1個 |
LED燈 | 1個 |
杜邦線(母對母) | 2條 |
硬件連線
按照下圖進行連線:
-
LED燈的正極(針腳比較長的一腳爲正極)連接ESP32-C3 開發板的 8 針腳;
-
LED燈的負極連接ESP32-C3 開發板的 GND 針腳;
-
最後使用 USB 線將 ESP32-C3 開發板連接到 PC。
創建項目
首先,打開 VS Code 從模板項目創建 Blink 項目,按 F1 或選擇頂部菜單【查看/命令面板(Ctrl + Shift + P)】,在命令行中輸入:esp-idf show,在顯示的下來菜單中選擇【展示示例項目】
然後選擇當前的 ESP-IDF 開發環境,如下圖所示:
然後選擇【blink】示例,如下圖所示進行操作:
然後選擇項目的保存地址,切記項目路徑不能包含中文,這樣就創建了 blink 項目了,如下圖所示:
程序入口
main/blink.c
/* Blink 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 <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.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_GPIO
void app_main(void)
{
/* Configure the IOMUX register for pad BLINK_GPIO (some pads are
muxed to GPIO on reset already, but some default to other
functions and need to be switched to GPIO. Consult the
Technical Reference for a list of pads and their default
functions.)
*/
gpio_reset_pin(BLINK_GPIO);
/* Set the GPIO as a push/pull output */
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
while(1) {
/* Blink off (output low) */
printf("Turning off the LED\n");
gpio_set_level(BLINK_GPIO, 0);
vTaskDelay(1000 / portTICK_PERIOD_MS);
/* Blink on (output high) */
printf("Turning on the LED\n");
gpio_set_level(BLINK_GPIO, 1);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
打開 idf.py 前端
對於Windows系統,可以直接在程序列表查找並打開 ESP-IDF 4.3 CMD 或 ESP-IDF 4.3 PowerShell,終端打開後會自動添加環境變量,執行如下命令,切換到項目根目錄:
> cd F:\05-workspace\dev\01-lab\esp32\book-esp32-c3-guide\src\ch04\blink
設置目標設備
使用如下命令將編譯目標設置爲 ESP32-C3 ,
idf.py set-target esp32c3
這時將自動生成配置文件 sdkconfig
配置 GPIO
接着使用命令
idf.py menuconfig
彈出配置界面:
進入選項配置界面,按向上/向下按鍵和Enter按鍵進入Example Configuration,選擇【Blink GPIO number】,輸入數字將GPIO改爲指定引腳,如下圖所示,按照提示進行保存即可。
保存後,按【ESC】退出。
TIPS
出現這個配置項,其實因爲文件 main/Kconfig.projbuild 中設置了這個選項
sdkconfig 是由編譯系統自動生成的,可通過命令 idf.py menuconfig 進行修改並重新生成。menuconfig中的選項主要從工程的 Kconfig.projbuild 以及組件的 Kconfig > 導入,組件的開發者一般通過在 Kconfig 中添加配置項,使組件具有靈活可配置的特性。main/Kconfig.projbuild 文件的內容如下:
menu "Example Configuration" config BLINK_GPIO int "Blink GPIO number" range 0 34 default 5 help GPIO number (IOxx) to blink on and off. Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to blink. GPIOs 35-39 are input-only so cannot be used as outputs. endmenu
這時配置文件 sdkconfig的這個配置現在已經修改爲8,
CONFIG_BLINK_GPIO=8
同時產生了一箇舊的配置文件 sdkconfig.old
選擇串口
使用 USB 線將 ESP32 開發板連接到電腦後,然後在 PC 桌面右鍵單擊【此電腦】圖標,選擇【管理】菜單項,在【計算機管理】中可以查看連接端口,如下圖所示:
每個人的串口可能不同,我這裏是 COM7, 請記住串口名,您會在後續步驟中使用。
編譯工程
然後依次如下命令進行編譯
idf.py build # 編譯工程
燒錄與監視輸出
然後依次如下命令進行燒錄與監視輸出
idf.py -p COM7 flash monitor # 燒錄到設備並監視輸出
結果
LED 燈開始閃爍,如下圖所示: