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();