I am trying to better understand PWM on Nordic/Zephyr (nrf52832).

I started with Blinky_PWM and modified that to flash a visible led (led0) at 1hz. It flashes, but I doubt it is at 1hz. And, if I lower the value below 4U, then it doesn’t flash at all… I put comments in the lines I suspect I don’t fully understand.

If someone could explain better (than the zephyr documentation) I would appreciate it.

And, if someone knows the correct setting for PWM at 38Khz I would appreciate seeing that too.

#include <zephyr/sys/printk.h>
#include <zephyr/device.h>
#include <zephyr/drivers/pwm.h>

static const struct pwm_dt_spec pwm_led0 = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led0));

#define ir_rate PWM_HZ(4) //value must be >=4 ???

int main(void)
{
	uint32_t period;
	int ret;

	printk("PWM-based LED Control\n");

	if (!pwm_is_ready_dt(&pwm_led0)) {
		printk("Error: PWM device %s is not ready\n",
		       pwm_led0.dev->name);
		return 0;
	}

	period = ir_rate;
	while (1) {
		ret = pwm_set_dt(&pwm_led0, period, period / 2U); //don't understand duty cycle setting???

		if (ret) {
			printk("Error %d: failed to set pulse width\n", ret);
			return 0;
		}

		k_msleep(10000); //don't understand how k_msleep works here. Doesn't seem to affect anything???
	}
	return 0;
}```

    I can’t seem to edit my original post, but to elaborate. I expected:

    PWM_HZ(1); //to set frequency to 1hz

    ret = pwm_set_dt(&pwm_led0, period, period / 2U); //I expected to set the duty cycle as ‘50’, ‘128’, or ‘13’ (½ 38k period) or something like that to work.

    I expected k_msleep(10000); //to modulate the 38khz signal to the IR which is required for some TSOP modules. I am using TSSP58038, which I don’t believe requires modulation, but I would still like to understand how everything works. K_msleep(any value) doesn’t seem to change the PWM signal at all, which probably makes sense, but I am just learning to use threads now. I usually do bare metal programming.

      Terms and Conditions | Privacy Policy