There are several questions I have in mind
The ws2812s control uses the PWM data to control the rgb values. Can it be possible to switch(0 or 1) the duty cycle of PWM so fast that it can meet the timing requirements for the rgb leds?
I tried to use the software method to change the duty cycle. but it seems that there are issues.
https://docs.espressif.com/projects/esp ... re-channel
The following is the code example I am using. The bad part is that I am using for() loop for sending PWM bits, which could increase the overhead and send totally wrong values. How to deal with this?
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "driver/ledc.h"
#include "esp_log.h"
#define DUTY_1 (5.6)
#define DUTY_0 (2.8)
static const char TAG[] = "main";
void app_main(void){
esp_err_t ret;
ESP_LOGI(TAG, "Initializing the example");
ledc_timer_config_t config = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.timer_num = LEDC_TIMER_0,
.duty_resolution = LEDC_TIMER_3_BIT,
.freq_hz = 800000,
.clk_cfg = LEDC_AUTO_CLK,
};
ESP_ERROR_CHECK(ledc_timer_config(&config));
ledc_channel_config_t cfconfig = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = 5,
.duty = 0,
.hpoint = 0
};
ESP_ERROR_CHECK(ledc_channel_config(&cfconfig));
//red color
for(uint8_t i=0 ; i<=7 ; i++){
ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, DUTY_0));
ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0));
}
for(uint8_t i=0 ; i<=7 ; i++){
ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, DUTY_0));
ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0));
}
for(uint8_t i=0 ; i<=7 ; i++){
ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, DUTY_1));
ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0));
}