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

send pwm to motor via button press

I am trying to have the dk (i have the 10036 preview) send pwm signals to a motor controller so that i can direct its speed. I have based a lot of the code below of thisutorial as well as some questions here in the dev zone but I can't seem to figure out why there's no signal going out.

I can get the motors to start, stop, and change direction but that's just by setting the right output pins which only happens if i comment out all the pwm code (init, start(), stop()), the motor spins at the factory default setting which isn't very high. Below is all the code i put in that pertains to pwm. Please point me in the right direction, thank you

APP_PWM_INSTANCE(PWM1,1);
// A flag indicating PWM status.
static volatile bool pwmReady = false;            

// PWM callback function
void pwm_ready_callback(uint32_t pwm_id)    
{
    pwmReady = true;
}


// Motor #1
int PWMA = 1; //Speed control 
int Motor1_1 = 12; //Direction
int Motor1_2 = 13; //Direction


void init_motors(void)
{
  // set up GPIOs
  nrf_gpio_cfg_output(Motor1_1);
  nrf_gpio_cfg_output(Motor1_2);
}


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)
{
		//Stop PWM
		app_pwm_disable(&PWM1);
		nrf_drv_gpiote_out_task_disable(PWMA);
}

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


// Function for handling the data from the Nordic UART Service.
static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, 
                             uint16_t length)
{
  if (strstr((char*)(p_data), RECORD)) {
		
  }
  else if (strstr((char*)(p_data), SHUFFLE)) {
    
  }
  else if (strstr((char*)(p_data), STOP)) {
    pwm_stop();
		nrf_drv_gpiote_out_task_disable(Motor1_1);
		nrf_gpio_cfg_output(Motor1_1);
		nrf_gpio_pin_clear(Motor1_1);
		nrf_drv_gpiote_out_task_disable(Motor1_2);
		nrf_gpio_cfg_output(Motor1_2);
		nrf_gpio_pin_clear(Motor1_2);
  }
  else if (strstr((char*)(p_data), PLAY)) {
		
  }
  else if (strstr((char*)(p_data), FORWARD)) {
    pwm_start();
		nrf_gpio_pin_clear(Motor1_1);
		nrf_gpio_pin_set(Motor1_2);
  }
  else if (strstr((char*)(p_data), REWIND)) {
    pwm_start();
		nrf_gpio_pin_set(Motor1_1);
		nrf_gpio_pin_clear(Motor1_2);
  }
}

Then in main loop

#define APP_TIMER_PRESCALER  0    /**< Value of the RTC1 PRESCALER register. */
#define APP_TIMER_MAX_TIMERS 6    /**< Maximum number of simultaneously created timers. */
#define APP_TIMER_OP_QUEUE_SIZE 4  /**< Size of timer operation queues. */

// Application main function.
int main(void)
{
    uint32_t err_code;

		
    // set up timers
    APP_TIMER_INIT(APP_TIMER_PRESCALER, 
                   APP_TIMER_OP_QUEUE_SIZE, false);
   
    // initlialize BLE
    ble_stack_init();
    gap_params_init();
    services_init();
    advertising_init();
    conn_params_init();
	

		PWMinit();
	
    // start BLE advertizing
    err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);

    // init GPIOTE
    err_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(err_code);

    // init PPI
    err_code = nrf_drv_ppi_init();
    APP_ERROR_CHECK(err_code);

    // intialize UART
    uart_init();

    // intitialize motors
    init_motors();
Parents
    1. When you use PWM library, I think it configures the gpio pins to output and you do not have to do it explicitly unless you need different drive currents.

    2. I see in your init_motors function that you need to control two motors but your PWM_Init function is only inlitializing pwm library with only one pin and one channel. You need to use APP_PWM_DEFAULT_CONFIG_2CH for controlling two pwm outputs.

    3. It is not acceptable to ignore return codes of functions like app_pwm_init. How would you know if this function has failed for some reason? please handle or atleast check if functions are returning with errors.

  • I did, but not initially. I do believe that i am successfully sending pwm signals to the motor controller (l293d) from the dk. I think the problem was that in this line app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(9000L, 15); i was telling it to go to the wrong pin on the motor controller. I changed the 15 to the pin go to one of the motor controller's input pins and it seems that go the signal across. The problem I am having now is that the motor only makes a humming sound when it is told to turn but I believe that is a problem with the amount of power I am using. I have a 1.5-3v motor and i am using the dk 5v pin to power both the motor controller logic and the motor, so i think the fix here would be to get additional power supply for the motor...right?

Reply
  • I did, but not initially. I do believe that i am successfully sending pwm signals to the motor controller (l293d) from the dk. I think the problem was that in this line app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(9000L, 15); i was telling it to go to the wrong pin on the motor controller. I changed the 15 to the pin go to one of the motor controller's input pins and it seems that go the signal across. The problem I am having now is that the motor only makes a humming sound when it is told to turn but I believe that is a problem with the amount of power I am using. I have a 1.5-3v motor and i am using the dk 5v pin to power both the motor controller logic and the motor, so i think the fix here would be to get additional power supply for the motor...right?

Children
No Data
Related