khoih-prog / RP2040_PWM

This library enables you to use Hardware-based PWM channels on RP2040-based boards, such as Nano_RP2040_Connect, RASPBERRY_PI_PICO, with either Arduino-mbed (mbed_nano or mbed_rp2040) or arduino-pico core to create and output PWM any GPIO pin. The most important feature is they're purely hardware-based PWM channels, supporting very high PWM frequencies. Therefore, their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks. These hardware-based PWMs, still work even if other software functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software-based PWM using ISR, millis() or micros(). That's necessary if you need to control devices requiring high precision. New efficient setPWM_manual function to facilitate waveform creation using PWM
MIT License
73 stars 19 forks source link

Setting duty cycle without disturbing frequency #4

Closed Gavin-Perry closed 2 years ago

Gavin-Perry commented 2 years ago

Describe the bug

Added in 1.0.5:

  1. Permit changing dutyCycle and keep same frequency on-the-fly should now work but it's not clear how.
  2. Add example - Example is not clear and is not included in the downloaded library from Arduino
  3. Not sure why bards need to be listed but I'm using the WaveShare RP2040-Zero $5 and small and castellated, just what I need for including in my project. Only difference from a generic is the cute little WS2812 RGB LED which I can't get to work.
  4. Yep, I can lower the debug level, though I'm not sure what those warnings are about.
  5. A clear and concise description of what the bug is. What are the units you are using for duty cycle? Normally with 16 but PWM you would just load a 16 bit count value and be done with it. It looks like you might be using float of 0.0 - 100.0% - Trying to be fast here why mess with floating point numbers? I tried both

    PWM_Instance[0]->setPWM(pin0, freq, (double(lookup8to16[i]), false);  //  Pass 16 bit value, but as a double (which is just a float?)
    PWM_Instance[2]->setPWM(pin2, freq, (double(lookup8to16[(i])/655.35, false);  // 
  // lookup8to16[] is a 256 value array of the gamma curve so brightness is properly log scale for human eye

Steps to Reproduce

Used your example PWM_Multi adding to the loop:

  for (uint i=255; i>0; i-- ) {
     PWM_Instance[0]->setPWM(pin0, freq, (double(lookup8to16[i]), false);  // 
    PWM_Instance[2]->setPWM(pin2, freq, (double(lookup8to16[(i+0x80)&0xff])/655.35, false);  // offset this one by 1/2 the table
    delay(30);
  } 

Also tried

// Do it by reaching in past the library to the PWM hardware
    pwm_set_counter (pwm_gpio_to_slice_num(pin0), (lookup8to16[i]/655.35));  // 0 to 100%?
   pwm_set_counter (pwm_gpio_to_slice_num(pin1), lookup8to16[(i+64)&0xff]); // 16 bit value?

Expected behavior

I'm looking for a smooth increase and then decrease in the duty cycle (and thus LED brightness) from 0 to 100% and back to 0

Actual behavior

Depending on which version I try I get some smooth action then big jumps in brightness (something is wrapping to zero at the wrong time). Even when it is working over some of the range it's flickering when it shouldn't (resetting the slice? IDK)

debug

set level to 3 don't get any reports One odd thing is that when I enable the Serial Monitor it always says (in orange at the top of Monitor pane): "Not Connected. Select a board and port to connect automatically" But then it works anyway. Took me a while to realize it's in a pane of the program window not a separate window as in 1.8 1.8 couldn't find the PIO.h file

Screenshots

I could send a video of the LED or scope traces of the PWM (Have to set up the o'scope)

Information

  setPWMfreq(pin#,newfreq);
  setPWMdcPct(pin#,newdc); // In percent if people really want that
  setPWMdc16(pin#, uint newDCval);  // 16 bit value

I've been reading the Pico hardware manual, it's dense, this machine is so powerful! I'm happy that you dug in and extracted a working PWM package, it just needs a little tweeking and documentation I think.

BTW another odd thing: After working for quite a while loading code after compile just fine. It quit loading and now requires unplugging and holding boot button every time. Is that because I'm using all the PIO?

khoih-prog commented 2 years ago

HI @Gavin-Perry

You have to write a better and concise issue report. I won't spend time to go thru your rant, without proof, enough and correct info to test and duplicated.

Please follow and post the standard MRE so that anyone can duplicate without wasting time.

Also post the issue to the correct library or core. Separate the issue to many issues as necessary.

I have to close this and wait for anything better to act upon.

Gavin-Perry commented 2 years ago

Khoi,

I apologize for the issue post on duty cycle appearing to be a rant. I posted it after a long frustrating day well after midnight and really should have read it over the next day before posting it. I thought I was covering all the requirements of an issue as I followed the outline given in the issue template. Ill check the MRE link in the future and be sure not to mix other issues into the post (especially since the git issue space is a public forum.

Upon rereading your Multi example I see that you are in fact using % for duty cycle. The issue is converting from double to uint16_t seems to be affecting the smoothness. I'll work on it some more and create a concise example that you can run.

THe second part is a suggestion that there be a simple SetPWMDC16bit routine (or whatever you would want to call it so that I can drop in my 16 bit duty cycle instead of converting to float and back.

Thanks again for the support and clarification about MRE. I owe you another cup of coffee. ;-)

Gavin

-- Gavin Perry, PhD Chief Technology Officer Meridian Electric Company 2392 Grissom Drive St Louis MO 63146 http://meridianlighting.com cell: 314 406-0697 http://voice.google.com/calls?a=nc,%2B13144060697

On Sun, Feb 20, 2022 at 11:35 PM Khoi Hoang @.***> wrote:

Closed #4 https://github.com/khoih-prog/RP2040_PWM/issues/4.

— Reply to this email directly, view it on GitHub https://github.com/khoih-prog/RP2040_PWM/issues/4#event-6108912672, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVR4PP4NDPYOEZM5WE3ITOLU4HFLFANCNFSM5O4ETJDQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

khoih-prog commented 2 years ago

Hi @Gavin-Perry

I'm sorry if you feel offended. I really didn't mean so.

We're all in the hi-tech, not sales and used to speak not in diplomatic way to have fast and good results.

I'm inviting you into a private repo, where many experts, I handpicked, have been participating and we can discuss / support each other relating to many more hi-tech subjects, sometimes more personally and suitable than in this public repo.

Regards,