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

About several problems nrf51822 PWM

How do you do 1.In the Nordic \ nRF5_SDK_12 _f012efa 2.0-3 d \ examples \ peripheral here, there are three PWM routines: Low_power_pwm, pwm_driver, pwm_library but only low_power_pwm and pwm_library can be used for NRF51822 right?

2.Low_power_pwm pwm_library and what is the difference between the two routines?Use under what circumstances?

3.NRF51822 should I use to produce two lines of PWM waveform, and the need to control the frequency and duty ratio, two road PWM phase, which routines for use?As shown in figure image description

4.Using pwm_library this routine I set like this: app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, 3, 4); /* Switch the polarity of the second channel. */ 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);
	
	  app_pwm_config_t pwm2_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, 1, 2);
/* Switch the polarity of the second channel. */
pwm2_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;

/* Initialize and enable PWM. */
err_code = app_pwm_init(&PWM2,&pwm2_cfg,pwm_ready_callback);
APP_ERROR_CHECK(err_code);
app_pwm_enable(&PWM1);
  APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 0, 50));
  APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM2, 0, 50));

Duty ratio is 50%, but it isn't ,How do you set this and phase?

image description

5.If you use low_power_pwm this routine, phase how Settings?

uint32_t err_code; low_power_pwm_config_t low_power_pwm_config;

APP_TIMER_DEF(lpp_timer_0);
low_power_pwm_config.active_high    = false;
low_power_pwm_config.period         = 220;
low_power_pwm_config.bit_mask       = BSP_LED_0_MASK;
low_power_pwm_config.p_timer_id     = &lpp_timer_0;
low_power_pwm_config.p_port         = NRF_GPIO;

err_code = low_power_pwm_init((&low_power_pwm_0), &low_power_pwm_config, pwm_handler);
APP_ERROR_CHECK(err_code);
err_code = low_power_pwm_duty_set(&low_power_pwm_0, 20);
APP_ERROR_CHECK(err_code);

APP_TIMER_DEF(lpp_timer_1);
low_power_pwm_config.active_high    = false;
low_power_pwm_config.period         = 200;
low_power_pwm_config.bit_mask       = BSP_LED_1_MASK;
low_power_pwm_config.p_timer_id     = &lpp_timer_1;
low_power_pwm_config.p_port         = NRF_GPIO;

err_code = low_power_pwm_init((&low_power_pwm_1), &low_power_pwm_config, pwm_handler);
APP_ERROR_CHECK(err_code);
err_code = low_power_pwm_duty_set(&low_power_pwm_1, 150);
APP_ERROR_CHECK(err_code);

Thank you very much looking forward to your reply.

  • Hi,

    In the Nordic \ nRF5_SDK_12 _f012efa 2.0-3 d \ examples \ peripheral here, there are three PWM routines: Low_power_pwm, pwm_driver, pwm_library but only low_power_pwm and pwm_library can be used for NRF51822 right?

    That is correct. The nRF52-series have dedicated PWM hardware peripheral(used in the pwm_driver example), while in the nRF51-series the PWM libraries are implemented using GPIOTE, TIMERs and PPI hardware peripherals.

    Low_power_pwm pwm_library and what is the difference between the two routines?Use under what circumstances?

    The low-power PWM library is for applications where you don't need high frequency or accuracy. The low power pwm example should be suitable for LEDs, as LEDs usually do not require that high PWM frequency. But for peripherals that need a high accuracy, you should use the PWM library instead.


    What PWM frequency do you need?

    Using the APP_PWM_DEFAULT_CONFIG_2CH(), the first argument is the period in us, so with 5000 us, you get 200 Hz (1 / 0.005 = 200). You set the duty cycle with the app_pwm_channel_duty_set() function.

    With the PWM library you can try something like this.

         ret_code_t err_code;
    
        /* 2-channel PWM, 200Hz, output on DK LED pins. */
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, 1, 2);
    
        /* Switch the polarity of the second channel. */
        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);
        
        //Set duty cycle to 50% for first waveform
        while (app_pwm_channel_duty_set(&PWM1, 0, 50) == NRF_ERROR_BUSY);
        
        
        //Set duty cycle to 50% for second waveform
        while (app_pwm_channel_duty_set(&PWM1, 1, 50) == NRF_ERROR_BUSY);
    
Related