Quadrature Encoder Interface (QEI)

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

Post Reply
PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times

Quadrature Encoder Interface (QEI)

Post by PicoPuls »

It would be extremely interesting if the Quadrature Encoder Interface (QEI) could be used on these 16 bit chips.

dsPIC30F3011
dsPIC33EP512

With only two input pins, it is possible to tell the position of a motor, tracing it backwards and forwards
IF possible also reset, making it 3 pins,

Can anyone tell what C-code should be inserted into Flowcode7 to make that happen ?

http://ww1.microchip.com/downloads/en/D ... 70208A.pdf


Best regards
BO

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times

Re: Quadrature Encoder Interface (QEI)

Post by QMESAR »

Hi.

It is not so easy to give you C code that works directly with your application as it depends on factors such as how many pulse per Rev you have is it something rotational or linear movement ? do you want to measure speed or detect position all of this is not possible for us to know as we do not have any information regarding your encoder and your plant to which you connect the encoder attached is an example I used with a dsPIC30F6010A where I measured the angular speed of a Motor shaft it does give you an idea of how you should initialize the QEI module but again that setup must fit your encoder and application and then you have to do something like I did in the C component to calculate what you need from the value you captured in the QEI registers
Attachments
QEI.rar
(26.44 KiB) Downloaded 568 times

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times

Re: Quadrature Encoder Interface (QEI)

Post by PicoPuls »

Thanks for QEI.rar
The first C command in INIT QEI is

ADPCFG |= 0x0038; // Configure QEI pins as digital inputs

I made an image to try to understand the logic flow from datasheet to code
Is this understanding correct ? Or is it more correct if AN3...AN5 are green ?
Once I get the basic idea how to read the datasheet, it should be possible to apply it to other chips.
QEA, QEB and INDX are exactly the pins needed for a superfast QEI
where INDX resets the counter

Best regards
BO
Attachments
dsPIC30F6010A_forum.jpg
(146.8 KiB) Downloaded 2341 times

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times

Re: Quadrature Encoder Interface (QEI)

Post by QMESAR »

As the QEI pins can be used as Analog ,digital and QEI pins there is a Analog pin select register that connect the pin either to the Analog to digital converter or to the digital circuits /modules
as you will see from your posted diagram QEA can be QEI A input or it can be Analog input pin AN4 and then it can also be a normal Digital pin RB4 therefore if you set the corresponding ADPCFG bit to Zero(0) then the pin connect to the Analog to digital converter(AN4) in this case you also have to set the corresponding TRIS bit to a 1
then if you set the ADPCFG bit to 1 then you connect the pin to the Digital circuits and by configuring the QEI you will have the signal in the QEI module

That is the basics of this register ADPCFG

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times

Re: Quadrature Encoder Interface (QEI)

Post by QMESAR »

I would also suggest you read the Family reference manual before you read the datasheet as the Reference manual give a better explanation of the modules and then you need to check that the device you are working with has all the features as described in the Reference manual

For dsPIC30F this is the document number (DS70046).

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times

Re: Quadrature Encoder Interface (QEI)

Post by QMESAR »

Here is a document for the QEI it is a very good reference I used it to do my application
I hope this helps you :D
Attachments
Measure speed and Position QEI.pdf
(287.36 KiB) Downloaded 603 times

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times

Re: Quadrature Encoder Interface (QEI)

Post by PicoPuls »

I will take a deep look at SpeedPosition.pdf Thanks !

In this image I try to understand the calculation.

Variables like FCV_POSCNTCOPY, FCV_ANGPOS_1
do they have to be declared somewhere or can Flowcode handle that automatically ?
I guess that they are also globally available in all other flowcode Icons in the project !?

Best regards
BO
Attachments
dsPIC30F6010A_forum3.jpg
(219.54 KiB) Downloaded 2321 times

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times

Re: Quadrature Encoder Interface (QEI)

Post by QMESAR »

Variables like FCV_POSCNTCOPY, FCV_ANGPOS_1
do they have to be declared somewhere or can Flowcode handle that automatically ?
I guess that they are also globally available in all other flowcode Icons in the project !?
These variable are declared under the Flowcode Variables just click the variable tab and you will see them however to reference to them in a C component they must have the prefix FCV_ and then the varaible name are in Capital letters

Code: Select all

 so in variables you will see  AngPos_1  in the C block you reference to this variable with FCV_ANGPOS_1
See the Flowcode help file for these Flowcode specifics and Basics
I guess that they are also globally available in all other flowcode Icons in the project !?
They are globally available to all code segments

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times

Re: Quadrature Encoder Interface (QEI)

Post by PicoPuls »

Hi
I made this large image for better understanding before trying to apply this to an other chip : dsPIC30F3011
Best regards
BO
Attachments
dsPIC30F6010A_forum2.jpg
(584.04 KiB) Downloaded 2314 times

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times

Re: Quadrature Encoder Interface (QEI)

Post by QMESAR »

It looks correct There might be some difference in Registers when moving to 30F3011 however the datasheet will show you this

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times

Re: Quadrature Encoder Interface (QEI)

Post by PicoPuls »

Hi
I am also making a try to apply these QEI principles to dsPIC33EP512MU810 It has 2 QEI's
http://ww1.microchip.com/downloads/en/D ... 70616g.pdf

I use this as reference : dsPIC30F6010A
It has 4 pins partly dedicated to QEI

pin
17 INDX Quadrature Encoder Index Pulse input.
16 QEA Quadrature Encoder Phase A input in QEI mode.Auxiliary Timer External Clock/Gate input in Timer mode.
15 QEB Quadrature Encoder Phase B input in QEI mode.
69 UPDN Position Up/Down Counter Direction State.

The strange thing is that the much bigger chip dsPIC33EP512MU810 with 2 QEI's
seems to have no pins at all dedicated to QEI

Where do they hide ???

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times

Re: Quadrature Encoder Interface (QEI)

Post by PicoPuls »

Trying to study the datasheet reveals
http://ww1.microchip.com/downloads/en/D ... f#page=101

Here I am totally lost - how to get access to QEI1 and how to pick the right pins
Can anyone indicate how this is translated to C-code to initialize the chip ?

Best regards
BO
Attachments
PERIPHERAL PIN SELECT INPUT REGISTER MAP.jpg
(168.73 KiB) Downloaded 2289 times

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times

Re: Quadrature Encoder Interface (QEI)

Post by QMESAR »

The QEI pins should be able to map to any of RPi pins this is different for each family you have to read the peripheral pin select reference manual and the datasheet carefully

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times

Re: Quadrature Encoder Interface (QEI)

Post by QMESAR »

Looking at the datasheet Page 211 shows the input pins and the register to which a certain peripheral maps to
in your case QEI1 maps to RPINR14,then page 234 shows you this register and the binary number you should write in the RPINR14 to map QEIA or QEIB

The code would look something like this provided you have select "allow multiple remapping" in the configuration

Code: Select all

RPINR14bits.QEA1R = 0b1111111  // This map the A signal to RP pin 127
You have to deceide to which pins you want to connect the QEI and then map the QEI to these pins
Attachments
2.JPG
2.JPG (28.32 KiB) Viewed 15598 times
1.JPG
1.JPG (54.07 KiB) Viewed 15598 times

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times

Re: Quadrature Encoder Interface (QEI)

Post by PicoPuls »

If a first build is made with Device: PIC16.33E.33EP512MU810
and just with an empty loop
the system should still show include files etc..

First part of the resulting c-code :

Code: Select all

#define MX_PIC16
#define MX_CAL_PIC16
#define MX_CLK_SPEED 12000000
#define FCP_NULL Unconnected_Port
#include <xc.h>
#include <math.h>
#include <stdlib.h>
#include "p33Exxxx.h"
//Configuration Start
_FGS(65535);
_FOSCSEL(65535);
_FOSC(65471);
_FWDT(65535);
_FPOR(65535);
_FICD(65535);
_FAS(65535);
//Configuration End
How does the compiler know that it is a 33EP512MU810 -device ?
Just curious why ....bits. is added to form RPINR14bits.QEA1R = 0b1111111
The definition is in include file p33EP512MU810.h
#define _QEA1R RPINR14bits.QEA1R

meaning that it would be equally correct but less readable (?) to write _QEA1R=0b1111111
in the Flowcode Init Icon,

An other small detail
RPINR14bits.QEA1R = 0b1111111 // This map the A signal to RP pin 127

I think there is no RP127, It should be RPI27

If suspicion is right, Could this be correct ?
RPINR14bits.QEA1R = 0b0011011 // This map the A signal to RPI pin 27

It seems to be RPI for input and RP for output(?)

It would be fun and useful is this will work - still much missing probably,
Anyway the reposition of pins seems super smart
Even a virtual connection between output and input is possible without any physical pin

Best regards
BO

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times

Re: Quadrature Encoder Interface (QEI)

Post by PicoPuls »

What I hope now is that these commands connect the inputs of QEI1 to the real pins 7,8,9

How many more c-commands are necessary to initialize
in order to run a first real test ?

I guess , reading the counter in a loop would confirm the functionality.

Best regards
BO
Attachments
follow002.jpg
(331.7 KiB) Downloaded 2261 times

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times

Re: Quadrature Encoder Interface (QEI)

Post by PicoPuls »

Commands like
RPINR15bits.lNDX1R=0b0110100
initialize the processor and creates the internal connections

Can this command also be given during execution to change the way the processor is wired internally ?
Can these registers be read during execution for checks and diagnoses ?

Best regards
BO

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times

Re: Quadrature Encoder Interface (QEI)

Post by QMESAR »

PicoPuls wrote:
An other small detail
RPINR14bits.QEA1R = 0b1111111 // This map the A signal to RP pin 127

I think there is no RP127, It should be RPI27
In my opinion you should be careful with this statement as in my understanding you can map RP pins to outputs and inputs but RPI pins can only be mapped as inputs
therefore I think my statement needs no correction as my examples does not refer to your exact chip it refers to a general description therefore you have to see what pins you have and what you want to use with the information given to you

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times

Re: Quadrature Encoder Interface (QEI)

Post by PicoPuls »

QUESTIONS:
......... provided you have select "allow multiple remapping" in the configuration

I have been looking under BUILD / project options
and BUILD /compiler options
and VIEW /Global Options

Where can it be found?

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times

Re: Quadrature Encoder Interface (QEI)

Post by QMESAR »


PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times

Re: Quadrature Encoder Interface (QEI)

Post by PicoPuls »

Is there any sample code to jump-start the configuration of dsPIC33EP512MU810 ?

I just want to do two things:

-> Connect Quadrature Encoder Functionality , QEA1, QEB1,Index1 to pin 7,8,9

-> and define 8 analog inputs and 19 digital outputs

That's all,

Code: Select all

This is the desired disposition
Pin 
 25 AN00  (B00 )
 24 AN01  (B01 )
 23 AN02  (B02 )
 22 AN03  (B03 )
 21 AN04  (B04 )
 20 AN05  (B05 )
 33 AN09  (B09 )
 43 AN14  (B14 )

 83 RD06  (RD06)
 84 RD07  (RD07)
 68 RD08  (RD08)
 69 RD09  (RD09)
 70 RD10  (RD10)
 71 RD11  (RD11)
 79 RD12  (RD12)
 80 RD13  (RD13)

 18 RE08  (RE08)
 19 RE09  (RE09)

 87 RF00  (RF00)
 88 RF01  (RF01)
 52 RF02  (RF02)
 49 RF04  (RF04)
 50 RF05  (RF05)
 53 RF08  (RF08)
 40 RF12  (RF12)
 39 RF13  (RF13)

 90 RG00  (RG00)

  7 RPI50 (C02 ) QEA1      RPINR14             QEA1R<6:0>           50 QEI1 Phase A(2)                                   RPINR14bits.QEA1R=0b0110010
  8 RPI51 (C03 ) QEB1      RPINR14             QEB1R<6:0>           51 QEI1 Phase A(2)                                   RPINR14bits.QEB1R=0b0110011
  9 RPI52 (C04 ) INDX1     RPINR15             INDX1R<6:0>          52 QEI1 Index'2'                                     RPINR15bits.INDX1R=0b0110100

Trying to write Init QEI
The compiler seems to accept this code -actually only 3 active lines.

Code: Select all

// C Code
// C Code:
/***********************************************************
  Initialize The QEI 
***********************************************************/

RPINR15bits.INDX1R=0b0110100;
RPINR14bits.QEA1R=0b0110010;
RPINR14bits.QEB1R=0b0110011;

/* A number of detailed QEI settings needed here. Correct syntax?? 
QCAPEN=0b1;   //Position Counter Input Capture Enable bit
FLTREN=0b1;   //Digital Filter Enable bit 
QFDIV=0b011;  //Digital Input Filter Clock Divide Select bits
etc....  
*/


/*Just To compare with working Init-Code  in dsPIC30F6010A

ADPCFG |= 0x0038; // Configure QEI pins as digital inputs

QEICONbits.QEIM = 0; // Disable QEI Module
QEICONbits.CNTERR = 0; // Clear any count errors
QEICONbits.QEISIDL = 0; // Continue operation during sleep
QEICONbits.SWPAB = 0; // QEA and QEB not swapped
QEICONbits.PCDOUT = 0; // Normal I/O pin operation
QEICONbits.POSRES = 1; // Index pulse resets position counter
DFLTCONbits.CEID = 1; // Count error interrupts disabled
DFLTCONbits.QEOUT = 1; // Digital filters output enabled for QEn pins
DFLTCONbits.QECK = 5; // 1:64 clock divide for digital filter for QEn
DFLTCONbits.INDOUT = 1; // Digital filter output enabled for Index pin
DFLTCONbits.INDCK = 5; // 1:64 clock divide for digital filter for Index
POSCNT = 0; // Reset position counter
QEICONbits.QEIM = 6; // X4 mode with position counter reset by Index

*/

Post Reply