Article Index

Page 140

#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "driver/ledc.h"

void app_main(void)
{
    ledc_timer_config_t ledc_timer = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .timer_num = 0,
        .duty_resolution = LEDC_TIMER_13_BIT,
        .freq_hz = 4000,
        .clk_cfg = LEDC_APB_CLK};

    ledc_timer_config(&ledc_timer);

    ledc_channel_config_t ledc_channel = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .channel = 0,
        .timer_sel = 0,
        .intr_type = LEDC_INTR_DISABLE,
        .gpio_num = 2,
        .duty = 4096,
        .hpoint = 0};

    ledc_channel_config(&ledc_channel);
}

 

Page 143

#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "esp_rom_sys.h"
#include "driver/ledc.h"
#include <unistd.h>
void delay_us(int t)
{
    usleep(t);
}
void PWMconfigLow(int gpio, int chan, int timer, int res,
                  int freq, float duty)
{
    ledc_timer_config_t ledc_timer = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .clk_cfg = LEDC_APB_CLK};
    ledc_timer.timer_num = timer;
    ledc_timer.duty_resolution = res;
    ledc_timer.freq_hz = freq;

    ledc_channel_config_t ledc_channel = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .hpoint = 0,
        .intr_type = LEDC_INTR_DISABLE,
    };
    ledc_channel.channel = chan;
    ledc_channel.timer_sel = timer;
    ledc_channel.gpio_num = gpio;
    ledc_channel.duty = ((float)(2 << (res - 1))) * duty;

    ledc_timer_config(&ledc_timer);
    ledc_channel_config(&ledc_channel);
}

void changeDutyLow(int chan, int res, float duty)
{
    ledc_set_duty(LEDC_LOW_SPEED_MODE, chan,
                  ((float)(2 << (res - 1))) * duty);
    ledc_update_duty(LEDC_LOW_SPEED_MODE, chan);
}

void app_main(void)
{
    PWMconfigLow(2, 0, 3, 13, 4000, 0.25);
    while (true)
    {
        delay_us(1000);
        changeDutyLow(0, 13, 0.5);
        delay_us(1000);
        changeDutyLow(0, 13, 0.25);
    }
}

 

Page 146

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "esp_rom_sys.h"
#include <sys/time.h>
#include "driver/ledc.h"
#include "math.h"
#include <unistd.h>

void delay_us(int t)
{
    usleep(t);
}

void PWMconfigLow(int gpio, int chan, int timer, int res, int freq, float duty)
{
    ledc_timer_config_t ledc_timer = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .clk_cfg = LEDC_APB_CLK};
    ledc_timer.timer_num = timer;
    ledc_timer.duty_resolution = res;
    ledc_timer.freq_hz = freq;

    ledc_channel_config_t ledc_channel = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .hpoint = 0,
        .intr_type = LEDC_INTR_DISABLE,
    };
    ledc_channel.channel = chan;
    ledc_channel.timer_sel = timer;
    ledc_channel.gpio_num = gpio;
    ledc_channel.duty = ((float)(2 << (res - 1))) * duty;

    ledc_timer_config(&ledc_timer);
    ledc_channel_config(&ledc_channel);
}

uint8_t wave[256];
void app_main(void)
{
    for (int i = 0; i < 256; i++)
    {
        wave[i] = (uint8_t)((128.0 +
                             sinf((float)i * 2.0 * 3.14159 / 255.0) * 128.0));
    }
    int f = 60000;
    int t = (1 * 1000 * 1000) / f;
    PWMconfigLow(2, 0, 0, 8, f, 0.25);
    fflush(stdout);
    while (true)
    {
        for (int i = 0; i < 256; i++)
        {
            ledc_set_duty(LEDC_LOW_SPEED_MODE, 0, wave[i]);
            ledc_update_duty(LEDC_LOW_SPEED_MODE, 0);
            delay_us(t);
        }
    }
}

 

 Page 148

#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "esp_rom_sys.h"
#include <sys/time.h>
#include "driver/ledc.h"

void delay_ms(int t)
{
    vTaskDelay(t / portTICK_PERIOD_MS);
}

void PWMconfigLow(int gpio, int chan, int timer, int res, int freq, float duty)
{
    ledc_timer_config_t ledc_timer = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .clk_cfg = LEDC_APB_CLK};
    ledc_timer.timer_num = timer;
    ledc_timer.duty_resolution = res;
    ledc_timer.freq_hz = freq;

    ledc_channel_config_t ledc_channel = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .hpoint = 0,
        .intr_type = LEDC_INTR_DISABLE,
    };
    ledc_channel.channel = chan;
    ledc_channel.timer_sel = timer;
    ledc_channel.gpio_num = gpio;
    ledc_channel.duty = ((float)(2 << (res - 1))) * duty;

    ledc_timer_config(&ledc_timer);
    ledc_channel_config(&ledc_channel);
}

void app_main(void)
{
    while (true)
    {
        PWMconfigLow(2, 0, 0, 8, 2000, 0.5);
        for (int d = 0; d <= 256; d++)
        {
            ledc_set_duty(LEDC_LOW_SPEED_MODE, 0, d);
            ledc_update_duty(LEDC_LOW_SPEED_MODE, 0);
            delay_ms(10);
        }
    }
}

 

Page 150 complete program to do cubic dimming

#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "esp_rom_sys.h"
#include <sys/time.h>
#include "driver/ledc.h"

void delay_ms(int t)
{
    vTaskDelay(t / portTICK_PERIOD_MS);
}

void PWMconfigLow(int gpio, int chan, int timer, int res, int freq, float duty)
{
    ledc_timer_config_t ledc_timer = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .clk_cfg = LEDC_APB_CLK};
    ledc_timer.timer_num = timer;
    ledc_timer.duty_resolution = res;
    ledc_timer.freq_hz = freq;

    ledc_channel_config_t ledc_channel = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .hpoint = 0,
        .intr_type = LEDC_INTR_DISABLE,
    };
    ledc_channel.channel = chan;
    ledc_channel.timer_sel = timer;
    ledc_channel.gpio_num = gpio;
    ledc_channel.duty = ((float)(2 << (res - 1))) * duty;

    ledc_timer_config(&ledc_timer);
    ledc_channel_config(&ledc_channel);
}

void app_main(void)
{
    while (true)
    {
        PWMconfigLow(2, 0, 0, 8, 2000, 0.5);
        for (int d = 0; d <= 256; d++)
        {
            ledc_set_duty(LEDC_LOW_SPEED_MODE, 0, d * d * d / 255 / 255);
            ledc_update_duty(LEDC_LOW_SPEED_MODE, 0);
            delay_ms(10);
        }
    }
}

 

 Page 154 

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/ledc.h"

void PWMconfigLow(int gpio, int chan, int timer, int res, int freq, float duty)
{
    ledc_timer_config_t ledc_timer = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .clk_cfg = LEDC_APB_CLK};
    ledc_timer.timer_num = timer;
    ledc_timer.duty_resolution = res;
    ledc_timer.freq_hz = freq;

    ledc_channel_config_t ledc_channel = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .hpoint = 0,
        .intr_type = LEDC_INTR_DISABLE,
    };
    ledc_channel.channel = chan;
    ledc_channel.timer_sel = timer;
    ledc_channel.gpio_num = gpio;
    ledc_channel.duty = ((float)(2 << (res - 1))) * duty;
    ledc_timer.deconfigure = false;

    ledc_timer_config(&ledc_timer);
    ledc_channel_config(&ledc_channel);
}

void app_main(void)
{

    PWMconfigLow(2, 0, 0, 8, 2550, 0);
    ledc_fade_func_install(LEDC_INTR_DISABLE);
    while (true)
    {
        ledc_set_fade_with_time(LEDC_LOW_SPEED_MODE, 0, 255, 1000);
        ledc_fade_start(LEDC_LOW_SPEED_MODE, 0, LEDC_FADE_WAIT_DONE);
        ledc_set_fade_with_time(LEDC_LOW_SPEED_MODE, 0, 0, 1000);
        ledc_fade_start(LEDC_LOW_SPEED_MODE, 0, LEDC_FADE_WAIT_DONE);
    }
}

 

Page 155 complete program from fragment

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/ledc.h"

void PWMconfigLow(int gpio, int chan, int timer, int res, int freq, float duty)
{
    ledc_timer_config_t ledc_timer = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .clk_cfg = LEDC_APB_CLK};
    ledc_timer.timer_num = timer;
    ledc_timer.duty_resolution = res;
    ledc_timer.freq_hz = freq;

    ledc_channel_config_t ledc_channel = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .hpoint = 0,
        .intr_type = LEDC_INTR_DISABLE,
    };
    ledc_channel.channel = chan;
    ledc_channel.timer_sel = timer;
    ledc_channel.gpio_num = gpio;
    ledc_channel.duty = ((float)(2 << (res - 1))) * duty;
    ledc_timer.deconfigure = false;

    ledc_timer_config(&ledc_timer);
    ledc_channel_config(&ledc_channel);
}
void app_main(void)
{

    PWMconfigLow(2, 0, 0, 8, 2550, 0);
    ledc_fade_func_install(LEDC_INTR_DISABLE);
    while (true)
    {
        ledc_set_fade_with_step(LEDC_LOW_SPEED_MODE, 0, 255, 1, 10);
        ledc_fade_start(LEDC_LOW_SPEED_MODE, 0, LEDC_FADE_WAIT_DONE);
        ledc_set_fade_with_step(LEDC_LOW_SPEED_MODE, 0, 0, 1, 10);
        ledc_fade_start(LEDC_LOW_SPEED_MODE, 0, LEDC_FADE_WAIT_DONE);
    }
}

 

 Page 156

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/ledc.h"

void PWMconfigLow(int gpio, int chan, int timer, int res, int freq, float duty)
{
    ledc_timer_config_t ledc_timer = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .clk_cfg = LEDC_APB_CLK};
    ledc_timer.timer_num = timer;
    ledc_timer.duty_resolution = res;
    ledc_timer.freq_hz = freq;

    ledc_channel_config_t ledc_channel = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .hpoint = 0,
        .intr_type = LEDC_INTR_DISABLE,
    };
    ledc_channel.channel = chan;
    ledc_channel.timer_sel = timer;
    ledc_channel.gpio_num = gpio;
    ledc_channel.duty = ((float)(2 << (res - 1))) * duty;
    ledc_timer.deconfigure = false;

    ledc_timer_config(&ledc_timer);
    ledc_channel_config(&ledc_channel);
}

void changeDutyPhaseLow(int chan, int res, float duty, float phase)
{
    ledc_set_duty_with_hpoint(LEDC_LOW_SPEED_MODE, chan,
                              ((float)(2 << (res - 1))) * duty,
                              ((float)(2 << (res - 1))) * phase);
    ledc_update_duty(LEDC_LOW_SPEED_MODE, chan);
}

void app_main(void)
{
    PWMconfigLow(2, 0, 0, 8, 2550, 0.5);
    PWMconfigLow(4, 1, 0, 8, 2550, 0.5);
    changeDutyPhaseLow(1, 8, 0.5, 0.25);
}