LEDC PWM Driver on esp32

Post and discuss new components that you have created.
Chadb
Posts: 63
http://meble-kuchenne.info.pl
Joined: Fri Dec 18, 2020 1:15 pm
Has thanked: 23 times

Re: LEDC PWM Driver on esp32

Post by Chadb »

Yeah, I get that. I am just confused why the SetDutyCycle doesn't work outside a loop and the set fade does. In theory, they should be both set and forget? Any way. I am looking forward to this being a component. Thanks for working on this. I have not seen the guts of an almost macro before. I am learning a lot.

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: LEDC PWM Driver on esp32

Post by mnfisher »

Does seem to work outside a loop - see above. Set and go...

Chadb
Posts: 63
Joined: Fri Dec 18, 2020 1:15 pm
Has thanked: 23 times

Re: LEDC PWM Driver on esp32

Post by Chadb »

Yeah, I get that. I am just confused why the SetDutyCycle doesn't work outside a loop and the set fade does. In theory, they should be both set and forget? Any way. I am looking forward to this being a component. Thanks for working on this. I have not seen the guts of an almost macro before. I am learning a lot.
The 1hz makes a nice simple heartbeat.

chad
*edit just saw your last post

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: LEDC PWM Driver on esp32

Post by mnfisher »

A second attempt. A less Flowcodian approach - but as you can have up to 16 channels - I gave it a go. It isn't easy to 'step' through multiple components - whereas this way it's possible to change multiple channels in a loop.

Here I set up 4 timers (all HS) with different frequencies and then open 8 channels on the pin assignments given in 'pins' in main's variables. The 8 HS and 8 low speed channels each have four timers (and there are some limits on clock / rate /combinations) Here - I set 5000, 2500, 1250, and 625 Hz and channels are assigned two to each timer.

Initially the pins are set to 100% duty for 5s and then a simple fade with each step 80% of previous duty. Then back to 100--0-100% fades Results on the scope look good - I'll perhaps wire an LED to a PIN or two and see how they look,

The docs imply it should be possible to have a 40Mhz PWM (0, 50 or 100% only?) but the esp32 throws errors at 10kHz and above for me?
ledc_driver.fcfx
(22.17 KiB) Downloaded 95 times
Oddly it seems to work well for HS mode - SetDuty doesn't seem to work for LS mode (LEDC not intialised) - but fade does..
Almost tempted to change channel to [0..15] and select speed_mode automatically - anyone any thoughts?

Martin

Chadb
Posts: 63
Joined: Fri Dec 18, 2020 1:15 pm
Has thanked: 23 times

Re: LEDC PWM Driver on esp32

Post by Chadb »

I had to do some python and git work for my LLM's and I broke my esp programming. I am going to try to get it going again and I will take a look.
Thanks!

Chad

Chadb
Posts: 63
Joined: Fri Dec 18, 2020 1:15 pm
Has thanked: 23 times

Re: LEDC PWM Driver on esp32

Post by Chadb »

Hi, I got my flow code programming again, and it was surprisingly easy. I just had to re-run the installation . bat file in the espidf.
It is running and programming in python 10.6 with the latest git FYI.

I played with your test, and it is running here. I agree with the speed mode, Just use the high speed and call it a day. I assume that if someone is using lots of led's they aren't going to care as much about power saving by using a slower timer? I also assume this will make the pwm freq easier to predict. I.E. 0 to 5k based on the resolution. If someone need to do something different with the slower timers they can do it in c.
here is some more info I found. This is how Arduino handles it. Also, esp's have different PWM channel amount numbers. Not sure how to handle this.
https://espressif-docs.readthedocs-host ... /ledc.html

Chad

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: LEDC PWM Driver on esp32

Post by mnfisher »

Hi Chad,

Thanks for the link and the feedback - I haven't had a play using tthe Arduino code - but many useful ideas in the link.

The number of channels etc could be found programmatically - for example GetMaxChannels() - there aee constants defined in the header files (eg LEDC_MAX_CHANNEL)

I was going to have a play with getting the interrupt to work too - as I see that being useful - I often use callbacks etc in my Flowcode - using cb = &FCM_Name; in C to get the address of the function. Storing pointers is easy too (just use a 16 or 32 bit unsigned integer as appropriate) The esp32 is 'hot' on type checking - so casts are essential too.

MArtin

Chadb
Posts: 63
Joined: Fri Dec 18, 2020 1:15 pm
Has thanked: 23 times

Re: LEDC PWM Driver on esp32

Post by Chadb »

Noob question. What does the .variable do? I am assuming that is a local undeclared variable type?
Also have you noticed when picking pins for a component there is a portB? There is no portb. Or is there?

Chad

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: LEDC PWM Driver on esp32

Post by mnfisher »

Yes - the. '.' signifies a local variable - either a parameter passed to a macro or a locally defined variable - (or constant). It's scope is limited - it can't be accessed from outside of the macro - and they are only allocated (on the stack) when in the macro - so only use memory then too (unlike a global)
They help make code more readable and easier to follow - I'd strongly advocate their use...

There is a port b - however the pins are more limited in use - and they can't be allocated for most functions...

Martin

Post Reply