I am trying to understand the operation of the PWM in nRF52832.
I am directly writing to the registers. I want to create a 4000Hz square wave with a 50% duty cycle.
My code so far does not work. I am missing something. Please help.
My code is below:
// *************************************************************
// Example of using PWM by direct write to registers (no driver)
//
// Create a PWM square wave of 4000 Hz with a 50% duty cycle
// Output waveform in only one GPIO (pin 17)
//
// Using DK board PCA10040 (with nRF52832)
// Using SDK 13.1.0
//
void pwm_direct_init(void)
{
uint16_t pwm_seq[1] = {16};
NRF_PWM_Type *pwm = NRF_PWM0;
nrf_gpio_cfg_output(17); // LED_1 on DK board
pwm->PSEL.OUT[0]= 17;
pwm->ENABLE = PWM_ENABLE_ENABLE_Enabled;
//pwm->SHORTS = 7;
pwm->MODE = PWM_MODE_UPDOWN_Up; // Default
pwm->COUNTERTOP = 32;
pwm->PRESCALER = PWM_PRESCALER_PRESCALER_DIV_128; // 125 kHz
pwm->DECODER = PWM_DECODER_LOAD_Individual | \
(PWM_DECODER_MODE_RefreshCount << PWM_DECODER_MODE_Pos);
pwm->LOOP = 0;
pwm->SEQ[0].PTR = (uint32_t) pwm_seq; // Duty cycle 32/2 = 16 (50%)
pwm->SEQ[0].CNT = 1;
pwm->SEQ[0].REFRESH = 0;
pwm->SEQ[0].ENDDELAY = 0;
//pwm->SEQ[1].PTR = (uint32_t) pwm_seq; // Duty cycle
//pwm->SEQ[1].CNT = 1;
//pwm->SEQ[1].REFRESH = 0;
//pwm->SEQ[1].ENDDELAY = 0;
//pwm->PSEL.OUT[1] =
//pwm->PSEL.OUT[2] =
//pwm->PSEL.OUT[3] =
//pwm->ENABLE = PWM_ENABLE_ENABLE_Enabled;
pwm->TASKS_SEQSTART[0] = 1;
}