<?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>Unable to stop PWM</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/76510/unable-to-stop-pwm</link><description>Hi guys, 
 I am now facing a problem that I can&amp;#39;t stop the pwm by using nrf_drv_pwm_stop. 
 
 Here&amp;#39;s my initial code for pwm and start the pwm in the while loop. 
 The output pin can complete generate the pulse 400Hz bit lust can&amp;#39;t stop. 
 
 
 The error</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 23 Jun 2021 07:53:21 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/76510/unable-to-stop-pwm" /><item><title>RE: Unable to stop PWM</title><link>https://devzone.nordicsemi.com/thread/316616?ContentTypeID=1</link><pubDate>Wed, 23 Jun 2021 07:53:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2e93e335-83f5-46a0-a4d3-69865463b5aa</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Did you try to add the additional delay (nrf_delay_ms) in your original code like I suggested? I didn&amp;#39;t mean to suggest that you should change the PWM_Stop() function.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unable to stop PWM</title><link>https://devzone.nordicsemi.com/thread/316367?ContentTypeID=1</link><pubDate>Tue, 22 Jun 2021 02:55:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:40dd740e-180d-4a3d-82dc-3633b5990662</guid><dc:creator>JoeTing</dc:creator><description>&lt;p&gt;Hi Vidar,&lt;/p&gt;
&lt;p&gt;I just tried&amp;nbsp;&amp;nbsp;&lt;span&gt;&lt;a title="nrfx_pwm_stop" href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.2/group__nrfx__pwm.html?cp=8_1_6_8_0_24_1_21#ga7d372f2f710abacd79ec5aefc358c8af"&gt;nrfx_pwm_stop&lt;/a&gt;&lt;/span&gt;&lt;span&gt;() but still didn&amp;#39;t work.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;So I did another method to control the duty cycle.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Here&amp;#39;s my code below：&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void PWM_Init_Start(void)
{
    NRF_LOG_INFO(&amp;quot;Demo 5&amp;quot;);

    nrf_drv_pwm_config_t const AFE4404_PWM_Config =
    {
        .output_pins =
        {
            Motor_pin | NRF_DRV_PWM_PIN_INVERTED, // channel 0
           // BSP_LED_2 | NRF_DRV_PWM_PIN_INVERTED, // channel 1
          //  BSP_LED_3 | NRF_DRV_PWM_PIN_INVERTED, // channel 2
           // BSP_LED_1 | NRF_DRV_PWM_PIN_INVERTED  // channel 3
        },
        .irq_priority = APP_IRQ_PRIORITY_LOWEST,
        .base_clock   = NRF_PWM_CLK_1MHz,
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = 1,
        .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
        .step_mode    = NRF_PWM_STEP_AUTO
    };
    APP_ERROR_CHECK(nrf_drv_pwm_init(&amp;amp;m_pwm0, &amp;amp;AFE4404_PWM_Config, NULL));
    m_used |= USED_PWM(0);


}

void PWM_Start_Cycle(void){
	    // This array cannot be allocated on stack (hence &amp;quot;static&amp;quot;) and it must
    // be in RAM (hence no &amp;quot;const&amp;quot;, though its content is not changed).
    static nrf_pwm_values_individual_t /*const*/ seq_values[] =
    {
        {      0},
        {      0},
				{      0},
				{      0},
				{      0},
				{      0},
				{      0},
				{      0},
				{      0},
				{ 0x8000}
    };
    nrf_pwm_sequence_t const seq =
    {
        .values.p_individual = seq_values,
        .length              = NRF_PWM_VALUES_LENGTH(seq_values),
        .repeats             = 0,
        .end_delay           = 0
    };

    (void)nrf_drv_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;seq, 1, NRF_DRV_PWM_FLAG_LOOP);
}

void PWM_Init_Stop(void)
{
    NRF_LOG_INFO(&amp;quot;Demo 5&amp;quot;);

    nrf_drv_pwm_config_t const AFE4404_PWM_Config =
    {
        .output_pins =
        {
            Motor_pin | NRF_DRV_PWM_PIN_INVERTED, // channel 0
           // BSP_LED_2 | NRF_DRV_PWM_PIN_INVERTED, // channel 1
          //  BSP_LED_3 | NRF_DRV_PWM_PIN_INVERTED, // channel 2
           // BSP_LED_1 | NRF_DRV_PWM_PIN_INVERTED  // channel 3
        },
        .irq_priority = APP_IRQ_PRIORITY_LOWEST,
        .base_clock   = NRF_PWM_CLK_1MHz,
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = 1,
        .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
        .step_mode    = NRF_PWM_STEP_AUTO
    };
    APP_ERROR_CHECK(nrf_drv_pwm_init(&amp;amp;m_pwm0, &amp;amp;AFE4404_PWM_Config, NULL));
    m_used |= USED_PWM(0);


}

void PWM_Stop_Cycle(void){
	    // This array cannot be allocated on stack (hence &amp;quot;static&amp;quot;) and it must
    // be in RAM (hence no &amp;quot;const&amp;quot;, though its content is not changed).
    static nrf_pwm_values_individual_t /*const*/ seq_values[] =
    {
        { 0x8000},
        { 0x8000},
				{ 0x8000},
				{ 0x8000},
				{ 0x8000},
				{ 0x8000},
				{ 0x8000},
				{ 0x8000},
				{ 0x8000},
				{ 0x8000}
    };
    nrf_pwm_sequence_t const seq =
    {
        .values.p_individual = seq_values,
        .length              = NRF_PWM_VALUES_LENGTH(seq_values),
        .repeats             = 0,
        .end_delay           = 0
    };

    (void)nrf_drv_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;seq, 1, NRF_DRV_PWM_FLAG_LOOP);
}

void PWM_Start(void){
	PWM_Init_Start();
	PWM_Start_Cycle();
}

void PWM_Stop(void){
	PWM_Init_Stop();
	PWM_Stop_Cycle();
}&lt;/pre&gt;&lt;/span&gt;&lt;span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;And I just combine the function into below:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void mask_on(void){
	PWM_Start();
	nrf_delay_ms(2000);
	PWM_Stop();	
}&lt;/pre&gt;&lt;/p&gt;
&lt;div class="eJOY__extension_root_class" id="eJOY__extension_root"&gt;But something weird is that when I call the mask_on in my main region, the pwm is on and off repeatedly.&lt;/div&gt;
&lt;div class="eJOY__extension_root_class"&gt;&lt;/div&gt;
&lt;div class="eJOY__extension_root_class"&gt;Are there any another way to stop the pwm？&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unable to stop PWM</title><link>https://devzone.nordicsemi.com/thread/316249?ContentTypeID=1</link><pubDate>Mon, 21 Jun 2021 11:32:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:619d685a-b5cc-4a49-8b9e-ce7e6cf95c36</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Try to add a delay after PWM_Stop():&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;    for (;;)
    {
		PWM_Start();
		nrf_delay_ms(500);
		PWM_Stop();
		nrf_delay_ms(500);
		
		/* Application level interrupts may prevent this function 
		 * from entering sleep. 
		 */	
        idle_state_handle(); 
    }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t see the implementation of idle_state_handle(), but I think what&amp;#39;s happening in your program is that PWM_Stop() is immediately being followed by a call to PWM_Start(). &lt;span&gt;&lt;a title="nrfx_pwm_stop" href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.2/group__nrfx__pwm.html?cp=8_1_6_8_0_24_1_21#ga7d372f2f710abacd79ec5aefc358c8af"&gt;nrfx_pwm_stop&lt;/a&gt;&lt;/span&gt;() is a boolean function and returns &amp;#39;1&amp;#39; when the PWM is stopped, so it&amp;#39;s not returning any errors.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Vidar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>