This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Nordic SDK: PWM duty cycle of 0 on LED

I am using PWM on a BLE Nano using Nordic SDK. I was having problems with the PWM and decided I would set the duty cycle to 0 to see if the program was working right:

#include <stdbool.h>  
#include <stdint.h>
#include "nrf.h"
#include "app_error.h"
#include "bsp.h"
#include "nrf_delay.h"
#include "app_pwm.h"

#define TIMER1_INSTANCE_INDEX      (TIMER0_ENABLED)


APP_PWM_INSTANCE(PWM1,1);                   // Create the instance "PWM1" using TIMER1.

static volatile bool ready_flag;            // A flag indicating PWM status.

void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
 {
ready_flag = true;
}

int main(void)
{
ret_code_t err_code;

app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(5000L, BSP_LED_0);
int dutyCycle = 0;
app_pwm_channel_duty_set(&PWM1, 0, dutyCycle);

pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;

/* Initialize and enable PWM. */
err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
APP_ERROR_CHECK(err_code);
app_pwm_enable(&PWM1);

}```   

After doing this I hooked an LED up to BSP_LED_0 and was surprised to find that it lit up. What could cause an LED to light up when the duty cycle is 0? Am I doing something wrong?

Parents
  • You are trying to enable the PWM before you initialize it.

    Try this code instead:

    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "app_error.h"
    #include "bsp.h"
    #include "nrf_delay.h"
    #include "app_pwm.h"
    
    #define TIMER1_INSTANCE_INDEX      (TIMER0_ENABLED)
    
    
    APP_PWM_INSTANCE(PWM1,1);                   // Create the instance "PWM1" using TIMER1.
    
    static volatile bool ready_flag;            // A flag indicating PWM status.
    
    
    int main(void)
    {
        ret_code_t err_code;
        
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(2000L, BSP_LED_0);
    
        pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
    
    
    
        /* Initialize and enable PWM. */
        err_code = app_pwm_init(&PWM1,&pwm1_cfg,NULL);
        
        APP_ERROR_CHECK(err_code);
        app_pwm_enable(&PWM1);
    
        
        while(true)
        {
            int dutyCycle = 50;
            /* Set the duty cycle - keep trying until PWM is ready... */
            while(app_pwm_channel_duty_set(&PWM1, 0, dutyCycle) == NRF_ERROR_BUSY);
                    
            // enter into sleep mode
            __SEV();
            __WFE();
            __WFE();
        }
        
    }
    

    This will give you 500 Hz PWM on the BSP_LED_0 pin(P0.21 on the nRF51-DK)

Reply
  • You are trying to enable the PWM before you initialize it.

    Try this code instead:

    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "app_error.h"
    #include "bsp.h"
    #include "nrf_delay.h"
    #include "app_pwm.h"
    
    #define TIMER1_INSTANCE_INDEX      (TIMER0_ENABLED)
    
    
    APP_PWM_INSTANCE(PWM1,1);                   // Create the instance "PWM1" using TIMER1.
    
    static volatile bool ready_flag;            // A flag indicating PWM status.
    
    
    int main(void)
    {
        ret_code_t err_code;
        
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(2000L, BSP_LED_0);
    
        pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
    
    
    
        /* Initialize and enable PWM. */
        err_code = app_pwm_init(&PWM1,&pwm1_cfg,NULL);
        
        APP_ERROR_CHECK(err_code);
        app_pwm_enable(&PWM1);
    
        
        while(true)
        {
            int dutyCycle = 50;
            /* Set the duty cycle - keep trying until PWM is ready... */
            while(app_pwm_channel_duty_set(&PWM1, 0, dutyCycle) == NRF_ERROR_BUSY);
                    
            // enter into sleep mode
            __SEV();
            __WFE();
            __WFE();
        }
        
    }
    

    This will give you 500 Hz PWM on the BSP_LED_0 pin(P0.21 on the nRF51-DK)

Children
No Data
Related