<?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>PWM - Demo 1 Event Handler</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/39361/pwm---demo-1-event-handler</link><description>For simplicity, I extracted the Demo 1 routine from the &amp;quot;pwm_driver&amp;quot; example which has 5 demos &amp;#39;cause I thought it is simpler. 
 The code is shown below: 
 
 
 Can you please explain in detail what is going on inside the demo1_handler? 
 I can&amp;#39;t seem</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 11 Oct 2018 08:46:38 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/39361/pwm---demo-1-event-handler" /><item><title>RE: PWM - Demo 1 Event Handler</title><link>https://devzone.nordicsemi.com/thread/152452?ContentTypeID=1</link><pubDate>Thu, 11 Oct 2018 08:46:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f662b3d3-3d18-4ba2-afc4-5f387e2fb8cf</guid><dc:creator>awneil</dc:creator><description>&lt;p&gt;You have read the description, and watched what the demo does, haven&amp;#39;t you?&lt;/p&gt;
&lt;p&gt;&lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v15.0.0%2Fpwm_hw_example.html"&gt;https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v15.0.0%2Fpwm_hw_example.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;On an nRF52DK (PCA10040), with four LEDs, it fades each one up and then down in turn:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Fade LED1 up, then down&lt;/li&gt;
&lt;li&gt;Fade LED2 up, then down&lt;/li&gt;
&lt;li&gt;Fade LED3 up, then down&lt;/li&gt;
&lt;li&gt;Fade LED4 up, then down&lt;/li&gt;
&lt;li&gt;repeat from 1.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I do agree that Nordic could explain this much better, but the easiest way to understand this type of thing is to follow what happens in the debugger.&lt;/p&gt;
&lt;p&gt;The first thing to note is what the&amp;nbsp;&lt;strong&gt;m_demo1_phase&lt;/strong&gt;&amp;nbsp;variable does:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static uint8_t                     m_demo1_phase;           // 0: Fading up   LED 1
                                                            // 1: Fading down LED 1

                                                            // 2: Fading up   LED 2
                                                            // 3: Fading down LED 2
                                                            
                                                            // 4: Fading up   LED 3
                                                            // 5: Fading down LED 3
                                                            
                                                            // 6: Fading up   LED 4
                                                            // 7: Fading down LED 4

                                                            // So: Evens are fading Up;
                                                            //     Odds  are fading Down.

&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Which then explains what&amp;#39;s happening here&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint8_t channel    = m_demo1_phase &amp;gt;&amp;gt; 1;
bool    down       = m_demo1_phase &amp;amp; 1;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The rest is just incrementing up to max during the fade-up, and then decrementing down to min for the fade down.&lt;/p&gt;
&lt;p&gt;When the decrement reaches minimum, the next channel (ie, the next LED) is selected.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>