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

how to config the ADC and PWM when sample motor current at pwm rising edge?

NRF52833+SDK17:

I want to use the saadc to sample a DC brushed motor running current when pwm at rising edge, pwm driving the motor.

I can't understand the real meaning of NRF_DRV_PWM_EVT_FINISHED / NRF_DRV_PWM_EVT_END_SEQ0 / NRF_DRV_PWM_EVT_END_SEQ1 and NRF_DRV_PWM_EVT_STOPPED. for in the nrf_drv_pwm.h they are the same comment for explations(Macro for forwarding the new implementation)。

my code is following , i can't sure it's right.

now I don't know how to config the settings, anyone can help me? 

//-----------------------------------------------------------------------
void pwm0_init(void)
{

    uint32_t err_code;
	pwm0_free();
	
    nrf_drv_pwm_config_t const config0 =
    {
        .output_pins =
        {
				//MOTOR_CTRL_PIN_0 | NRF_DRV_PWM_PIN_INVERTED,
			MOTOR_CTRL_PIN_0 ,
        },
        .irq_priority = APP_IRQ_PRIORITY_LOW,
        .base_clock   = NRF_PWM_CLK_125kHz, 
        .count_mode   = NRF_PWM_MODE_UP, //NRF_PWM_MODE_UP,
        .top_value    = PWM_TOP_VALUE,
        .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
        .step_mode    = NRF_PWM_STEP_AUTO
    };
    //err_code = nrf_drv_pwm_init(&m_pwm0, &config0, NULL);
    err_code = nrf_drv_pwm_init(&m_pwm0, &config0, motor_current_detect_handler);
    APP_ERROR_CHECK(err_code);
    m_used |= USED_PWM(0);
    
    
    #ifdef MOTOR_CTRL_PIN_0
    m_pwm_seq_values.channel_0 = 0;
    #endif

    nrf_drv_pwm_simple_playback(&m_pwm0, &m_pwm_seq, 1, NRF_DRV_PWM_FLAG_LOOP| NRF_DRV_PWM_FLAG_SIGNAL_END_SEQ0);
}
//-------------------------------------------------------------------
void motor_current_detect_handler(nrf_drv_pwm_evt_type_t event_type)
{
	static uint8_t i = 0; 
	if (event_type == NRF_DRV_PWM_EVT_END_SEQ0) 
	{
		//NRF_LOG_DEBUG("motor current detect");
		{
			saadc_sampling_trigger();
			i = 0;
		}
	}
}

Parents Reply
  • I want to know how to to this, please?

    I want to sample during the PWM active pulse.  It appears to me right now the PWM logic in negative, so when the driving pin is low (active PWM) I want to sample the H-Bridge low side current resistor. I guess I'd also like to setup another SAADC channel to sample the PWM during the inactive time of the waveform, to measure back-EMF.   I want it ALL!

    Please?

Children
No Data
Related