<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Synchronized PWM outputs with nRF Connect SDK</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/107826/synchronized-pwm-outputs-with-nrf-connect-sdk</link><description>I’m trying to set up two synchronized PWM outputs, to control the high and low side switches of a half bridge. The result should look like Fig 3 in the PWM module hardware documentartion , but I don’t know how to get that result. Starting from blinky_pwm</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sat, 27 Jan 2024 22:32:56 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/107826/synchronized-pwm-outputs-with-nrf-connect-sdk" /><item><title>RE: Synchronized PWM outputs with nRF Connect SDK</title><link>https://devzone.nordicsemi.com/thread/466342?ContentTypeID=1</link><pubDate>Sat, 27 Jan 2024 22:32:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:962056be-7f42-411e-b6a2-4f55b0d96b61</guid><dc:creator>DougR</dc:creator><description>&lt;p&gt;I got it working. Looks like pwm_set_dt() only sets the first channel. The following code sets each channel individually, and seems to work as desired.&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;zephyr/sys/printk.h&amp;gt;
#include &amp;lt;zephyr/device.h&amp;gt;
#include &amp;lt;zephyr/drivers/pwm.h&amp;gt;
#include &amp;lt;math.h&amp;gt;

static const struct pwm_dt_spec custompwm0 = PWM_DT_SPEC_GET(DT_ALIAS(mycustompwm));

#define PWM_FREQ 100 // Hz
#define CYCLE_FREQ 1 // Hz

#define STEPS 8
float levels[STEPS]; 
#define DUTY_RANGE 0.8f
#define DEADTIME_NS 500U

#define TWO_PI 6.28318530718f

int main(void)
{
	int ret;

	if (!device_is_ready(custompwm0.dev)) {
		printk(&amp;quot;Error: PWM device %s is not ready\n&amp;quot;,
		       custompwm0.dev-&amp;gt;name);
		return 0;
	}

	for (int i =0; i &amp;lt; STEPS; i++) {
		levels[i] = 0.5f*(1 + DUTY_RANGE*sin(TWO_PI*i/STEPS));
	}

	int count = 0;
	
	while (1) {
		ret = pwm_set_dt(&amp;amp;custompwm0, 
			PWM_HZ(PWM_FREQ), 
			(int) PWM_HZ(PWM_FREQ)*levels[count++ % STEPS]);
		ret = pwm_set(custompwm0.dev, 1,
			PWM_HZ(PWM_FREQ), 
			(unsigned int) PWM_HZ(PWM_FREQ)*levels[count % STEPS] - DEADTIME_NS, PWM_POLARITY_NORMAL);
		ret = pwm_set(custompwm0.dev, 2,
			PWM_HZ(PWM_FREQ), 
			(unsigned int) PWM_HZ(PWM_FREQ)*levels[count % STEPS] + DEADTIME_NS, PWM_POLARITY_INVERTED);

		if (ret) {
			printk(&amp;quot;Error %d: failed to set pulse width\n&amp;quot;, ret);
			return 0;
		}

		k_sleep(K_USEC(1000000U/(CYCLE_FREQ*STEPS)));
	}
	return 0;
}
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>