This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Different polarity at 2 pins when using PWM.

My platform is SDK16 with softdevice, MCU nRF52840. I used 2 pin to generate PWM at 125KHz for Manchester coding. I want them are at different pin polarity. For example, if the pin_125K_OUT_PLUS at high state and the pin_125K_OUT_MINUS should at low state. To speed up the program for manchester coding that I created 2 functions of pwm_125k_enable and pwm_125k_disable. Fig-1 is current PWM output and Fig-2 is the prefer output. My code is as follows.

#define pin_125K_OUT_PLUS 24
#define pin_125K_OUT_MINUS 25

uint16_t pwm_playback_count = 1;

void PWM_Init(void)
{
nrf_drv_pwm_config_t const config0 =
{
.output_pins =
{
pin_125K_OUT_PLUS | NRF_DRV_PWM_PIN_INVERTED,
// pin_125K_OUT_MINUS,
pin_125K_OUT_MINUS | NRF_DRV_PWM_PIN_INVERTED,
},
.irq_priority = APP_IRQ_PRIORITY_LOWEST,
.base_clock = NRF_PWM_CLK_1MHz,
.count_mode = NRF_PWM_MODE_UP,
.top_value = 4,
.load_mode = NRF_PWM_LOAD_INDIVIDUAL,
.step_mode = NRF_PWM_STEP_AUTO
};
APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, NULL));
m_used |= USED_PWM(0);
}


void PWM_RF_125K_Enable(void)
{
static nrf_pwm_values_individual_t LF_125K_seq_values[] =
{
{ 4, 0, 0, 0 },
{ 0, 4, 0, 0 },
};
nrf_pwm_sequence_t const LF_125K_seq =
{
.values.p_individual = LF_125K_seq_values,
.length = NRF_PWM_VALUES_LENGTH(LF_125K_seq_values),
.repeats = 0,
.end_delay = 0
};
(void)nrf_drv_pwm_simple_playback(&m_pwm0, &LF_125K_seq, pwm_playback_count, NRF_DRV_PWM_FLAG_LOOP);
}

inline void pwm_125k_enable(void)
{
// enable PWM output, need to initialize PWM output before run here
*((volatile uint32_t *)((volatile uint8_t *)m_pwm0.p_registers + (pwm_playback_count ? NRF_PWM_TASK_SEQSTART1 : NRF_PWM_TASK_SEQSTART0))) = 0x1UL;
pwm_125k_state = 1;
}

inline void pwm_125k_disable(void)
{
// disable PWM output
*((volatile uint32_t *)((volatile uint8_t *)m_pwm0.p_registers + NRF_PWM_TASK_STOP)) = 0x1UL;
pwm_125k_state = 0;
}

Fig-1

Fig-2

Parents Reply Children
Related