The simplest use of PWM driver

Hello all!

I would like to use the PWM driver, yes, the hardware that is on board the chip. To get going certain code parts from the PWM driver that comes with the SDK, namely those 5 demos were copied to my project, namely

/* PWI instance ID. */
static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);
#define PWM_INSTANCE_ID 0

// PWM init
 static void pwm_init()
{
     nrf_drv_pwm_config_t const config =
     {
         .output_pins =
         {
             17, // channel 0
             NRF_DRV_PWM_PIN_NOT_USED, // channel 1
             NRF_DRV_PWM_PIN_NOT_USED, // channel 2
             NRF_DRV_PWM_PIN_NOT_USED  // channel 3
         },
         .irq_priority = APP_IRQ_PRIORITY_LOWEST,
         .base_clock   = NRF_PWM_CLK_125kHz,
         .count_mode   = NRF_PWM_MODE_UP,
         .top_value    = 12500,
         .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
         .step_mode    = NRF_PWM_STEP_AUTO
     };
     APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config, pwm_handler));
 }

The idea is to dim and fade the LEDs that are on the following GPIOs.

/* LEDs GPIOs */
#define GPIO_PIN_25_green 25  // green
#define GPIO_PIN_24_red 24    // red
#define GPIO_PIN_23_yellow 23 // yellow
#define GPIO_PIN_22_white 22  // white

However, I am not sure how to use the PWM handler, namely does the whole idea of the PWM needs to be defined here or somewhere else? The idea is to simply use the PWM to UP and DOWN, namely to dim the LED. For example, if that would be 8bit driver that would go from 0 to 255 with a simple for loop, and for diming that other way around. So, the question is do, I have to implement that into the PWM handler or somewhere else? This is my PWM handler so far. Indeed, there is nothing there yet?

// PWM instance handler
 static void pwm_handler(nrf_drv_pwm_evt_type_t event_type)
{
    switch(event_type){
       case // EVENT ID:
           break;
       default;
           break;
    }
 }

The idea of the PWM should be used once a certain values of a particular variable goes above 1, for example,

if (a>1){ start  PWM "UP" ... decreasing the size of the width

and / or

if (a<1){ start  PWM "DOWN" ... increasing the size of the width}

Any ideas how to go about this?

Best.

  • Hi,

    In most cases the PWM handler it isn't necessary to use. In the PWM_driver example the button handler is used to switch between different PWM sequences when a button is pressed. The PWM handler is mostly used to signalize the state of the PWM peripheral such when a sequence has been played. Here is an example that varies the duty cycle/intensity of the onboards LEDs. It's made for NCS, but it shouldn't be that hard to port it to nRF5SDK.

    regards

    Jared 

Related