• AVR Freaks

Hot!dspic33CK256MP506- QEI1 resets the encoder counter every 32768 pulses

Author
JJAYAR
Starting Member
  • Total Posts : 34
  • Reward points : 0
  • Joined: 2011/11/30 20:40:39
  • Location: 0
  • Status: offline
2020/05/17 23:42:45 (permalink)
0

dspic33CK256MP506- QEI1 resets the encoder counter every 32768 pulses

Have configured QEI1 for quadrature encoder read
For some reason the 32 bit position register resets itself every 32768 pulses
long int ReadEncoder1()
{
int lowCurrentPosition=0;
long int currentPosition=0;
long int temp0=0;
long int temp1=0;
lowCurrentPosition = POS1CNTL;
temp0 = POS1HLD;
temp1 = temp0<<16 ;
currentPosition = (temp1 + lowCurrentPosition);
return currentPosition;
}
 
 
This is how I am setting up the QEI1 module
RP64 = A, RP65 = B & RP66=I
void SetupEncoder1()
{
//ENCODER CONTROL REGISTER
QEI1CONbits. QEIEN = 0;//disable the encoder
QEI1CONbits.PIMOD = 0;//INDEX BIT does not effect counter
QEI1CONbits.QEISIDL = 0;//CONTINUE WHEN IDLE #13
QEI1CONbits.IMV = 0;//INDEX BIT does not effect counter
QEI1CONbits.INTDIV = 0x0; //freq divisor
QEI1CONbits.CNTPOL = 1;//counter is positive by default
QEI1CONbits.GATEN = 0;//gate not enabled
QEI1CONbits.CCM = 0;//quadrature
QEI1IOCbits.QCAPEN = 0;//home event does not capture position
QEI1IOCbits.FLTREN = 1;//filter enabled
QEI1IOCbits.QFDIV = 0x2;//filter freq divide
QEI1IOCbits.OUTFNC = 0;//out function disabled
QEI1IOCbits.SWPAB=0;//do not swap AB
//signal polarities
QEI1IOCbits.HOMPOL=0;//home polarity not inverted
QEI1IOCbits.IDXPOL=0;//index polarity not inverted
QEI1IOCbits.QEAPOL=0;//A polarity not inverted
QEI1IOCbits.QEBPOL=0;//B polarity not inverted
//
QEI1IOCbits.INDEX=1;
QEI1IOCbits.HOME=1;
QEI1IOCbits.QEA=1;
QEI1IOCbits.QEB=1;
//
QEI1STATbits.POSOVIEN=1;//position counter overflow interrupt enable
QEI1STATbits.VELOVIEN=1;//vel counter overflow

//Interrupt
IFS3bits.QEI1IF = 0;//clear long int flag
IEC3bits.QEI1IE = 0;//keep disabled interrupt


//ASSIGN ENCODER PINS
RPINR14bits.QEIA1R = 64;//RP64 = encoder input pin A
RPINR14bits.QEIB1R = 65;//RP65 = encoder input pin B
RPINR15bits.QEINDX1R = 66;//RP66 = encoder input pin I

//INitialize all registers
POS1HLD = 0X00;//pos hold reg
POS1CNTL = 0X00;//posn count low
POS1CNTH = 0x00;//posn count hi

INDX1HLD = 0x00;//index count hold reg
INDX1CNTH = 0x00;//index count high reg
INDX1CNTL = 0x00;//index count low reg

VEL1CNT = 0X0;
INT1TMRH = 0x00;//interval timer Hi bit
INT1TMRL = 0x00;//interval timer Lo bit
INT1HLDH = 0x00;//interval timer hold High
INT1HLDL = 0x00;//interval timer hold Lo
QEI1ICH = 0x00;//Initialization/capture HI
QEI1ICL = 0x00;//Initialization/capture Lo

QEI1CONbits.QEIEN = 1;//enable the encoder
}
Was wondering if anyone else has encountered such a situation
Any comments appreciated
Thank you in advance
 
Jay
 
#1

10 Replies Related Threads

    ric
    Super Member
    • Total Posts : 28321
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: dspic33CK256MP506- QEI1 resets the encoder counter every 32768 pulses 2020/05/18 00:06:28 (permalink)
    0
    JJAYAR
    For some reason the 32 bit position register resets itself every 32768 pulses

    It is really important to state "how do you know this is happening?"
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    Antipodean
    Super Member
    • Total Posts : 1904
    • Reward points : 0
    • Joined: 2008/12/09 10:19:08
    • Location: Didcot, United Kingdom
    • Status: offline
    Re: dspic33CK256MP506- QEI1 resets the encoder counter every 32768 pulses 2020/05/18 03:22:46 (permalink)
    0
    ric
    JJAYAR
    For some reason the 32 bit position register resets itself every 32768 pulses

    It is really important to state "how do you know this is happening?"
     

     
    I'm guessing the encoder counter uses a 16 bit signed register ... which will automagically roll over on +/-32768.
    But then it sounds like he hasn't read the data sheet to find out.
     

    Do not use my alias in your message body when replying, your message will disappear ...

    Alan
    #3
    oliverb
    Super Member
    • Total Posts : 286
    • Reward points : 0
    • Joined: 2009/02/16 13:12:38
    • Location: 0
    • Status: offline
    Re: dspic33CK256MP506- QEI1 resets the encoder counter every 32768 pulses 2020/05/18 03:50:04 (permalink)
    5 (1)
    I've a suspicion that the code to combine two ints into a long is failing.
    The expression "temp0<<16" may need to be cast to unsigned.
     
    Edit: or better still unsigned long. Also the final add should be a bitwise-or in order to avoid sign-bit problems.
    post edited by oliverb - 2020/05/18 05:46:15
    #4
    ric
    Super Member
    • Total Posts : 28321
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: dspic33CK256MP506- QEI1 resets the encoder counter every 32768 pulses 2020/05/18 03:58:38 (permalink)
    0
    Indeed. As usual, the problem is most likely in the code that was not posted.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    JJAYAR
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2011/11/30 20:40:39
    • Location: 0
    • Status: offline
    Re: dspic33CK256MP506- QEI1 resets the encoder counter every 32768 pulses 2020/05/18 12:23:10 (permalink)
    0
    Hi Ric- thanx for the reply.  I am reading the 32bit counter register at regular intervals as the motor shaft is rotated
    & storing posn values in an array
    I transfer array to excel to see values & graph
    - i can see it value rise to about 32k & then drop down to zero & repeat as I rotate shaft
    #6
    JJAYAR
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2011/11/30 20:40:39
    • Location: 0
    • Status: offline
    Re: dspic33CK256MP506- QEI1 resets the encoder counter every 32768 pulses 2020/05/18 12:26:34 (permalink)
    0
    Hi Alan, Thanx for the reply
    The data sheet does mention process to read 32 bit counter register (lower 16 bit & higher 16bit)
    However I did not see any mention of 16bit signed register mode
    Can you please specify where you might have seen this.  Thanx for your help
     
    #7
    ric
    Super Member
    • Total Posts : 28321
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: dspic33CK256MP506- QEI1 resets the encoder counter every 32768 pulses 2020/05/18 13:15:41 (permalink)
    0
    JJAYAR
    Hi Ric- thanx for the reply.  I am reading the 32bit counter register at regular intervals as the motor shaft is rotated
    & storing posn values in an array

    Once again, SHOW US THE CODE.
    This is most lilkely where the error is occuring.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    Antipodean
    Super Member
    • Total Posts : 1904
    • Reward points : 0
    • Joined: 2008/12/09 10:19:08
    • Location: Didcot, United Kingdom
    • Status: offline
    Re: dspic33CK256MP506- QEI1 resets the encoder counter every 32768 pulses 2020/05/18 17:11:13 (permalink)
    0
    JJAYAR
    Hi Alan, Thanx for the reply
    The data sheet does mention process to read 32 bit counter register (lower 16 bit & higher 16bit)
    However I did not see any mention of 16bit signed register mode
    Can you please specify where you might have seen this.  Thanx for your help
     

     
    I DON'T KNOW that is the problem, but it looks like it is.
     
    As others keep saying SHOW US YOUR CODE for SETTING UP AND READING the QEI registers. Until we see that we are just guessing, like I guessed it is a 16 bit signed register.
     

    Do not use my alias in your message body when replying, your message will disappear ...

    Alan
    #9
    JJAYAR
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2011/11/30 20:40:39
    • Location: 0
    • Status: offline
    Re: dspic33CK256MP506- QEI1 resets the encoder counter every 32768 pulses 2020/05/18 18:27:18 (permalink)
    0
    Hi Alan
    My first email has code for both (read & register setup- about 50 lines of code!) - Is it not showing up in the mail when you read?
    added code for copying currentPosn to an array here
     
    long int count;
    ..
    while(1)
    {
          count = ReadEncoder1();
          if(count >= 0)
         {
            Blue(1);//light up led once motor shaft starts turning
            if(i < (dataArraySize-2))
            {
               posn = count;
            }
          i++;
         Delay(4200);//add delay so motor gets a chance to turn
       }
       else
       Blue(0);//shut led
     
    }//end while 1
    #10
    ric
    Super Member
    • Total Posts : 28321
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: dspic33CK256MP506- QEI1 resets the encoder counter every 32768 pulses 2020/05/18 18:33:19 (permalink)
    0
    Where is the code for "ReadEncoder1()" ?
    Edit: Sorry, it is in your first post
     
    (Note the code in your previous post does not display correctly because you did not put "code" tags around it.
    The index [   i   ] is interpreted as "start italics" by the forum if you don't.
    just put a single
    [CODE]
    before your block of code, and [/CODE] after it, but change to lower case ( "code" and "/code" between the square braces) to activate them.
    post edited by ric - 2020/05/18 18:45:17

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #11
    Jump to:
    © 2020 APG vNext Commercial Version 4.5