文章目錄
- 一、快速運行
- 二、運行效果
- 三、硬體說明
- 四、RMT簡介
- 五、例程說明
- 六、優化與效果
- 七、相關函式說明
- 7.1 rmt 相關
- 7.2 led strip 相關
- 八、參考
一、快速運行
- 示例專案中,選擇
peripherals—>rmt—>led_strip menuconfig配置ESP32C3-Specific—>Rec 0- 芯片選擇
ESP32-C3(Built-in USB JTAG) - 快速運行
ESP-IDF Build, Flash and Monitor(左下角)
二、運行效果
ESP32-C3入門教程 基礎篇⑧——WS2812 全彩RGB LED燈帶2
三、硬體說明
從硬體原理圖中可以看出來
RGB LED是通過GPIO8來控制- 而
RGB LED是SK68XXMINI-HS - 大部分人對
SK68XXMINI不熟悉,但是對WS2812熟悉 - 查一下資料就曉得,
SK68XXMINI和WS2812其實是一個東西,控制原理是一樣的 - SK68XXMINI資料
- WS2812資料,WS2812資料2




四、RMT簡介
RMT 是 Remote Control 的簡稱,由于RMT模塊的靈活性,驅動器還可用于生成或接收許多其他型別的信號,
WS2812是一個數字RGB LED,其定義的協議資料格式與RMT外圍設備中的協議資料格式兼容,
- RMT發送

- RMT接收

五、例程說明
- RMT配置
rmt_configrmt_mode = RMT_MODE_TX輸出模式channel通道號gpio_numGPIO口- 其他
- RMT驅動安裝
rmt_driver_installchannel通道號rx_buf_size = 0接收快取大小intr_alloc_flags = 0
- led_strip配置
LED_STRIP_DEFAULT_CONFIGmax_leds最大LED個數dev設備手柄,即RMT通道號的地址
- led_strip實體化
led_strip_new_rmt_ws2812led_strip_config_tled_strip配置引數
- led_strip實體化后有多個方法
esp_err_t (*set_pixel)(led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue);為特定像素設定RGBesp_err_t (*refresh)(led_strip_t *strip, uint32_t timeout_ms);重繪LEDs顏色esp_err_t (*clear)(led_strip_t *strip, uint32_t timeout_ms);關閉所有LEDsesp_err_t (*del)(led_strip_t *strip);釋放LEDs所有資源
- while回圈
- hue從0到360的階梯遞增
- hue轉rgb
strip->set_pixel(strip, j, red, green, blue)設定RGB進去到第j個LEDstrip->refresh(strip, 100)重繪LED的顏色- 延時
strip->clear(strip, 50);關閉所有LEDs- 延時
- 回圈……
void app_main(void)
{
uint32_t red = 0;
uint32_t green = 0;
uint32_t blue = 0;
uint16_t hue = 0;
uint16_t start_rgb = 0;
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(CONFIG_EXAMPLE_RMT_TX_GPIO, RMT_TX_CHANNEL);
// set counter clock to 40MHz
config.clk_div = 2;
ESP_ERROR_CHECK(rmt_config(&config));
ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));
// install ws2812 driver
led_strip_config_t strip_config = LED_STRIP_DEFAULT_CONFIG(CONFIG_EXAMPLE_STRIP_LED_NUMBER, (led_strip_dev_t)config.channel);
led_strip_t *strip = led_strip_new_rmt_ws2812(&strip_config);
if (!strip) {
ESP_LOGE(TAG, "install WS2812 driver failed");
}
// Clear LED strip (turn off all LEDs)
ESP_ERROR_CHECK(strip->clear(strip, 100));
// Show simple rainbow chasing pattern
ESP_LOGI(TAG, "LED Rainbow Chase Start");
while (true) {
for (int i = 0; i < 3; i++) {
for (int j = i; j < CONFIG_EXAMPLE_STRIP_LED_NUMBER; j += 3) {
// Build RGB values
hue = j * 360 / CONFIG_EXAMPLE_STRIP_LED_NUMBER + start_rgb;
led_strip_hsv2rgb(hue, 100, 100, &red, &green, &blue);
// Write RGB values to strip driver
ESP_ERROR_CHECK(strip->set_pixel(strip, j, red, green, blue));
}
// Flush RGB values to LEDs
ESP_ERROR_CHECK(strip->refresh(strip, 100));
vTaskDelay(pdMS_TO_TICKS(EXAMPLE_CHASE_SPEED_MS));
strip->clear(strip, 50);
vTaskDelay(pdMS_TO_TICKS(EXAMPLE_CHASE_SPEED_MS));
}
start_rgb += 60;
}
}
六、優化與效果
我感覺這個效果在我這個開發板上面效果不咋地,所以我做了如下優化
- 首先,RMT TX的GPIO在開發板上面是GPIO8,所以,
#define EXAMPLE_RMT_TX_GPIO 8 - 其次,RGB LED我就一個,所以,
#define EXAMPLE_STRIP_LED_NUMBER 1 - 最后,hue遞增,再轉rgb的方式,太騷了,不適合我,我直接紅綠藍RGB三種顏色每秒流轉即可
改動后效果如下:
ESP32-C3入門教程 基礎篇⑧——WS2812 全彩RGB LED燈帶3
改動代碼如下:
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/rmt.h"
#include "led_strip.h"
static const char *TAG = "example";
#define RMT_TX_CHANNEL RMT_CHANNEL_0
#define EXAMPLE_CHASE_SPEED_MS (1000)
#define EXAMPLE_RMT_TX_GPIO 8
#define EXAMPLE_STRIP_LED_NUMBER 1
void app_main(void)
{
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(EXAMPLE_RMT_TX_GPIO, RMT_TX_CHANNEL);
// set counter clock to 40MHz
config.clk_div = 2;
ESP_ERROR_CHECK(rmt_config(&config));
ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));
// install ws2812 driver
led_strip_config_t strip_config = LED_STRIP_DEFAULT_CONFIG(EXAMPLE_STRIP_LED_NUMBER, (led_strip_dev_t)config.channel);
led_strip_t *strip = led_strip_new_rmt_ws2812(&strip_config);
if (!strip) {
ESP_LOGE(TAG, "install WS2812 driver failed");
}
// Clear LED strip (turn off all LEDs)
ESP_ERROR_CHECK(strip->clear(strip, 100));
// Show simple rainbow chasing pattern
ESP_LOGI(TAG, "LED Rainbow Chase Start");
while (true) {
strip->set_pixel(strip, 0, 255, 0, 0);
strip->refresh(strip, 50);
vTaskDelay(pdMS_TO_TICKS(EXAMPLE_CHASE_SPEED_MS));
strip->set_pixel(strip, 0, 0, 255, 0);
strip->refresh(strip, 50);
vTaskDelay(pdMS_TO_TICKS(EXAMPLE_CHASE_SPEED_MS));
strip->set_pixel(strip, 0, 0, 0, 255);
strip->refresh(strip, 50);
vTaskDelay(pdMS_TO_TICKS(EXAMPLE_CHASE_SPEED_MS));
}
}
七、相關函式說明
7.1 rmt 相關
\esp-idf\components\driver\include\driver\rmt.h
/**
* @brief Data struct of RMT configure parameters
*/
typedef struct {
rmt_mode_t rmt_mode; /*!< RMT mode: transmitter or receiver */
rmt_channel_t channel; /*!< RMT channel */
gpio_num_t gpio_num; /*!< RMT GPIO number */
uint8_t clk_div; /*!< RMT channel counter divider */
uint8_t mem_block_num; /*!< RMT memory block number */
uint32_t flags; /*!< RMT channel extra configurations, OR'd with RMT_CHANNEL_FLAGS_[*] */
union {
rmt_tx_config_t tx_config; /*!< RMT TX parameter */
rmt_rx_config_t rx_config; /*!< RMT RX parameter */
};
} rmt_config_t;
/**
* @brief Default configuration for Tx channel
*
*/
#define RMT_DEFAULT_CONFIG_TX(gpio, channel_id) \
{ \
.rmt_mode = RMT_MODE_TX, \
.channel = channel_id, \
.gpio_num = gpio, \
.clk_div = 80, \
.mem_block_num = 1, \
.flags = 0, \
.tx_config = { \
.carrier_freq_hz = 38000, \
.carrier_level = RMT_CARRIER_LEVEL_HIGH, \
.idle_level = RMT_IDLE_LEVEL_LOW, \
.carrier_duty_percent = 33, \
.carrier_en = false, \
.loop_en = false, \
.idle_output_en = true, \
} \
}
components\driver\rmt.c
esp_err_t rmt_config(const rmt_config_t *rmt_param)
esp_err_t rmt_driver_install(rmt_channel_t channel, size_t rx_buf_size, int intr_alloc_flags)
7.2 led strip 相關
\components\led_strip\include\led_strip.h
/**
* @brief LED Strip Configuration Type
*
*/
typedef struct {
uint32_t max_leds; /*!< Maximum LEDs in a single strip */
led_strip_dev_t dev; /*!< LED strip device (e.g. RMT channel, PWM channel, etc) */
} led_strip_config_t;
/**
* @brief Declare of LED Strip Type
*
*/
struct led_strip_s {
/**
* @brief Set RGB for a specific pixel
*
* @param strip: LED strip
* @param index: index of pixel to set
* @param red: red part of color
* @param green: green part of color
* @param blue: blue part of color
*
* @return
* - ESP_OK: Set RGB for a specific pixel successfully
* - ESP_ERR_INVALID_ARG: Set RGB for a specific pixel failed because of invalid parameters
* - ESP_FAIL: Set RGB for a specific pixel failed because other error occurred
*/
esp_err_t (*set_pixel)(led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue);
/**
* @brief Refresh memory colors to LEDs
*
* @param strip: LED strip
* @param timeout_ms: timeout value for refreshing task
*
* @return
* - ESP_OK: Refresh successfully
* - ESP_ERR_TIMEOUT: Refresh failed because of timeout
* - ESP_FAIL: Refresh failed because some other error occurred
*
* @note:
* After updating the LED colors in the memory, a following invocation of this API is needed to flush colors to strip.
*/
esp_err_t (*refresh)(led_strip_t *strip, uint32_t timeout_ms);
/**
* @brief Clear LED strip (turn off all LEDs)
*
* @param strip: LED strip
* @param timeout_ms: timeout value for clearing task
*
* @return
* - ESP_OK: Clear LEDs successfully
* - ESP_ERR_TIMEOUT: Clear LEDs failed because of timeout
* - ESP_FAIL: Clear LEDs failed because some other error occurred
*/
esp_err_t (*clear)(led_strip_t *strip, uint32_t timeout_ms);
/**
* @brief Free LED strip resources
*
* @param strip: LED strip
*
* @return
* - ESP_OK: Free resources successfully
* - ESP_FAIL: Free resources failed because error occurred
*/
esp_err_t (*del)(led_strip_t *strip);
};
八、參考
Remote Control (RMT) - ESP32-C2 - —— ESP-IDF編程指南
覺得好,就一鍵三連唄(點贊+收藏+關注)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/300815.html
標籤:其他
