51Views0Replies

Author Options:

Stepper motor working with nrf51dk (problem with timer i think) Answered

Hello everyone,
I am trying to make a program that makes work a stepper motor 28BYJ-48, my stepper motor is well connected to the ULN2003 and the ULN2003 is well connected to my nrf51dk, here is a picture of my setup :

https://coeleveld.com/arduino-stepper-uln2003a/

I foloowed this tutorial and it worked with my Arduino, so I guess the problem is not coming from there.

I am now making a program using the nrf51dk, I am using nrf51_sdk_10.0.0 and the ble_app_uart as an example, my project is to control the motor via Bluetooth.

But for the moment I just would like to make the motor runing.

so I follow the same principe of code as in the tutorial for making it work.

I need a timer, so I also followed the "Application timer tutorial" from nordic :

devzone.nordicsemi.com/.../application-timer-tutorial

here is my code that I Added to the ble_app_uart project :

//At the top I define the differents gpios that I will have to use
//and step_number that is a variable use to navigate threw the differents steps for the motor to work int step_number = 0; #define motorPin1 1 #define motorPin2 2 #define motorPin3 3 #define motorPin4 4 #define APP_TIMER_PRESCALER 15 /**< Value of the RTC1 PRESCALER register. */ #define APP_TIMER_OP_QUEUE_SIZE 5 /**< Size of timer operation queues. */ //I add 4 different timers, one for each pin APP_TIMER_DEF(timer_step1); APP_TIMER_DEF(timer_step2); APP_TIMER_DEF(timer_step3); APP_TIMER_DEF(timer_step4); . . . //the code already defined in the existing project
//Timeout handler for the repeated timer
//I need 4 timeout handler, one foreach motorPin
static void timer_a_handler(void * p_context)
{
  nrf_drv_gpiote_out_toggle(motorPin1);
}
static void timer_b_handler(void * p_context)
{
  nrf_drv_gpiote_out_toggle(motorPin2);
}
static void timer_c_handler(void * p_context)
{
  nrf_drv_gpiote_out_toggle(motorPin3);
}
static void timer_d_handler(void * p_context)
{
  nrf_drv_gpiote_out_toggle(motorPin4);
}
//Then I create the timers
// Create timers
static void create_timers()
{   
    uint32_t err_code1;
  uint32_t err_code2;
  uint32_t err_code3;
  uint32_t err_code4;
    // Create timers
    err_code1 = app_timer_create(&timer_step1,
                                APP_TIMER_MODE_REPEATED,
                                timer_a_handler);
    APP_ERROR_CHECK(err_code1);
 err_code2 = app_timer_create(&timer_step2,
                                APP_TIMER_MODE_REPEATED,
                                timer_b_handler);
    APP_ERROR_CHECK(err_code2);
 err_code3 = app_timer_create(&timer_step3,
                                APP_TIMER_MODE_REPEATED,
                                timer_c_handler);
    APP_ERROR_CHECK(err_code3);
 err_code4 = app_timer_create(&timer_step4,
                                APP_TIMER_MODE_REPEATED,
                                timer_c_handler);
    APP_ERROR_CHECK(err_code4);
}
//And here is my main
//with the loop to make work the motor the same way I did with the arduino, but adapting the code
int main(void)
{
  uint32_t err_code;
    uint32_t err_code1;
  uint32_t err_code2;
  uint32_t err_code3;
  uint32_t err_code4;
    bool erase_bonds;
    uint8_t  start_string[] = START_STRING;
    
    // Initialize.
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
    uart_init();
    buttons_leds_init(&erase_bonds);
    ble_stack_init();
    gap_params_init();
    services_init();
    advertising_init();
    conn_params_init();
    
    printf("%s",start_string);
    err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
    create_timers();
   
 
  nrf_gpio_pin_dir_set(motorPin1, NRF_GPIO_PIN_DIR_OUTPUT);
  nrf_gpio_pin_dir_set(motorPin2, NRF_GPIO_PIN_DIR_OUTPUT);
  nrf_gpio_pin_dir_set(motorPin3, NRF_GPIO_PIN_DIR_OUTPUT);
  nrf_gpio_pin_dir_set(motorPin4, NRF_GPIO_PIN_DIR_OUTPUT);
 
    // Enter main loop.
  
    for (;;)
    {
   uint8_t str1[1] = "1";
   uint8_t str2[1] = "2";
   uint8_t str3[1] = "3";
   uint8_t str4[1] = "4";
   switch(step_number){
    case 0:
     
     err_code1 = app_timer_start(timer_step1, APP_TIMER_TICKS(200, APP_TIMER_PRESCALER), NULL);
     APP_ERROR_CHECK(err_code1);
     err_code2 = app_timer_stop(timer_step2);
     APP_ERROR_CHECK(err_code2);
     err_code3 = app_timer_stop(timer_step3);
     APP_ERROR_CHECK(err_code3);
     err_code4 = app_timer_stop(timer_step4);
     APP_ERROR_CHECK(err_code4);
      
    ble_nus_string_send(&m_nus, str1, strlen((char*)str1));
    break;
    case 1:
     err_code1 = app_timer_stop(timer_step1);
     APP_ERROR_CHECK(err_code1);
     err_code2 = app_timer_start(timer_step2, APP_TIMER_TICKS(200, APP_TIMER_PRESCALER), NULL);
     APP_ERROR_CHECK(err_code2);
     err_code3 = app_timer_stop(timer_step3);
     APP_ERROR_CHECK(err_code3);
     err_code4 = app_timer_stop(timer_step4);
     APP_ERROR_CHECK(err_code4);
    ble_nus_string_send(&m_nus, str2, strlen((char*)str2));
    break;
    case 2:
     err_code1 = app_timer_stop(timer_step1);
     APP_ERROR_CHECK(err_code1);
     err_code2 = app_timer_stop(timer_step2);
     APP_ERROR_CHECK(err_code2);
     err_code3 = app_timer_start(timer_step3, APP_TIMER_TICKS(200, APP_TIMER_PRESCALER), NULL);
     APP_ERROR_CHECK(err_code3);
     err_code4 = app_timer_stop(timer_step4);
     APP_ERROR_CHECK(err_code4);
    ble_nus_string_send(&m_nus, str3, strlen((char*)str3));
    break;
    case 3:
     err_code1 = app_timer_stop(timer_step1);
     APP_ERROR_CHECK(err_code1);
     err_code2 = app_timer_stop(timer_step2);
     APP_ERROR_CHECK(err_code2);
     err_code3 = app_timer_stop(timer_step3);
     APP_ERROR_CHECK(err_code3);
     err_code4 = app_timer_start(timer_step4, APP_TIMER_TICKS(200, APP_TIMER_PRESCALER), NULL);
     APP_ERROR_CHECK(err_code4);
    ble_nus_string_send(&m_nus, str4, strlen((char*)str4));
    break;
   }
   step_number++;
  
   if(step_number > 3){
    step_number = 0;
   }
 
      
   
      
   
        power_manage();
    }
}

Probably I am doing Something wrong with the timer, because when I modify it and decide to use it with a led for testing like in the tutorial, the motor makes noise but doesn't move and the led works.
If someone can help, it's been hours I am on this problem and I can't find my mistakes.

Tags:nrf51dk

Discussions