I have been attempting to use Channel2 as a clock to determine speed and I believe I finally got it.
The code below is from the initial program I posted (Reply #32) but with Channel2 enabled and set to TCLK4. I also added more remarks and cleaned it up a bit.
Index interrupts must be enabled for the speed portion to work.
/*
Quadrature Encoder with Marker via hardware
*/
volatile int z_Total=0;
volatile double TPR=0;
void setup() {
Serial.begin(115200);
delay(100);
// Setup Quadrature Encoder
REG_PMC_PCER0 = (1<<27)|(1<<28)|(1<<29); // activate clock for TC0,TC1,TC2
// select XC0 as clock source and set capture mode
REG_TC0_CMR0 = 5;
//Resets the count every index. Rem out the line above,
//and use the line below instead.
//REG_TC0_CMR0 = (1<<0)|(1<<2)|(1<<8)|(1<<10);
REG_TC0_CMR2 = (1<<0)|(1<<1); // Set to TCLK4
// activate quadrature encoder and position measure mode, no filters
REG_TC0_BMR = (1<<8)|(1<<9)|(1<<12);
// Enable the clock (CLKEN=1) and reset the counter (SWTRG=1)
REG_TC0_CCR0 = 5;
REG_TC0_CCR1 = 5;
REG_TC0_CCR2 = 5;
//Remark out these 4 lines to disable Index interrupts
REG_TC0_CMR1 = (1<<8); // Set rising edge of Z
REG_TC0_IER1=0b10000000; // enable interrupt on Z
REG_TC0_IDR1=0b01111111; // disable other interrupts
NVIC_EnableIRQ(TC1_IRQn);
}
void loop() {
//REG_TC0_CV0 Stores count from encoder
Serial.println(REG_TC0_CV0);
//REG_TC0_CV1 Stores count from index if interrupts are off
//if interrupts are on, CV1 will always reset every cycle to zero.
if (REG_TC0_CV1 == 0)
{
Serial.print(z_Total);
Serial.println(" -With Int");
}
else
{
Serial.print(REG_TC0_CV1);
Serial.println(" -No Int");
}
//TPR holds the quantity of ticks every Index. Index interrupts must be on.
//TCLK4 is set so 128 is the divisor.
Serial.print((F_CPU/128/TPR)*60);Serial.println(" RPM");
Serial.println("-------");
delay(500);
}
void TC1_Handler() {
//This won't fire unless the Index interrupt is enabled above
z_Total++;
long dummy=REG_TC0_SR1; // vital - reading this clears some flag
// otherwise you get infinite interrupts
TPR=REG_TC0_CV2; //Store ticks
REG_TC0_CCR2 = 4; //Reset counter
}