I don't understand how your code example works just yet. I'm neither a C or C++ wizard, so it makes it hard for me.
For what it was worth, I managed to run the interrupt at 4x and decode the states of A & B in forward and reverse. My code is longer and much uglier than yours, (and could be written more clearly) but at least I fully understand mine. By choosing a 4x rate, it guarantees the phases are correct independent of period. Looking at your code you do something similar, a lot more elegantly.
I used GPT1 and want to use GPT2. Should I change USE_GPT_PIT_150MHz to true in defaultConfig.h? If this change is made, can I enter 0.25us as an argument to beginPeriodic?
Thank your for the reference to EncSim, but if I could smoothly sweep the period by 250ns steps or so, I'd be perfectly happy to use what I have. When I need more sophistication, may migrate to EncSim.
Code:
#include "TeensyTimerTool.h"
using namespace TeensyTimerTool;
// for Teensy 4.1 only
Timer t1(GPT1);
int period;
int count;
volatile bool forward;
#define A 1
#define B 2
// Callbacks
void a_ns( )
{
//Serial.print("count = "); Serial.println(count);
if (count==0)
{
if (forward) { digitalWriteFast(A, HIGH); digitalWriteFast(B, LOW); }
else { digitalWriteFast(A, LOW); digitalWriteFast(B, HIGH); }
}
if (count==1)
{
if (forward) { digitalWriteFast(A, HIGH); digitalWriteFast(B, HIGH); }
else { digitalWriteFast(A, HIGH); digitalWriteFast(B, HIGH); }
}
if (count==2)
{
if(forward) { digitalWriteFast(A, LOW); digitalWriteFast(B, HIGH); }
else { digitalWriteFast(A, HIGH); digitalWriteFast(B, LOW); }
}
if (count==3)
{
digitalWriteFast(A, LOW); digitalWriteFast(B, LOW);
// same for both fwd and reverse
}
if ((count>3)|(count<0))
{
digitalWriteFast(A, LOW); digitalWriteFast(B, LOW);
Serial.println("Fatal Error");
}
count += 1;
if (count>3) count = 0;
}
void setup() {
// put your setup code here, to run once:
for(unsigned pin=1; pin<=2; pin++) pinMode(pin, OUTPUT);
Serial.begin(9600);
//while(!Serial) delay(100);
count = 0;
forward = true;
t1.beginPeriodic( a_ns, 1);
}
void loop() {
// put your main code here, to run repeatedly:
}