How to use LEDC with inverted pin?

vonnieda
Posts: 145
Joined: Tue Nov 07, 2017 3:42 pm

How to use LEDC with inverted pin?

Postby vonnieda » Tue Aug 31, 2021 4:16 pm

Hi all,

I have a LED I need to control with LEDC for PWM. The pin is inverted in the hardware, i.e. 1 is off, 0 is on. The LEDC API expects the opposite. I've sort of fixed this by inverting my duty cycle, and that works fine, but when I first enable the LEDC channel there is a very brief flicker where the LED turns on before turning off.

Here is how I am configuring LEDC:

Code: Select all

	// Configure LEDC for backlight PWM
	ledc_timer_config_t ledc_timer = {
		.duty_resolution = LEDC_DUTY_RESOLUTION,
		.freq_hz = LEDC_FREQ,
		.speed_mode = LEDC_SPEED_MODE,
		.timer_num = LEDC_TIMER_1
	};
	ledc_timer_config(&ledc_timer);

	ledc_channel_config_t ledc_channel = {
		.channel    = LEDC_CHANNEL_LCD_BL_BASE,
		.duty       = (1 << LEDC_DUTY_RESOLUTION) - 1,
		.gpio_num   = LCD_IO_BACKLIGHT_N,
		.speed_mode = LEDC_SPEED_MODE,
		.hpoint     = 0,
		.timer_sel  = LEDC_TIMER_1
	};
	ledc_channel_config(&ledc_channel);
	
The flicker happens in the call to ledc_channel_config. Before that call the LED is off, during the call it turns on very briefly, and when the call returns it is off again.

So, question: Is there a specific way I should use LEDC with an inverted pin?

And follow up question: Is there a way to avoid the brief "on" before it turns "off"?


Thanks,
Jason

Victoria Nope
Posts: 75
Joined: Fri Dec 04, 2020 9:56 pm

Re: How to use LEDC with inverted pin?

Postby Victoria Nope » Tue Aug 31, 2021 5:27 pm

Set the output_invert member of the ledc_channel_config_t structure's flags to 1 or 0 respectively.

vonnieda
Posts: 145
Joined: Tue Nov 07, 2017 3:42 pm

Re: How to use LEDC with inverted pin?

Postby vonnieda » Tue Aug 31, 2021 5:44 pm

Victoria Nope wrote:
Tue Aug 31, 2021 5:27 pm
Set the output_invert member of the ledc_channel_config_t structure's flags to 1 or 0 respectively.

Thanks for that! Looks like that flag is not in my version of IDF, but easy enough to backport.

Thanks,
Jason

Victoria Nope
Posts: 75
Joined: Fri Dec 04, 2020 9:56 pm

Re: How to use LEDC with inverted pin?

Postby Victoria Nope » Wed Sep 01, 2021 2:56 am

If you did not want to upgrade IDF, you could just modify relevant components to pass the inverting value to the already existing out_inv parameter of the esp_rom_gpio_connect_out_signal function call like has been done recently.

But overall upgrade is what I would highly recommend.

ESP_Sprite
Posts: 9759
Joined: Thu Nov 26, 2015 4:08 am

Re: How to use LEDC with inverted pin?

Postby ESP_Sprite » Wed Sep 01, 2021 3:21 am

Either that, or call esp_rom_gpio_connect_out_signal() in your own code with the proper values after initializing LEDC. +1 on the 'better to upgrade' thing though.

Who is online

Users browsing this forum: No registered users and 126 guests