schwingkopf:
just solved the problem.. the EDGPHA bit in the TC_BMR register controls whether the edges are detected on both PHA and PHB or just on PHA. I had this bit set to 0 before which according to the datasheet was the good choice.("0: edges are detected on both PHA and PHB. 1: edges are detected on PHA only.")
BUT!! the description in the datasheet is WRONG!! its inverted!
so the code that works fine is the following:
const int quad_A = 2;
const int quad_B = 13;
const unsigned int mask_quad_A = digitalPinToBitMask(quad_A);
const unsigned int mask_quad_B = digitalPinToBitMask(quad_B);
void setup() {
Serial.begin(115200);
delay(100);
// activate peripheral functions for quad pins
REG_PIOB_PDR = mask_quad_A; // activate peripheral function (disables all PIO functionality)
REG_PIOB_ABSR |= mask_quad_A; // choose peripheral option B
REG_PIOB_PDR = mask_quad_B; // activate peripheral function (disables all PIO functionality)
REG_PIOB_ABSR |= mask_quad_B; // choose peripheral option B
// activate clock for TC0
REG_PMC_PCER0 = (1<<27);
// select XC0 as clock source and set capture mode
REG_TC0_CMR0 = 5;
// activate quadrature encoder and position measure mode, no filters
REG_TC0_BMR = (1<<9)|(1<<8)|(1<<12);
// enable the clock (CLKEN=1) and reset the counter (SWTRG=1)
// SWTRG = 1 necessary to start the clock!!
REG_TC0_CCR0 = 5;
void loop() {
Serial.println(REG_TC0_CV0,DEC);
delay(500);
}
I tested the proposed code at up to 60 kHz, works great! But the index is not work at A7. Does anyone understand? My knowledge there is no longer enough.