A simple example of using PWM with softdevice S110 using the Nordic nRF51-DK

A simple example of using PWM with softdevice S110 using the Nordic nRF51-DK. I have tried to remove all unnecessary layers (like the Nordic BSP support) to make this example simple to understand.

http://electronut.in/nrf51-pwm-test/

UPDATE:

I have now updated this example to use GPIOTE events also.

  • Not sure if you looked at my code, but I am doing pretty much everything you posted, and didn't run into any issue. I also mentioned nrf_drv_config.h in my article. (You might have missed that this was posted under 'blog' and not 'questions'.)

  • I had the same issue but used the HRM example (that uses the S110 so all the BLE stuff is enabled). You will need to enable the Timer 2 (Timer 0 and 1 are used by the example).

    In the file nrf_drv_config.H

      #define TIMER2_ENABLED 1
    

    Then in the main.c, add this (not inside of the void main{}):

    #include "app_pwm.h" // I had to include this or it would not work.
    APP_PWM_INSTANCE(PWM1,2);                   // Create the instance "PWM1" using TIMER2.  Timer0 used by S110 and Timer1 used by other BLE services (battery level...)
    
    
    static volatile bool pwm_change_ready_flag;            // A flag indicating PWM status.
    
    void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
    {
        pwm_change_ready_flag = true;
    }
    

    Add this into your void main(){}

    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, BSP_LED_0, BSP_LED_1);  //PWM on two LEDs.  
    //Switch to whatever port pin like  by switching BSP_LED_0 and BSP_LED_1 to  4,5 for P0.4 and P0.5
    
    	/* Switch the polarity of channels. */
            //    pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
    	//    pwm1_cfg.pin_polarity[0] = 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 the duty cycle to whatever you want from 0 to 100.
    int dutycycle = 50;  //50% dutycycle (50% brightness for an LED)
    
    while (app_pwm_channel_duty_set(&PWM1, 0, duty) == NRF_ERROR_BUSY);  //PWM instance, PWM number (0 or 1), duty cycle,  This sets the PWM1 instance of channel 0
    
    while (app_pwm_channel_duty_set(&PWM1, 1, duty) == NRF_ERROR_BUSY);  //adjust second PWM which is channel 1