Hi,
Schematic:
No ESD protection, no filters, no schmitt tirggers. Are you sure the signals are reliable?
Did you check the sensor signals?
Maybe
* the signal is ringing
* the voltage levels don´t match your circuit
* the rise/fall rate is too slow
... causing multiple transitions, where one expects one clean edge.
--> The first thing you need are "clean signals". Else every additional transition may cause the direction bit to toggle.
*******
Code:
It says nothing how the "direction" is generated. Just one line:
Code:
RotateDirection = EQep1Regs.QEPSTS.bit.QDF;
You should have some datasheet/description how the signal is generated.
You are free to add code that includes some hysteresis to position and direction.
This may be one key to reduce write rate: Don´t write the new value as soon as it changes, but write it only after the value has changed by a pre defined threshold.
*******
The problem here is, because I am developing impedance matching tuner , the capacitor position keeps changing quickly while matching.
In that case I need to keep the current position every single time.
I wonder why you use a mechanically adjustable capacitor. Why no solution without mechanics? Varicap, switched capacitors... but I don´t know your capacitor range (200pF...2000pF?) and resolution.
I don´t understand the "power down problem". You say it is a impedance matching tuner, where the capacitor value has to change quickly.
So on power down, you surely loose tracking. After power up (it may be after 100ms or after one week) it has to find the tracking point anyways... unimportant how the value was just before power down.
There is the Z-signal at the encoder: Do you use it?
******
But I haven't included power saving mechanism to my controller and don't have much time to redesign.
I don´t think that adding a NVRAM at SPI or I2C and adding a small piece of code is a big task.
******
Now, we don't have any information where the capacitor is
If you use the Z signal you surely can know where the capacitor was after power up:
* Run the motor and count the encoder pulses until you find the Z signal.
* after that you may change direction of the motor and move the capacitor back the same count of pulses to the "power up" position... or to any other position.
I think there are a couple of ways to solve your problem. But the problem won´t solve itself. You have to do it. And yes - it will be some manageable effort. Just try it.
Klaus