How to change pwm period on runtime in ncs?

Hi, 

I'm trying to play some sounds on a piezo with the help of the pwm. I tried to set the period with pwm_set_dt but only the first call is successful and every subsequent call raised an error. 

Here is my test code:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
for(uint16_t i = 400; i < 800; i++)
{
int ret = pwm_set_dt(&piezo_pwm_pos, PWM_HZ(i),
PWM_HZ(i) / 2);
LOG_DBG("pwm_set_dt = %d", ret);
ret = pwm_set_dt(&piezo_pwm_neg, PWM_HZ(i),
PWM_HZ(i) / 2);
LOG_DBG("pwm_set_dt = %d", ret);
k_msleep(10);
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The results looked like this:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
00> D: pwm_set_dt = 0
00> D: pwm_set_dt = 0
00> E: Incompatible period.
00> D: pwm_set_dt = -22
00> E: Incompatible period.
00> D: pwm_set_dt = -22
00> E: Incompatible period.
00> D: pwm_set_dt = -22
00> E: Incompatible period.
00> D: pwm_set_dt = -22
00> E: Incompatible period.
00> D: pwm_set_dt = -22
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I didn't find anything in the documentation of pwm_set for the error 22.

How can I change period and pulse while the pwm is running?

Best regards,

Christian

Parents Reply Children
  • Hi,

    at the moment I'm using SDK5 and can easily change the frequency within an event handler, which is called when the PWM is finished. How is this possible in Nordic Connect SDK?

    Best regards,

    Christian

  • Hi,

    unfortunatelly this doesn't work either. If I set the period with this function I will just get some "clicking" sounds on the piezo. Is there a similar function like in the nrf5 SDK where I can start the PWM for n repeats and then it will automatically stop? 

    Best regards,

    Christian

  • Hi!

    I tried repdicing the issues, but it works fine here.

    Here is the project I used for testing:

    pwm_test.zip

    Tested with NCS 2.8.0 and nRF52832DK(build target nrf52dk/nrf52832)

  • Hi, 

    in your case you just changed the period of one pin. If I do it also for only one channel everything works. I want to set two pins exactly inverted with changing frequency over time as seen in the image below.

    But I only can change the frequency for one channel or set it once for two channels. It's not possible to change the frequency while the pwm is active. I could change the frequency if I disable the pwm for both channels before setting the new frequency, but this results in an interrupted output sound on the piezo. 

    How can I invert channel 1 in relation to channel 0? 

    Best regards,

    Christian