I am using the software PWM library (SDK12.0) with the DRV8834 to drive a bipolar stepper motor. Although the motor is working in both directions with the code below, the input for the STEP pin on the driver board wants the following:
#define STEP_PULSE(steps, microsteps, rpm) (60*1000000L/steps/microsteps/rpm) - detail in link below
Consequently, the PWM_library is not a good structure as the above values are in the opposite direction of duty cycle which is the main input for the PWM project.
#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "app_error.h"
#include "bsp.h"
#include "nrf_delay.h"
#include "app_pwm.h"
#include "nrf_drv_gpiote.h"
///* Using PWM
APP_PWM_INSTANCE(PWM1,1); // Create the instance "PWM1" using TIMER1.
int main(void)
{
ret_code_t err_code;
uint16_t MTR_DIR = 26;
uint16_t MTR1_STEP = 27;
nrf_gpio_cfg_output(MTR_DIR);
app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(5000L, MTR1_STEP);
// Switch the polarity of the first channel.
pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
// Initialize and enable PWM.
err_code = app_pwm_init(&PWM1,&pwm1_cfg,NULL);
APP_ERROR_CHECK(err_code);
app_pwm_enable(&PWM1);
uint8_t rpm_slow = 20;
uint8_t rpm_fast = 80;
while (true)
{
nrf_gpio_pin_write(MTR_DIR, 1);
while (app_pwm_channel_duty_set(&PWM1, 0, rpm_fast) == NRF_ERROR_BUSY);
nrf_delay_ms(1000);
//Reverse Motor
nrf_gpio_pin_write(MTR_DIR, 0);
while (app_pwm_channel_duty_set(&PWM1, 0, rpm_slow) == NRF_ERROR_BUSY);
nrf_delay_ms(1000);
}
}
My question is as follows:
Is there another example project that is more suitable for these popular drivers (they are all the same - DRV8825, A4988, DRV8880 etc) where the values are not the same as the PWM signal? Or would it be better to just explicitly control the timing, pin values, and pin states? I believe that is what the C++ driver for the boards is doing (link here).
Thanks in Advance