Cant run simple PWM output on NRF5340

Hi All!

I have a problem running simple PWM output on NRF5340 board. I have a following code:

static nrf_pwm_values_individual_t values;
static nrf_pwm_sequence_t sequences;
const tPtHalPwm_device* nordic_device = device_get_binding( "PWM_0" );
const tPtHalPwmNrfxConfig *config = nordic_device->config;
config->initial_config.base_clock = NRF_PWM_CLK_125kHz;
config->initial_config.count_mode = NRF_PWM_MODE_UP;
config->initial_config.load_mode = NRF_PWM_LOAD_INDIVIDUAL;
config->initial_config.step_mode = NRF_PWM_STEP_AUTO;
config->initial_config.top_value = 62;
nrfx_pwm_uninit( &config->pwm );
nrfx_err_t res = nrfx_pwm_init( &config->pwm,
                                &m_ptHalPwm_configs[id],
                                pwmEventHandler,
                                ( void* ) 0 );
values.channel_0 = ( 50UL | 1UL << 15 ); // High pulses

sequences.values.p_individual = &values;
sequences.length = NRF_PWM_VALUES_LENGTH(values);
sequences.repeats = 0;
sequences.end_delay = 0;

nrfx_pwm_simple_playback( &config->pwm, &sequences, 1, NRFX_PWM_FLAG_LOOP );

Now what i'm trying to achieve is to get 50% of duty cycle with frequency of 2 kHz. Just simple wave coming out of pin in PWM_0.

What i'm currently getting is indeed almost perfect 2 kHz frequency wave, but with duty cycle of 80%?

I was trying to test other duty cycle values, but results on the scope is following;

1% -> 1,6%

5% -> 8%

20% -> 32%

40% -> 64%

60% -> 96%

More than 70% -> always 100%.

Could you please give me a clue what is wrong with following code?

Parents
  • So a compare value of 50 when the top value is 62 should give a duty cycle around 80% so looks correct (assuming the values are not converted to duty cycle in the driver). If this is correct, set the values.channel to 50% of the initial_config.top_value.

  • This is from Nordic SDK:

    nrf_pwm_values_t values; ///< Pointer to an array with duty cycle values. This array must be in Data RAM.
    /**< This field is defined as an union of pointers
    * to provide a convenient way to define duty
    * cycle values in various loading modes
    * (see @ref nrf_pwm_dec_load_t).
    * In each value, the most significant bit (15)
    * determines the polarity of the output and the
    * others (14-0) compose the 15-bit value to be
    * compared with the pulse generator counter. */

    So basically youre trying to tell me that they're actually expecting compare value not duty cycle (which normally is denoted in percentage)?
    If so, i suggest better naming here

Reply
  • This is from Nordic SDK:

    nrf_pwm_values_t values; ///< Pointer to an array with duty cycle values. This array must be in Data RAM.
    /**< This field is defined as an union of pointers
    * to provide a convenient way to define duty
    * cycle values in various loading modes
    * (see @ref nrf_pwm_dec_load_t).
    * In each value, the most significant bit (15)
    * determines the polarity of the output and the
    * others (14-0) compose the 15-bit value to be
    * compared with the pulse generator counter. */

    So basically youre trying to tell me that they're actually expecting compare value not duty cycle (which normally is denoted in percentage)?
    If so, i suggest better naming here

Children
No Data
Related