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;
}```