Author Topic: fast 1/2 ch. quadrature decoder A/B+index, RP2040, Arduino  (Read 390 times)

0 Members and 1 Guest are viewing this topic.

Offline mino-fmTopic starter

  • Regular Contributor
  • *
  • Posts: 145
  • Country: de
fast 1/2 ch. quadrature decoder A/B+index, RP2040, Arduino
« on: March 13, 2024, 09:49:46 am »
With its PIO units, the RP2020 installed on the Pico board offers the possibility of implementing one or two 4-fold quadrature decoders with a high count rate, so that signal changes of up to 1 x 10^7 edges/s can be recorded. Furthermore, the index pulse can be evaluated by position or rotary encoders, which sets the internal counter to 0 or a freely selectable default value.

The circuit consists of the finished RP2040 pico board, to whose inputs GPIO10 and GPIO11 for channel 0 the two phases A/B of the encoder signal are connected (GPIO14+15 for channel 1). The index pulse is expected at GPIO12 (GPIO13 at channel 1) and is active with a '0' level in the program shown. If '1' level is required, the GPIO pin can be reprogrammed with 'set_index0_pol(1)'.

If the encoder signals deliver levels > 3.3 V, voltage dividers or at least protective resistors must be inserted into the signal lines.
The demo program ‘Pico-qcnt.ino’ shows examples for two channels 'init_qcnt0(0)' and 'init_qcnt1(0)', is written for the Arduino IDE and outputs the current counter reading when changed via the USB interface. The .uf2 file can be written directly to the Pico board in boot mode without using an Arduino IDE. The program module 'pio_qcnt0.c' or 'pio_qcnt1.c' can be transferred to other IDEs. If the file extension is renamed to '*.ino' for reasons of compatibility with Arduino, 'rp2040_arduino.h' must be used to avoid duplicate definitions.
http://mino-elektronik.de/progs/RP2040/Pico-qcnt.zip
 

Offline MarkT

  • Frequent Contributor
  • **
  • Posts: 367
  • Country: gb
Re: fast 1/2 ch. quadrature decoder A/B+index, RP2040, Arduino
« Reply #1 on: March 14, 2024, 09:00:17 pm »
Ah, handy... Thanks for sharing.
 

Offline mino-fmTopic starter

  • Regular Contributor
  • *
  • Posts: 145
  • Country: de
Re: fast 1 - 4 ch. quadrature decoder A/B(+index), RP2040, Arduino
« Reply #2 on: March 18, 2024, 11:30:47 am »
2024-03-18: Expansion to 1 - 4 channels
In the program shown previously, the function for the second channel was implemented by simply copying from PIO1_SM0 to PIO0_SM0. It was quick, but not particularly efficient.

In order to have PIO0 freely available again for other applications, PIO1_SM1 is now used and two additional channels using PIO1_SM2 and PIO1_SM3 are activated. A total of four quadrature decoders are available: Pico-qcnt4x.zip

Channels 2 and 3 are reduced in function as examples. This eliminates the need for an INDEX signal, which saves one input pin, and only uses one DMA channel for data acquisition. The retriggering of the DMA_TRANS_COUNT is done via software.
Basically, this could be achieved via an interrupt after 2^32-1 changes have expired, here it is done by resetting the counter with each function call 'get_qcnt2()' or 'get_qcnt3()'.

For demonstration purposes, the maximum clock frequency was also reduced to 1/10. Like the GPIOx and DMA_CHx used, you can set this differently as you wish.
http://mino-elektronik.de/progs/RP2040/Pico-qcnt4x.zip
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf