nRF5 SDK is not maintained anymore
More Info: Consider nRF Connect SDK for new designs
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to use 52840 Qdec correctly

I have checked the qDEC example, I don't know what it is doing, so I can directly say what I need. I connected a Hall sensor to two pins of 52840, and configured QDEC. I set the sampling rate to 128us, and turned off the sampling report interruption, because the report is interrupted with the minimum of 10 samples. Means it will break after 1.28ms, so I'm going to poll the ACC register directly to read the pulse count in the main loop, configured as follows:

void qdec_init(void)
{
	nrfx_qdec_config_t p_config;
	p_config.dbfen = true; 
	p_config.interrupt_priority = 2; 
	p_config.ledpol = NRF_QDEC_LEPOL_ACTIVE_LOW ;
	p_config.ledpre = 0; 
	p_config.psela = 0x27; 
	p_config.pselb = 0x26;
	p_config.pselled = 0xffffffff; 
	p_config.reportper = NRF_QDEC_REPORTPER_DISABLED  ;  
	p_config.sampleper = NRF_QDEC_SAMPLEPER_128us; 
	p_config.sample_inten = false;
	
 
	ret_code_t err_code = nrfx_qdec_init(&p_config,NULL); 
	APP_ERROR_CHECK(err_code);
  
	nrf_drv_qdec_enable();//

}

I read the ACC register at 1ms in main() :

     nrfx_qdec_disable();
     R1_Handle->Encoder.Acc    = (float)nrf_qdec_acc_get();
     R1_Handle->Encoder.Accdbl = (float)nrf_qdec_accdbl_get();
     nrfx_qdec_enable();

My Hall has 24 pulses in a lap, after QDEC I get 48 pulses in a lap, and it shows fine in the ACC register.

So my question: is it necessary to call nrfx_qdec_disable before reading ACC register? Because if I hadn't called nrfx_qdec_disable , the acc value wouldn't have changed. Or can you provide the correct practice for polling acc register values exactly?

I am mainly used for speed measurement, do you have a better way to help me complete this design? I expect to send the resulting speed through the UART at 1khz. thank you

  • Hi 

    The recommended way to handle this is to first activate the READCLRACC task. This will move the current state of the ACC and ACCDBL registers into the ACCREAD and ACCDBLREAD registers, and clear ACC and ACCDBL, in an atomic way. 

    Then you can read the ACCREAD and ACCDBLREAD registers afterwards, without worrying that the registers will change while you are busy reading them out. 

    By following this method there is no need to disable and re-enable the driver, and you ensure that you will not lose any pulses. 

    Best regards
    Torbjørn

Related