I am using the PWM library in this repository for a nRF51822. I write the following code to generate a ~ 30% dutycycle 500 kHz PWM output:
#define V_CLK 5
static void v_clock_start(void)
{
// Init PWM
nrf_pwm_config_t pwm_config = PWM_DEFAULT_CONFIG;
pwm_config.mode = PWM_MODE_500kHz; // PRESCALER of 0, pwm_max_value of 31
pwm_config.num_channels = 1;
pwm_config.gpio_num[0] = V_CLK;
// Initialize the PWM library
nrf_pwm_init(&pwm_config);
// Start PWM
nrf_pwm_set_value(0, 10);
}
And I use
nrf_pwm_set_enabled(false);
to stop PWM. The enum type PWM_MODE_500kHz (self-defined) corresponds to PRESCALER of 0 and pwm_max_value of 31, making the PWM frequency 500 kHz. I enable and disable PWM periodically based on a timer, and monitor the output with an oscilloscope. I can see for the majority of time, the PWM has correct dutycycle ~30%. However, from time to time, the dutycycle is inversed (~70%), and I can see the complementary PWM waveform. It happens regularly and randomly. I am just wondering what can cause such a problem.