<?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>Duty cycle and frequency</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/83317/duty-cycle-and-frequency</link><description>Hi, 
 I am trying to test the sample code example of PWM, 
 I am not able to change duty cycle other than 25% and 75%. 
 please suggest !! 
 Thank you.</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 05 Jan 2022 09:09:18 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/83317/duty-cycle-and-frequency" /><item><title>RE: Duty cycle and frequency</title><link>https://devzone.nordicsemi.com/thread/346069?ContentTypeID=1</link><pubDate>Wed, 05 Jan 2022 09:09:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0d8a79d0-3ecb-4271-9daf-78440e5fb18e</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;I changed the infinite while loop and it seems to work well here, e.g.:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;	max_period = 1000;
	period = 0;

	while(1) {
				ret = pwm_pin_set_usec(pwm, PWM_CHANNEL, max_period, period, PWM_FLAGS);
				if (ret) {
					printk(&amp;quot;Error %d: failed to set pulse width\n&amp;quot;, ret);					
				}	
				period += 10;
				if(period&amp;gt;max_period)
					period = 0;
				
				k_sleep(K_MSEC(100U));
	}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Duty cycle and frequency</title><link>https://devzone.nordicsemi.com/thread/345958?ContentTypeID=1</link><pubDate>Tue, 04 Jan 2022 15:23:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8e958b73-afde-4a15-88d1-368f48de20e5</guid><dc:creator>Swati kumari</dc:creator><description>&lt;p&gt;Hi @ Kenneth,&lt;/p&gt;
&lt;p&gt;below sample code for testing.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.3.2/zephyr/samples/basic/blinky_pwm/README.html"&gt;http://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.3.2/zephyr/samples/basic/blinky_pwm/README.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;nCS\v1.8.0\zephyr\samples\basic\blinky_pwm&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;#include &amp;lt;zephyr.h&amp;gt;&lt;br /&gt;#include &amp;lt;sys/printk.h&amp;gt;&lt;br /&gt;#include &amp;lt;device.h&amp;gt;&lt;br /&gt;#include &amp;lt;drivers/pwm.h&amp;gt;&lt;/p&gt;
&lt;p&gt;#define PWM_LED0_NODE DT_ALIAS(pwm_led0)&lt;/p&gt;
&lt;p&gt;#if DT_NODE_HAS_STATUS(PWM_LED0_NODE, okay)&lt;br /&gt;#define PWM_CTLR DT_PWMS_CTLR(PWM_LED0_NODE)&lt;br /&gt;#define PWM_CHANNEL DT_PWMS_CHANNEL(PWM_LED0_NODE)&lt;br /&gt;#define PWM_FLAGS DT_PWMS_FLAGS(PWM_LED0_NODE)&lt;br /&gt;#else&lt;br /&gt;#error &amp;quot;Unsupported board: pwm-led0 devicetree alias is not defined&amp;quot;&lt;br /&gt;#define PWM_CTLR DT_INVALID_NODE&lt;br /&gt;#define PWM_CHANNEL 0&lt;br /&gt;#define PWM_FLAGS 0&lt;br /&gt;#endif&lt;/p&gt;
&lt;p&gt;#define MIN_PERIOD_USEC (USEC_PER_SEC / 64U)&lt;br /&gt;#define MAX_PERIOD_USEC USEC_PER_SEC&lt;/p&gt;
&lt;p&gt;void main(void)&lt;br /&gt;{&lt;br /&gt; const struct device *pwm;&lt;br /&gt; uint32_t max_period;&lt;br /&gt; uint32_t period;&lt;br /&gt; uint8_t dir = 0U;&lt;br /&gt; int ret;&lt;/p&gt;
&lt;p&gt;printk(&amp;quot;PWM-based blinky\n&amp;quot;);&lt;/p&gt;
&lt;p&gt;pwm = DEVICE_DT_GET(PWM_CTLR);&lt;br /&gt; if (!device_is_ready(pwm)) {&lt;br /&gt; printk(&amp;quot;Error: PWM device %s is not ready\n&amp;quot;, pwm-&amp;gt;name);&lt;br /&gt; return;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;/*&lt;br /&gt; * In case the default MAX_PERIOD_USEC value cannot be set for&lt;br /&gt; * some PWM hardware, decrease its value until it can.&lt;br /&gt; *&lt;br /&gt; * Keep its value at least MIN_PERIOD_USEC * 4 to make sure&lt;br /&gt; * the sample changes frequency at least once.&lt;br /&gt; */&lt;br /&gt; printk(&amp;quot;Calibrating for channel %d...\n&amp;quot;, PWM_CHANNEL);&lt;br /&gt; max_period = MAX_PERIOD_USEC;&lt;br /&gt; while (pwm_pin_set_usec(pwm, PWM_CHANNEL,&lt;br /&gt; max_period, max_period / 2U, PWM_FLAGS)) {&lt;br /&gt; max_period /= 2U;&lt;br /&gt; if (max_period &amp;lt; (4U * MIN_PERIOD_USEC)) {&lt;br /&gt; printk(&amp;quot;Error: PWM device &amp;quot;&lt;br /&gt; &amp;quot;does not support a period at least %u\n&amp;quot;,&lt;br /&gt; 4U * MIN_PERIOD_USEC);&lt;br /&gt; return;&lt;br /&gt; }&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;printk(&amp;quot;Done calibrating; maximum/minimum periods %u/%u usec\n&amp;quot;,&lt;br /&gt; max_period, MIN_PERIOD_USEC);&lt;/p&gt;
&lt;p&gt;period = max_period;&lt;br /&gt; while (1) {&lt;br /&gt; ret = pwm_pin_set_usec(pwm, PWM_CHANNEL,&lt;br /&gt; period, period / 2U, PWM_FLAGS);&lt;br /&gt; if (ret) {&lt;br /&gt; printk(&amp;quot;Error %d: failed to set pulse width\n&amp;quot;, ret);&lt;br /&gt; return;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;period = dir ? (period * 2U) : (period / 2U);&lt;br /&gt; if (period &amp;gt; max_period) {&lt;br /&gt; period = max_period / 2U;&lt;br /&gt; dir = 0U;&lt;br /&gt; } else if (period &amp;lt; MIN_PERIOD_USEC) {&lt;br /&gt; period = MIN_PERIOD_USEC * 2U;&lt;br /&gt; dir = 1U;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;k_sleep(K_SECONDS(4U));&lt;br /&gt; }&lt;br /&gt;}&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Duty cycle and frequency</title><link>https://devzone.nordicsemi.com/thread/345885?ContentTypeID=1</link><pubDate>Tue, 04 Jan 2022 12:06:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:edba3376-748b-4e1d-a5ad-f212f4453982</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Maybe you can share the lines of code you have used to set 25% and 75%, and then I can help you set something else?&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>