[已解决] ESP32 :如何精确控制LED Control功能PWM的脉冲数
[已解决] ESP32 :如何精确控制LED Control功能PWM的脉冲数
想用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.
-
- Posts: 786
- Joined: Wed Nov 14, 2018 8:45 am
Re: ESP32 :如何精确控制LED Control功能PWM的脉冲数
你先看下 MCPWM 和 相关示例,examples/peripherals/mcpwm。
-
- Posts: 786
- Joined: Wed Nov 14, 2018 8:45 am
Re: ESP32 :如何精确控制LED Control功能PWM的脉冲数
优秀,能分享一下就更棒了。
Re: ESP32 :如何精确控制LED Control功能PWM的脉冲数
在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
}
}
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
}
}
Re: [已解决] ESP32 :如何精确控制LED Control功能PWM的脉冲数
楼主想请教下配置计数器初始值是哪个方法啊 ![Neutral :|](./images/smilies/icon_neutral.gif)
![Neutral :|](./images/smilies/icon_neutral.gif)
Re: [已解决] ESP32 :如何精确控制LED Control功能PWM的脉冲数
请问是如何解决的我目前也遇到了这个问题
Re: [已解决] ESP32 :如何精确控制LED Control功能PWM的脉冲数
不能指定通道启用中断嘛,如果同时其他外设也需要使用pwm,并且需要很高的频率这样就不行了吧
Who is online
Users browsing this forum: No registered users and 43 guests