[已解决] ESP32 :如何精确控制LED Control功能PWM的脉冲数

chzfmx
Posts: 11
Joined: Tue Jun 12, 2018 4:40 am

[已解决] ESP32 :如何精确控制LED Control功能PWM的脉冲数

Postby chzfmx » Tue Jun 01, 2021 7:16 am

想用ESP32驱动步进电机(开环,无编码器),需要精确控制PWM的脉冲个数,看了下datasheet是有高/低速时钟计数器达到最大计数值触发中断的功能,但是看了下LEDC的API:esp_err_tledc_isr_register(void (*fn)(void *), void *arg, int intr_alloc_flags, ledc_isr_handle_t *handle ),不知道该怎么用:1.比如我两个通道输出PWM(两个电机),怎么注册对应通道的中断函数? 2.intr_alloc_flags参数该填什么?
Last edited by chzfmx on Thu Jun 10, 2021 9:06 am, edited 2 times in total.

ESP_Gargamel
Posts: 786
Joined: Wed Nov 14, 2018 8:45 am

Re: ESP32 :如何精确控制LED Control功能PWM的脉冲数

Postby ESP_Gargamel » Wed Jun 02, 2021 2:56 am

你先看下 MCPWM 和 相关示例,examples/peripherals/mcpwm。

chzfmx
Posts: 11
Joined: Tue Jun 12, 2018 4:40 am

Re: ESP32 :如何精确控制LED Control功能PWM的脉冲数

Postby chzfmx » Thu Jun 03, 2021 6:02 am

谢谢,搞定了 :D

ESP_Gargamel
Posts: 786
Joined: Wed Nov 14, 2018 8:45 am

Re: ESP32 :如何精确控制LED Control功能PWM的脉冲数

Postby ESP_Gargamel » Fri Jun 04, 2021 10:26 am

优秀,能分享一下就更棒了。

chzfmx
Posts: 11
Joined: Tue Jun 12, 2018 4:40 am

Re: ESP32 :如何精确控制LED Control功能PWM的脉冲数

Postby chzfmx » Thu Jun 10, 2021 9:01 am

在ledc_struct.h头文件里有LED Control所有寄存器的描述信息ledc_dev_t:

extern ledc_dev_t LEDC;

//中断回调函数
static void IRAM_ATTR pwm_plus_cnt(void *param)
{
static uint32_t cnt1, cnt2;
uint32_t ledc_intr_status = 0;
ledc_intr_status = LEDC.int_st.val;
if (ledc_intr_status & LEDC_HSTIMER0_OVF_INT_ENA)
{
gpio_set_level(0, cnt1++ % 2);
}
if (ledc_intr_status & LEDC_LSTIMER1_OVF_INT_ENA)
{
gpio_set_level(2, cnt2++ % 2);
}
LEDC.int_clr.val = ledc_intr_status;
}

void ledc_config(void)
{
//定义定时器的参数和每个通道的参数
//略......

//使能相应定时器的溢出中断
LEDC.int_ena.val = LEDC_HSTIMER0_OVF_INT_ENA | LEDC_LSTIMER1_OVF_INT_ENA;
//注册中断回调函数pwm_plus_cnt
ledc_isr_register(pwm_plus_cnt, 0, ESP_INTR_FLAG_IRAM, NULL);

// Set LED Controller with previously prepared configuration
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
ledc_channel_config(&ledc_channel[ch]); //配置相关寄存器,开始输出PWM
}
}

hardcode
Posts: 1
Joined: Wed Sep 15, 2021 2:12 am

Re: [已解决] ESP32 :如何精确控制LED Control功能PWM的脉冲数

Postby hardcode » Wed Sep 15, 2021 2:19 am

楼主想请教下配置计数器初始值是哪个方法啊 :|

TuringQin
Posts: 4
Joined: Sun Feb 20, 2022 7:43 am

Re: [已解决] ESP32 :如何精确控制LED Control功能PWM的脉冲数

Postby TuringQin » Wed Oct 12, 2022 3:36 am

请问是如何解决的我目前也遇到了这个问题

future
Posts: 2
Joined: Tue Nov 01, 2022 2:44 am

Re: [已解决] ESP32 :如何精确控制LED Control功能PWM的脉冲数

Postby future » Wed Jul 24, 2024 3:29 am

不能指定通道启用中断嘛,如果同时其他外设也需要使用pwm,并且需要很高的频率这样就不行了吧

Who is online

Users browsing this forum: Bing [Bot] and 203 guests