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
  • I tried switching to TIMER0 and got the following error: 'TIMER0_INSTANCE_INDEX' undeclared here (not in a function).
    One important detail I should add is that I am actually using the ble_central/app_hrs project instead of the peripheral/pwm example. I just copied the main.c from pwm to app_hrs because the app_hrs was already configured to use a BLE nano. If I remember correctly I had to change some stuff in config/nrf_drv_config.h pertaining to the timer to get the project to compile.......

    I was able to set the pin to both high and low using the commands nrf_gpio_cfg_output(28); nrf_gpio_pin_clear(28); //use to set pin low nrf_gpio_pin_set(28); //use to set pin high

    Currently this is how my code looks: gist.github.com/.../0838f1f71187e23a8fb7ac48fb002f13

Reply
  • I tried switching to TIMER0 and got the following error: 'TIMER0_INSTANCE_INDEX' undeclared here (not in a function).
    One important detail I should add is that I am actually using the ble_central/app_hrs project instead of the peripheral/pwm example. I just copied the main.c from pwm to app_hrs because the app_hrs was already configured to use a BLE nano. If I remember correctly I had to change some stuff in config/nrf_drv_config.h pertaining to the timer to get the project to compile.......

    I was able to set the pin to both high and low using the commands nrf_gpio_cfg_output(28); nrf_gpio_pin_clear(28); //use to set pin low nrf_gpio_pin_set(28); //use to set pin high

    Currently this is how my code looks: gist.github.com/.../0838f1f71187e23a8fb7ac48fb002f13

Children
No Data
Related