PWM drive: how to generate PWM signals that are phase shifted 90 degree

Hi

I want to generate two PWM signals that are phase shifted with 90 degree, with similar duty cycle. I have the following pwm configuration, but I don't know how to proceed further. I would be more than happy if someone could give me some help. I would appreciate if someone could send me a link to an example, if exists.

nrf_drv_pwm_config_t const pwm_cfg =
	{
		.output_pins =
        {
            9,		 																// channel 0
            10, 	 							             			// 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_1MHz,
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = 100,
        .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
        .step_mode    = NRF_PWM_STEP_AUTO
		
	};
	APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &pwm_cfg, 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[] = {25, 25, 0, 0};
		
		nrf_pwm_sequence_t const seq =
    {
        .values.p_individual = seq_values,
        .length          = NRF_PWM_VALUES_LENGTH(seq_values),
        .repeats         = 0,
        .end_delay       = 0
    };

		 nrf_drv_pwm_simple_playback(&m_pwm0, &seq, 5, NRF_DRV_PWM_FLAG_STOP);
 

p.s I am using sdk.v14

Thanks in advance

  • Hello, you should look at the pwm driver example. In the demo5 function there are four channels that are shifted 90 degrees to each other. This can be seen in the seq_values[] matrix.

  • 0
    10 pts.
    in reply to Hakon

    Hi Thanks...

    I just checked that using the oscilloscope and they look 90 degree shifted. But, I just realized that I am not getting a 25% duty cycle that I desired. I tried to put the following in the seq_values[]:

     static nrf_pwm_values_individual_t /*const*/ seq_values[] =
        {
            { 0x2000,      0,      0,      0 },
            {      0, 0x2000,      0,      0 },
            {      0,      0, 0x2000,      0 },
            {      0,      0,      0, 0x2000 }
        };

  • +1
    2192 pts.
    in reply to t_m

    static nrf_pwm_values_individual_t /*const*/ seq_values[] =
    {
        { 0x8000, 0x8000, 0x8000,      0 },
        { 0x8000, 0x8000,      0, 0x8000 },
        { 0x8000,      0, 0x8000, 0x8000 },
        {      0, 0x8000, 0x8000, 0x8000 }
    };

    With this matrix, all the channels will be phase shifted 90 degrees to each other and have 25 % duty cycle.

  • 0
    10 pts.
    in reply to Hakon

    That is right!

    Thank you Hakon.

    But I still want to understand how to set my duty cycle correctly for any other value than the usual 25%, 50%, 75%..etc. If I understood it correctly, the 0x8000 HEX value is the max. value of the CCR register (2^15). 

Related