Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nrf52832 pwm

I want two pwm channel output 1MHZ frequency, the phase difference between two PWM channels is 180 degrees。when i set two pwm frequency to 500KHZ, use the configure below, the pwm is right.
nrf_drv_pwm_config_t const config0 =
{
.output_pins =
{
EMS_BRIDGE_1_PIN, // channel 0
EMS_BRIDGE_2_PIN, // channel 1
NRF_DRV_PWM_PIN_NOT_USED, // channel 2
NRF_DRV_PWM_PIN_NOT_USED // channel 3
},
.irq_priority = APP_IRQ_PRIORITY_LOWEST,
.base_clock = NRF_PWM_CLK_16MHz,
.count_mode = NRF_PWM_MODE_UP,
.top_value = 16,
.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);

// This array cannot be allocated on stack (hence "static") and it must
// be in RAM (hence no "const", though its content is not changed).
static nrf_pwm_values_individual_t seq_values[] =
{
0xE, 0x7FFF
};

static nrf_pwm_values_individual_t seq_values1[] =
{
0x7FFF, 0xE
};

nrf_pwm_sequence_t const seq =
{
.values.p_individual = seq_values,
.length = NRF_PWM_VALUES_LENGTH(seq_values),
.repeats = 0,
.end_delay = 0
};

nrf_pwm_sequence_t const seq1 =
{
.values.p_individual = seq_values1,
.length = NRF_PWM_VALUES_LENGTH(seq_values1),
.repeats = 0,
.end_delay = 0
};

nrf_drv_pwm_complex_playback(&m_pwm0, &seq, &seq1, 1, NRF_DRV_PWM_FLAG_LOOP);

but when i modify ".top_value = 16," to " .top_value = 8,", "seq_values[]" from "0xE, 0x7FFF" to "0x6, 0x7FFF"; and "seq_values1[]" from "0x7FFF, 0xE" to "0x7FFF, 0x6", the pwm output is not correct, just like below:
nrf_drv_pwm_config_t const config0 =
{
.output_pins =
{
EMS_BRIDGE_1_PIN, // channel 0
EMS_BRIDGE_2_PIN, // channel 1
NRF_DRV_PWM_PIN_NOT_USED, // channel 2
NRF_DRV_PWM_PIN_NOT_USED // channel 3
},
.irq_priority = APP_IRQ_PRIORITY_LOWEST,
.base_clock = NRF_PWM_CLK_16MHz,
.count_mode = NRF_PWM_MODE_UP,
.top_value = 8,
.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);

// This array cannot be allocated on stack (hence "static") and it must
// be in RAM (hence no "const", though its content is not changed).
static nrf_pwm_values_individual_t seq_values[] =
{
0x6, 0x7FFF
};

static nrf_pwm_values_individual_t seq_values1[] =
{
0x7FFF, 0x6
};

nrf_pwm_sequence_t const seq =
{
.values.p_individual = seq_values,
.length = NRF_PWM_VALUES_LENGTH(seq_values),
.repeats = 0,
.end_delay = 0
};

nrf_pwm_sequence_t const seq1 =
{
.values.p_individual = seq_values1,
.length = NRF_PWM_VALUES_LENGTH(seq_values1),
.repeats = 0,
.end_delay = 0
};

nrf_drv_pwm_complex_playback(&m_pwm0, &seq, &seq1, 1, NRF_DRV_PWM_FLAG_LOOP);

why?

Parents Reply Children
  • Hi Dmitry, thanks for your reply! I have solved this problem by myself!

    The correct configure code as follows:

    nrf_drv_pwm_config_t const config2 =
    {
    .output_pins =
    {
    EMS_BRIDGE_1_PIN, // channel 0
    EMS_BRIDGE_2_PIN, // channel 1
    NRF_DRV_PWM_PIN_NOT_USED, // channel 2
    NRF_DRV_PWM_PIN_NOT_USED // channel 3
    },
    .irq_priority = APP_IRQ_PRIORITY_LOWEST,
    .base_clock = NRF_PWM_CLK_16MHz,
    .count_mode = NRF_PWM_MODE_UP_AND_DOWN,
    .top_value = 8,
    .load_mode = NRF_PWM_LOAD_INDIVIDUAL,
    .step_mode = NRF_PWM_STEP_AUTO
    };

    APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm2, &config2, NULL));

    // This array cannot be allocated on stack (hence "static") and it must
    // be in RAM (hence no "const", though its content is not changed).
    static nrf_pwm_values_individual_t seq_values[] =
    {
    0x8002, 0x6
    };

    nrf_pwm_sequence_t const seq =
    {
    .values.p_individual = seq_values,
    .length = NRF_PWM_VALUES_LENGTH(seq_values),
    .repeats = 0,
    .end_delay = 0
    };

    (void)nrf_drv_pwm_simple_playback(&m_pwm2, &seq, 1, NRF_DRV_PWM_FLAG_LOOP);

    The correct pwm wave as follow chart:

Related