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

Enable pwm with button click

I would like to start a pwm signal on PIN 15 while BUTTON_1 or BUTTON_2 is pressed. If BUTTON_1 or BUTTON_2 is released, than there should be no signal(low) on PIN 15.

Setup is:

-SoftDevice 8.0

-SDK 8.1

-nRF51DK

There is a related post to this topic: devzone.nordicsemi.com/.../

The problem is, if I comment out app_pwm_enable(&PWM1); in the PWMinit function then I cannot connect to the board and the buttons are also not working.

If I enable the pwm in the PWMinit function, then the pwm will start immediately. But the pwm should only start when BUTTON_1 or BUTTON_2 are pressed.

static void PWMinit(void)
{
  app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(9000L, 15);
  pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
  app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
  app_pwm_enable(&PWM1);
}

void pwm_stop(void)
{
   app_pwm_disable(&PWM1);
   nrf_drv_gpiote_out_task_disable(15);
   nrf_gpio_pin_clear(15);
}

void pwm_start(void)
{   
    nrf_drv_gpiote_out_task_enable(15); 
    app_pwm_enable(&PWM1);
    while (app_pwm_channel_duty_set(&PWM1, 0, 50) == NRF_ERROR_BUSY);
}

static void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
    if(button_action == APP_BUTTON_PUSH)
    {
	    switch (pin_no)
        {
            case 	Switch_up:
								pwm_start();
								nrf_gpio_pin_set(23);			
								break;

					case 	Switch_down:
								pwm_start();
								nrf_gpio_pin_set(24);			
								break;
        
					default:

								break;
        }
    }
	
    else if (button_action == APP_BUTTON_RELEASE)
    {
        switch (pin_no)
        {
            case 	Switch_up:
								pwm_stop();
								nrf_gpio_pin_clear(23);			
								break;
					
					case 	Switch_down:
								pwm_stop();
								nrf_gpio_pin_clear(24);			
								break;
					
        default:
								
								break;
        }
    }
 }

Motorsteurung.zip

Related