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?

  • Hi,

    Looks like there is a bug in SDK 8.1 with the pwm/ppi library when you have the SOFTDEVICE_PRESENT flag, the bug causes the program to run into a hardfault. Ideally, you should upgrade to a newer SDK, but you could also fix this by downloading SDK 10 from here, and replace the following libraries in SDK 8.1 with the SDK 10 libraries.

    Replace the following files:

    The 2 files located in SDK_folder\components\libraries\pwm

    The 2 files located in SDK_folder\components\drivers_nrf\ppi

    The file called nrf_ppi in SDK_folder\components\drivers_nrf\hal

  • @sigurd I updated to the sdk 10 libraries and the behavior didn't change at all. Are you sure that the code you sent me works or is it possible there are problems with it? Also is there a difference in the SDK10 PWM and SDK12 PWM. I don't want to update it if it isn't going to make a difference.

  • Have you flashed the SoftDevice? Does the code work without the SoftDevice in the peripheral/pwm example ?

  • What do you mean flash the softdevice? I use the following commands to update the code the device is running: make all SOFTDEVICE=~/BleNano/components/softdevice/s120/hex/s120_softdevice.hex OUTPUT=_build/output.hex INPUT=_build/nrf51422_xxac_s120.hex srec_cat $SOFTDEVICE -intel $INPUT -intel -o $OUTPUT -intel --line-length=44 cp $OUTPUT /media/tyler/DAPLINK/ I know that the device is being programmed because if I comment out all the code then the LED doesn't turn on.

  • How do I know if the code will work without a Softdevice? I'm confused on how I could run a program without the softdevice. I am unable to compile the peripheral/pwm because when I try to run the srec_cat $SOFTDEVICE -intel $INPUT -intel -o $OUTPUT -intel --line-length=44 command I get the following error: "srec_cat: _build/nrf51422_xxac_s120.hex: open: No such file or directory". I tried switching the INPUT to _build/nrf51422_xxac.hex because that file existed, but when I run the srec command I get the following error: "srec_cat: _build/nrf51422_xxac.hex: 1: contradictory 0x00000000 value (previous = 0xC0, this one = 0x00)"

Related