Can you please present a complete code example that demonstrates your problem?
Actually my mind is very tired now and I've tried a lot of things. I'm exhausted.
Actually the problem is easy. I'm trying to measure the speed of a small motor using an incremental encoder which I've done many times before. In this case I only use A channel to measure the speed, A and B channel to measure the position. Using QEI, position measurement is not a problem. And there should be no problem with the speed measurement but I'd have many adventures, it is a long story, nevermind.
I've remapped home input same as the A signal to measure the period of the A pulse since QEI module has no interrupt for QEA and QEB signals, and I decided to use HOME signal for this purpose (I've also tried INDX with the same result). To measure the time, I used timer2:timer3 (Don't ask why I'm not using interval timer, velocity feature, input capture etc, As I've said it is a long story). At every interrupt I clear the timer, and at the other interrupt I capture the timer value. Precision is enough for me.
The idea works but occasionaly I get wrong values. 1 or 2 in a 500 reads. The pulse period is constant and ~670µs.
I first suspected that the module does not generate the interrupt correctly sometimes but then I created 2 arrays to keep the captured values to be sure it is not a timer problem. And the test resulted as I've described in my first post.
Then I compared the position values at every interrrupt. The position value difference must be the same everytime and saw that when I get wrong values also the difference value differs. Which shows that the interrupt is generated at the wrong time.
I could not related two, I'm confused, I'm tired and I simply ignore values as a solution when the position difference value is not what expected for now.
Thank you very much.
void __attribute__ ( ( interrupt, no_auto_psv ) ) _ISR _QEI1Interrupt( void )
uint32_t countVal = 0xFFFFFFFF;
uint16_t countValUpper = 0;
uint16_t countValLower = 0;
uint32_t countVal2 = 0xFFFFFFFF;
uint16_t countValUpper2 = 0;
uint16_t countValLower2 = 0;
if(QEI1STATbits.HOMIRQ == 1)
countValLower = TMR2;
countValUpper = TMR3HLD;
countVal = (((uint32_t)countValUpper << 16) | countValLower);
// calculate speed
speed = 70000000ul / countVal;
// read 2nd time
countValLower2 = TMR2;
countValUpper2 = TMR3HLD;
countVal2 = (((uint32_t)countValUpper2 << 16) | countValLower2);
capArray[indx] = countVal ;
capArray2[indx] = countVal2 ;
if(++indx > 499)
indx = 0;
IFS3bits.QEI1IF = 0;