mirror of
https://github.com/espressif/esp-idf.git
synced 2025-09-30 19:19:21 +00:00
Merge branch 'feature/ledc_low_speed_channels' into 'master'
add ledc low speed channels 1. add low speed channels 2. modify example code. See merge request !490
This commit is contained in:
@@ -14,3 +14,4 @@
|
||||
|
||||
* This example use GPIO18/19/4/5 as LEDC ouput, and it will change the duty repeatedly.
|
||||
|
||||
* GPIO18/19 are from high speed channel group. GPIO4/5 are from low speed channel group.
|
||||
|
@@ -27,105 +27,124 @@
|
||||
*
|
||||
* 4. This example use GPIO18/19/4/5 as LEDC ouput, and it will change the duty repeatedly.
|
||||
*
|
||||
* 5. GPIO18/19 are from high speed channel group. GPIO4/5 are from low speed channel group.
|
||||
*
|
||||
*/
|
||||
#define LEDC_HS_TIMER LEDC_TIMER_0
|
||||
#define LEDC_HS_MODE LEDC_HIGH_SPEED_MODE
|
||||
#define LEDC_HS_CH0_GPIO (18)
|
||||
#define LEDC_HS_CH0_CHANNEL LEDC_CHANNEL_0
|
||||
#define LEDC_HS_CH1_GPIO (19)
|
||||
#define LEDC_HS_CH1_CHANNEL LEDC_CHANNEL_1
|
||||
|
||||
#define LEDC_IO_0 (18)
|
||||
#define LEDC_IO_1 (19)
|
||||
#define LEDC_IO_2 (4)
|
||||
#define LEDC_IO_3 (5)
|
||||
#define LEDC_LS_TIMER LEDC_TIMER_1
|
||||
#define LEDC_LS_MODE LEDC_LOW_SPEED_MODE
|
||||
#define LEDC_LS_CH2_GPIO (4)
|
||||
#define LEDC_LS_CH2_CHANNEL LEDC_CHANNEL_2
|
||||
#define LEDC_LS_CH3_GPIO (5)
|
||||
#define LEDC_LS_CH3_CHANNEL LEDC_CHANNEL_3
|
||||
|
||||
esp_err_t app_main()
|
||||
{
|
||||
ledc_timer_config_t ledc_timer = {
|
||||
//set timer counter bit number
|
||||
.bit_num = LEDC_TIMER_13_BIT,
|
||||
//set frequency of pwm
|
||||
.freq_hz = 5000,
|
||||
//timer mode,
|
||||
.speed_mode = LEDC_HIGH_SPEED_MODE,
|
||||
//timer index
|
||||
.timer_num = LEDC_TIMER_0
|
||||
#define LEDC_TEST_CH_NUM (4)
|
||||
typedef struct {
|
||||
int channel;
|
||||
int io;
|
||||
int mode;
|
||||
int timer_idx;
|
||||
} ledc_info_t;
|
||||
|
||||
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
|
||||
}
|
||||
};
|
||||
|
||||
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
|
||||
};
|
||||
//configure timer0 for high speed channels
|
||||
ledc_timer_config(&ledc_timer);
|
||||
|
||||
ledc_channel_config_t ledc_channel = {
|
||||
//set LEDC channel 0
|
||||
.channel = LEDC_CHANNEL_0,
|
||||
//set the duty for initialization.(duty range is 0 ~ ((2**bit_num)-1)
|
||||
.duty = 100,
|
||||
//GPIO number
|
||||
.gpio_num = LEDC_IO_0,
|
||||
//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_HIGH_SPEED_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_TIMER_0
|
||||
};
|
||||
//set the configuration
|
||||
ledc_channel_config(&ledc_channel);
|
||||
//configure timer1 for low speed channels
|
||||
ledc_timer.speed_mode = LEDC_LS_MODE;
|
||||
ledc_timer.timer_num = LEDC_LS_TIMER;
|
||||
ledc_timer_config(&ledc_timer);
|
||||
|
||||
//config ledc channel1
|
||||
ledc_channel.channel = LEDC_CHANNEL_1;
|
||||
ledc_channel.gpio_num = LEDC_IO_1;
|
||||
ledc_channel_config(&ledc_channel);
|
||||
//config ledc channel2
|
||||
ledc_channel.channel = LEDC_CHANNEL_2;
|
||||
ledc_channel.gpio_num = LEDC_IO_2;
|
||||
ledc_channel_config(&ledc_channel);
|
||||
//config ledc channel3
|
||||
ledc_channel.channel = LEDC_CHANNEL_3;
|
||||
ledc_channel.gpio_num = LEDC_IO_3;
|
||||
ledc_channel_config(&ledc_channel);
|
||||
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);
|
||||
}
|
||||
|
||||
//initialize fade service.
|
||||
ledc_fade_func_install(0);
|
||||
|
||||
while(1) {
|
||||
while (1) {
|
||||
printf("LEDC fade up\n");
|
||||
ledc_set_fade_with_time(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 1000, 2000);
|
||||
ledc_set_fade_with_time(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 7000, 2000);
|
||||
ledc_set_fade_with_time(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 5000, 2000);
|
||||
ledc_set_fade_with_time(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 3000, 2000);
|
||||
ledc_fade_start(LEDC_CHANNEL_0, LEDC_FADE_NO_WAIT);
|
||||
ledc_fade_start(LEDC_CHANNEL_1, LEDC_FADE_NO_WAIT);
|
||||
ledc_fade_start(LEDC_CHANNEL_2, LEDC_FADE_NO_WAIT);
|
||||
ledc_fade_start(LEDC_CHANNEL_3, LEDC_FADE_NO_WAIT);
|
||||
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);
|
||||
}
|
||||
vTaskDelay(3000 / portTICK_PERIOD_MS);
|
||||
|
||||
printf("LEDC fade down\n");
|
||||
ledc_set_fade_with_time(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 100, 2000);
|
||||
ledc_set_fade_with_time(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 300, 2000);
|
||||
ledc_set_fade_with_time(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 500, 2000);
|
||||
ledc_set_fade_with_time(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 700, 2000);
|
||||
ledc_fade_start(LEDC_CHANNEL_0, LEDC_FADE_NO_WAIT);
|
||||
ledc_fade_start(LEDC_CHANNEL_1, LEDC_FADE_NO_WAIT);
|
||||
ledc_fade_start(LEDC_CHANNEL_2, LEDC_FADE_NO_WAIT);
|
||||
ledc_fade_start(LEDC_CHANNEL_3, LEDC_FADE_NO_WAIT);
|
||||
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);
|
||||
}
|
||||
vTaskDelay(3000 / portTICK_PERIOD_MS);
|
||||
|
||||
printf("LEDC set duty without fade\n");
|
||||
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 1000);
|
||||
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 7000);
|
||||
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 5000);
|
||||
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 3000);
|
||||
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0);
|
||||
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1);
|
||||
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2);
|
||||
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3);
|
||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||
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);
|
||||
}
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
|
||||
printf("LEDC set duty without fade\n");
|
||||
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0);
|
||||
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0);
|
||||
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0);
|
||||
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, 0);
|
||||
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0);
|
||||
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1);
|
||||
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2);
|
||||
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3);
|
||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||
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);
|
||||
}
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user