Updated example and descritpion of LED PWM Controller API

This commit is contained in:
krzychb
2017-09-18 07:08:01 +02:00
parent de750e9921
commit d60722c33d
4 changed files with 279 additions and 101 deletions

View File

@@ -1,4 +1,4 @@
/* Ledc fade example
/* LEDC (LED Controller) fade example
This example code is in the Public Domain (or CC0 licensed, at your option.)
@@ -9,25 +9,28 @@
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/xtensa_api.h"
#include "freertos/queue.h"
#include "driver/ledc.h"
#include "esp_attr.h"
#include "esp_err.h"
/*
* About this example
* 1. init LEDC module:
* a. You need to set the timer of LEDC first, this decide the frequency and resolution of PWM.
* b. You need to set the LEDC channel you want to use, and bind with one of the timers.
*
* 2. You can install a default fade function, then you can use fade APIs.
* 1. Start with initializing LEDC module:
* a. Set the timer of LEDC first, this determines the frequency
* and resolution of PWM.
* b. Then set the LEDC channel you want to use,
* and bind with one of the timers.
*
* 2. You need first to install a default fade function,
* then you can use fade APIs.
*
* 3. You can also set a target duty directly without fading.
*
* 4. This example use GPIO18/19/4/5 as LEDC ouput, and it will change the duty repeatedly.
* 4. This example uses GPIO18/19/4/5 as LEDC output,
* and it will change the duty repeatedly.
*
* 5. GPIO18/19 are from high speed channel group. GPIO4/5 are from low speed channel group.
* 5. GPIO18/19 are from high speed channel group.
* GPIO4/5 are from low speed channel group.
*
*/
#define LEDC_HS_TIMER LEDC_TIMER_0
@@ -45,105 +48,113 @@
#define LEDC_LS_CH3_CHANNEL LEDC_CHANNEL_3
#define LEDC_TEST_CH_NUM (4)
typedef struct {
int channel;
int io;
int mode;
int timer_idx;
} ledc_info_t;
#define LEDC_TEST_DUTY (4000)
#define LEDC_TEST_FADE_TIME (3000)
void app_main()
{
int ch;
ledc_info_t ledc_ch[LEDC_TEST_CH_NUM] = {
{
.channel = LEDC_HS_CH0_CHANNEL,
.io = LEDC_HS_CH0_GPIO,
.mode = LEDC_HS_MODE,
.timer_idx = LEDC_HS_TIMER
},
{
.channel = LEDC_HS_CH1_CHANNEL,
.io = LEDC_HS_CH1_GPIO,
.mode = LEDC_HS_MODE,
.timer_idx = LEDC_HS_TIMER
},
{
.channel = LEDC_LS_CH2_CHANNEL,
.io = LEDC_LS_CH2_GPIO,
.mode = LEDC_LS_MODE,
.timer_idx = LEDC_LS_TIMER
},
{
.channel = LEDC_LS_CH3_CHANNEL,
.io = LEDC_LS_CH3_GPIO,
.mode = LEDC_LS_MODE,
.timer_idx = LEDC_LS_TIMER
}
};
/*
* Prepare and set configuration of timers
* that will be used by LED Controller
*/
ledc_timer_config_t ledc_timer = {
.bit_num = LEDC_TIMER_13_BIT, //set timer counter bit number
.freq_hz = 5000, //set frequency of pwm
.speed_mode = LEDC_HS_MODE, //timer mode,
.timer_num = LEDC_HS_TIMER //timer index
.bit_num = LEDC_TIMER_13_BIT, // resolution of PWM duty
.freq_hz = 5000, // frequency of PWM signal
.speed_mode = LEDC_HS_MODE, // timer mode
.timer_num = LEDC_HS_TIMER // timer index
};
//configure timer0 for high speed channels
// Set configuration of timer0 for high speed channels
ledc_timer_config(&ledc_timer);
//configure timer1 for low speed channels
// Prepare and set configuration of timer1 for low speed channels
ledc_timer.speed_mode = LEDC_LS_MODE;
ledc_timer.timer_num = LEDC_LS_TIMER;
ledc_timer_config(&ledc_timer);
/*
* Prepare individual configuration
* for each channel of LED Controller
* by selecting:
* - controller's channel number
* - output duty cycle, set initially to 0
* - GPIO number where LED is connected to
* - speed mode, either high or low
* - timer servicing selected channel
* Note: if different channels use one timer,
* then frequency and bit_num of these channels
* will be the same
*/
ledc_channel_config_t ledc_channel[LEDC_TEST_CH_NUM] = {
{
.channel = LEDC_HS_CH0_CHANNEL,
.duty = 0,
.gpio_num = LEDC_HS_CH0_GPIO,
.speed_mode = LEDC_HS_MODE,
.timer_sel = LEDC_HS_TIMER
},
{
.channel = LEDC_HS_CH1_CHANNEL,
.duty = 0,
.gpio_num = LEDC_HS_CH1_GPIO,
.speed_mode = LEDC_HS_MODE,
.timer_sel = LEDC_HS_TIMER
},
{
.channel = LEDC_LS_CH2_CHANNEL,
.duty = 0,
.gpio_num = LEDC_LS_CH2_GPIO,
.speed_mode = LEDC_LS_MODE,
.timer_sel = LEDC_LS_TIMER
},
{
.channel = LEDC_LS_CH3_CHANNEL,
.duty = 0,
.gpio_num = LEDC_LS_CH3_GPIO,
.speed_mode = LEDC_LS_MODE,
.timer_sel = LEDC_LS_TIMER
},
};
// Set LED Controller with previously prepared configuration
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
ledc_channel_config_t ledc_channel = {
//set LEDC channel 0
.channel = ledc_ch[ch].channel,
//set the duty for initialization.(duty range is 0 ~ ((2**bit_num)-1)
.duty = 0,
//GPIO number
.gpio_num = ledc_ch[ch].io,
//GPIO INTR TYPE, as an example, we enable fade_end interrupt here.
.intr_type = LEDC_INTR_FADE_END,
//set LEDC mode, from ledc_mode_t
.speed_mode = ledc_ch[ch].mode,
//set LEDC timer source, if different channel use one timer,
//the frequency and bit_num of these channels should be the same
.timer_sel = ledc_ch[ch].timer_idx,
};
//set the configuration
ledc_channel_config(&ledc_channel);
ledc_channel_config(&ledc_channel[ch]);
}
//initialize fade service.
// Initialize fade service.
ledc_fade_func_install(0);
while (1) {
printf("LEDC fade up\n");
printf("1. LEDC fade up to duty = %d\n", LEDC_TEST_DUTY);
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
ledc_set_fade_with_time(ledc_ch[ch].mode, ledc_ch[ch].channel, 4000, 2000);
ledc_fade_start(ledc_ch[ch].mode, ledc_ch[ch].channel, LEDC_FADE_NO_WAIT);
ledc_set_fade_with_time(ledc_channel[ch].speed_mode,
ledc_channel[ch].channel, LEDC_TEST_DUTY, LEDC_TEST_FADE_TIME);
ledc_fade_start(ledc_channel[ch].speed_mode,
ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
}
vTaskDelay(3000 / portTICK_PERIOD_MS);
vTaskDelay(LEDC_TEST_FADE_TIME / portTICK_PERIOD_MS);
printf("LEDC fade down\n");
printf("2. LEDC fade down to duty = 0\n");
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
ledc_set_fade_with_time(ledc_ch[ch].mode, ledc_ch[ch].channel, 0, 2000);
ledc_fade_start(ledc_ch[ch].mode, ledc_ch[ch].channel, LEDC_FADE_NO_WAIT);
ledc_set_fade_with_time(ledc_channel[ch].speed_mode,
ledc_channel[ch].channel, 0, LEDC_TEST_FADE_TIME);
ledc_fade_start(ledc_channel[ch].speed_mode,
ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
}
vTaskDelay(3000 / portTICK_PERIOD_MS);
vTaskDelay(LEDC_TEST_FADE_TIME / portTICK_PERIOD_MS);
printf("LEDC set duty without fade\n");
printf("3. LEDC set duty = %d without fade\n", LEDC_TEST_DUTY);
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
ledc_set_duty(ledc_ch[ch].mode, ledc_ch[ch].channel, 2000);
ledc_update_duty(ledc_ch[ch].mode, ledc_ch[ch].channel);
ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, LEDC_TEST_DUTY);
ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
printf("LEDC set duty without fade\n");
printf("4. LEDC set duty = 0 without fade\n");
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
ledc_set_duty(ledc_ch[ch].mode, ledc_ch[ch].channel, 0);
ledc_update_duty(ledc_ch[ch].mode, ledc_ch[ch].channel);
ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, 0);
ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}