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

Parents
  • Hi Filip,

    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.

    You can refer to the Wheel module in nRF Desktop to see an example of using the Zephyr QDEC driver, and see how it is used in the nRF Desktop reference design. The QDEC peripheral is essentially the same on all nRF52 devices. Another option could be to use the low-level nrfx driver directly.

    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.

    The LED is an output, not input (to control the LED that shins through holes or similar in the rotating encoder). Some encoders has an index that gives you a fixed reference point, so that you can know the exact position of the wheel. That is not supported by the nRF QDEC peripheral, though (which target use case is something like a mouse wheel).

  • Hello!

    The LED is an output, not input (to control the LED that shins through holes or similar in the rotating encoder).

    Okay, I see. So in my case I would probably just disable this input then.

    I am currently trying to use the QDEC driver in the nrfx library: https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrfx/drivers/qdec/driver.html

    But I can't seem to get it to work. And there are no example codes for this either. Could you help me with a simple stup for an incremental encoder with channel A and B?

    Thank you.

    Filip

  • Hi Filip,

    The nRF5 SDK is distributed as a zip, you can download it from here: https://www.nordicsemi.com/Products/Development-software/nRF5-SDK/Download?lang=en#infotabs.

    Faws said:
    But when I try to define a struct in my program I get an error, is this how you would define it?

    This looks sensible. Which error do you get?

    Faws said:
    how would you define that handler to get meaningful data out of it?

    The handler should have a prototype like this:

    void your_qdec_handler(nrfx_qdec_event_t event);

    and the events are on this form:

    /** @brief QDEC event handler structure. */
    typedef struct
    {
        nrf_qdec_event_t  type; /**< Event type. */
        union
        {
            nrfx_qdec_sample_data_evt_t sample; /**< Sample event data. */
            nrfx_qdec_report_data_evt_t report; /**< Report event data. */
        } data;                                 /**< Union to store event data. */
    } nrfx_qdec_event_t;

  • 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).

Reply Children
No Data
Related