hi : I want 4 channel PWM output, the following code doesn't work, I want to know where is wrong
#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "app_error.h"
#include "bsp.h"
#include "nrf_delay.h"
#include "app_pwm.h"
static volatile bool ready_flag; // A flag indicating PWM status.
static volatile bool ready_flag1; // A flag indicating PWM status.
void pwm_ready_callback(uint32_t pwm_id) // PWM callback function
{
ready_flag = true;
}
//void pwm_ready_callback1(uint32_t pwm_id) // PWM callback function
//{
// ready_flag1 = true;
//}
int main(void)
{
ret_code_t err_code;
APP_PWM_INSTANCE(PWM1,1); // Create the instance "PWM1" using TIMER1.
/* 2-channel PWM, 200Hz, output on DK LED pins. */
app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, 0, 1);
pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
/* Switch the polarity of the second channel. */
pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
/* Initialize and enable PWM. */
err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
APP_ERROR_CHECK(err_code);
app_pwm_enable(&PWM1);
APP_PWM_INSTANCE(PWM2,2);
/* 2-channel PWM, 200Hz, output on DK LED pins. */
app_pwm_config_t pwm2_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, 30, 5);
/* Switch the polarity of the second channel. */
pwm2_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
pwm2_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
/* Initialize and enable PWM. */
err_code = app_pwm_init(&PWM2,&pwm2_cfg,pwm_ready_callback);
APP_ERROR_CHECK(err_code);
app_pwm_enable(&PWM2);
uint32_t value;
ready_flag = false;
/* Set the duty cycle - keep trying until PWM is ready... */
while (app_pwm_channel_duty_set(&PWM1, 0, 50) == NRF_ERROR_BUSY);
/* ... or wait for callback. */
while (!ready_flag);
ready_flag = false;
/* Set the duty cycle - keep trying until PWM is ready... */
while (app_pwm_channel_duty_set(&PWM1, 1, 50) == NRF_ERROR_BUSY);
/* ... or wait for callback. */
while (!ready_flag);
ready_flag = false;
/* Set the duty cycle - keep trying until PWM is ready... */
while (app_pwm_channel_duty_set(&PWM2, 0, 50) == NRF_ERROR_BUSY);
/* ... or wait for callback. */
while (!ready_flag);
ready_flag1= false;
/* Set the duty cycle - keep trying until PWM is ready... */
while (app_pwm_channel_duty_set(&PWM2, 1, 50) == NRF_ERROR_BUSY);
/* ... or wait for callback. */
while (!ready_flag);
while (true)
{
nrf_delay_ms(25);
}
}