Using the QDEC driver with a incremental encoder.

Hello!

I have an incremental encoder such as this one: https://www.digikey.no/en/products/detail/cui-devices/AMT103-2048-N4000-S/10468125

I have to make this work with the nRF52832.

After some research I see that the nRF connect sdk has a QDEC driver, which I assume is for dealing with these kinds of encoders. Looking through the documentation and exaples I am not sure how to set this up properly.

First of all: when creating the example code from zephyr, the nRF52 is not listed as compatible, is this right? I can't find an exaple using nrfx or for nordic boards.

Second: there are three pins coming from the encoder, channel A, B and index. The documentation only uses channels a and b, then one input for LED. I don't understand what this LED input is for.

Thanks for the assistance!

Filip

  • Hello!

    This looks sensible. Which error do you get?

    I get some error about incomplete type, I am not sure what it means. But I fixed it by just removing the struct, and it worked. Guessing it has something to do about it being a typedef.

    I have currently defined the event handler like this:

    static void qdec_event_handler(nrfx_qdec_event_t event)
    {
    	acc_data = event.data.report.acc;
    	accdbl_data = event.data.report.accdbl;
    }

    But I am not sure how to get the data out of this, I have attached the encoder to voltage from the DK, and correct pins on the DK. But I don't get any meaningfull data, only 0 from both acc_data and accdbl_data. How do you get information about how many pulses the encoder has given? And is there a way to only call the handler when there is change in the encoder, to save system resources?

    Thank you for the assistance!

  • Hi,

    Faws said:
    How do you get information about how many pulses the encoder has given?

    The number of transitions are in the ACC register, which you get in event.data.report.acc. (You also see double transitions in accdbl, refer to the QDEC chapter in PS for details on that.). If you get the event called regularly (the interval is defined by your configured report period), the QDEC peripheral is operational. Could it be that you are using the wrong pins? Or not rotating the encoder?

    Faws said:
    And is there a way to only call the handler when there is change in the encoder, to save system resources?

    You can enable interrupt for sample ready event (SAMPLERDY). That is done by the sample_inten flag in the configuration struct.

    By the way, a simpler example using the old (but very similar) API is attached to this thread: https://devzone.nordicsemi.com/f/nordic-q-a/77632/quadrature-decoder-settings-for-motor-encoder

  • Hello!

    This information is very helpfull!

    I saw from both the post you linked, and from the old nRF5 SDK that the event handler uses a NRF_QDEC_EVENT_REPORTRDY. I cannot seem to find out what this is in the new nRF connect sdk or what it really does. I assume that it checks if there is a report that is ready and then gets the accumulated transitions. What is the corresponding macro in the new sdk?

    Thank you.

  • Hi,

    NRF_QDEC_EVENT_REPORTRDY is used in the same way in nrfx in both SDKs (in fact nrfx itself is the same in both nRF Connect SDK and the nRF Connect SDK, except for version differences in nrfx itself). Also, NRF_QDEC_EVENT_REPORTRDY just represents the EVENTS_REPORTRDY register. In essence, this event is used t give you an interrupt for every new report, and the frequency of the reports is configured by the REPORTPER register, which gets the value you write to the reportper field in the configuration struct (nrfx_qdec_config_t).

Related