resetting quadrature encoder on Due

I'm having trouble resetting the encoder counter on my Due in a timely way.
What happens when I execute the following program is that I get the correct data in the first loop. When that is complete, I disable the clock, re-enable the clock and the software trigger, CLKEN and SWTRG. This is supposed to reset the counter.
However, the counter, CV0, doesn't reset until the encoder is subsequently actually rotated. In other words, the value of CV0 keeps the value it had at the end of the first loop. It will keep this value in the second loop until a position change occurs.

Here's my code:

void setup()
{
Serial.begin(38400);
  REG_PIOB_PDR = digitalPinToBitMask(2);
  REG_PIOB_ABSR |= digitalPinToBitMask(2);
  REG_PIOB_PDR = digitalPinToBitMask(13);
  REG_PIOB_ABSR |= digitalPinToBitMask(13);
  REG_PMC_PCER0 = (1<<27);
  REG_TC0_CMR0 = 5;
  REG_TC0_BMR = (1<<9)|(1<<8)|(1<<12);
}

void loop()
{
  int16_t pulseCount;
  REG_TC0_CCR0 = TC_CCR_CLKEN | TC_CCR_SWTRG;
  for(int i = 0; i < 50; i++)
  {
    pulseCount = REG_TC0_CV0; //read counter register
    Serial.print(i);
    Serial.print(' ');
    Serial.println(pulseCount);
    delay(100);
  }
  REG_TC0_CCR0 = TC_CCR_CLKDIS;  //disable clock
  delay(1000);
  REG_TC0_CCR0 = TC_CCR_CLKEN | TC_CCR_SWTRG;  //enable clock and reset counter
  for(int i = 0; i < 50; i++)
  {
    pulseCount = REG_TC0_CV0; //read counter register
    Serial.print(i);
    Serial.print(' ');
    Serial.println(pulseCount);
    delay(100);
  }
  while(1);
}

Here's an example readout where I don't change the encoder position in the second loop until i == 20:

1 0
2 0
3 0
4 0
5 0
6 0
7 103
8 440
9 795
10 1153
11 1630
12 1905
13 2376
14 2888
15 3321
16 3791
17 4209
18 4701
19 5090
20 5348
21 5639
22 5934
23 6136
24 6300
25 6333
26 6333
27 6333
28 6333
29 6333
30 6333
31 6333
32 6333
33 6333
34 6333
35 6333
36 6333
37 6333
38 6333
39 6333
40 6333
41 6333
42 6333
43 6333
44 6333
45 6333
46 6333
47 6333
48 6333
49 6333
0 6333
1 6333
2 6333
3 6333
4 6333
5 6333
6 6333
7 6333
8 6333
9 6333
10 6333
11 6333
12 6333
13 6333
14 6333
15 6333
16 6333
17 6333
18 6333
19 6333
20 -84
21 -624
22 -1153
23 -1565
24 -1861
25 -2062
26 -2083
27 -2083
28 -2083
29 -2083
30 -2083
31 -2083
32 -2083
33 -2083
34 -2083
35 -2083
36 -2083
37 -2083
38 -2083
39 -2083
40 -2083
41 -2083
42 -2083
43 -2083
44 -2083
45 -2083
46 -2083
47 -2083
48 -2083
49 -2083

Any help appreciated

A first issue: TC_CV is a 32_bit counter, but pulsecount is declared as an int16 !

And try to power OFF/ON TC0 in between the two parts :

PMC->PMC_PCER0 &= ~PMC_PCER0_PID27;
PMC->PMC_PCER0 |= PMC_PCER0_PID27;

However I don't think this is the right way to read an encoder...

I tried your suggestions but no luck clearing the register until motion is detected.

new code:

void setup()
{
  Serial.begin(38400);
  REG_PIOB_PDR = digitalPinToBitMask(2);
  REG_PIOB_ABSR |= digitalPinToBitMask(2);
  REG_PIOB_PDR = digitalPinToBitMask(13);
  REG_PIOB_ABSR |= digitalPinToBitMask(13);
  REG_PMC_PCER0 = (1<<27);
  REG_TC0_CMR0 = 5;
  REG_TC0_BMR = (1<<9)|(1<<8)|(1<<12);
}

void loop()
{
  int32_t pulseCount;
  REG_TC0_CCR0 = TC_CCR_CLKEN | TC_CCR_SWTRG;
  for(int i = 0; i < 50; i++)
  {
    pulseCount = REG_TC0_CV0; //read counter register
    Serial.print(i);
    Serial.print(' ');
    Serial.println(pulseCount);
    delay(100);
  }
  REG_TC0_CCR0 = TC_CCR_CLKDIS;  //disable clock
  PMC->PMC_PCER0 &= ~PMC_PCER0_PID27;
  PMC->PMC_PCER0 |= PMC_PCER0_PID27;
  REG_TC0_CCR0 = TC_CCR_CLKEN | TC_CCR_SWTRG;  //enable clock and reset counter
  for(int i = 0; i < 50; i++)
  {
    pulseCount = REG_TC0_CV0; //read counter register
    Serial.print(i);
    Serial.print(' ');
    Serial.println(pulseCount);
    delay(100);
  }
  while(1);
}

What is the output with the last version ?

Different values but essentially the same thing. The counter doesn't change until I move the rotary encoder after the second loop has begun.

1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 -224
11 -492
12 -626
13 -871
14 -1139
15 -1346
16 -1553
17 -1745
18 -1985
19 -2212
20 -2410
21 -2650
22 -2899
23 -3215
24 -3495
25 -3809
26 -3968
27 -4231
28 -4503
29 -4526
30 -4376
31 -4169
32 -4140
33 -4140
34 -4140
35 -4140
36 -4140
37 -4140
38 -4140
39 -4140
40 -4140
41 -4140
42 -4140
43 -4140
44 -4140
45 -4140
46 -4140
47 -4140
48 -4140
49 -4140
0 -4140
1 -4140
2 -4140
3 -4140
4 -4140
5 -4140
6 -4140
7 -4140
8 101
9 600
10 892
11 1189
12 1466
13 1686
14 1927
15 2174
16 2501
17 2821
18 3176
19 3481
20 3772
21 4058
22 4260
23 4424
24 4442
25 4442
26 4442
27 4442
28 4442
29 4442
30 4442
31 4442
32 4442
33 4442
34 4442
35 4442
36 4442
37 4442
38 4442
39 4442
40 4442
41 4442
42 4442
43 4442
44 4442
45 4442
46 4442
47 4442
48 4442
49 4442

I think that if I toggle the index input, A6, I might be able to get a reset of the counter.
What I'm trying is connecting digital output pin 4 to A6 and toggling pin 4 from 0 to 1 to 0.
I'm getting mixed results and haven't figured this out satisfactorily.

This is the same thing. The reset doesn't happen until the encoder changes position.

Here's my new code:

void setup()
{
  pinMode(4, OUTPUT);
  Serial.begin(38400);  
  REG_PIOB_PDR = digitalPinToBitMask(2);
  REG_PIOB_ABSR |= digitalPinToBitMask(2);
  REG_PIOB_PDR = digitalPinToBitMask(13);
  REG_PIOB_ABSR |= digitalPinToBitMask(13);
  REG_PMC_PCER0 = (1<<27);
  //REG_TC0_CMR0 = 5;
  REG_TC0_CMR0 = (1<<0)|(1<<2)|(1<<8)|(1<<10);
  REG_TC0_BMR = (1<<9)|(1<<8)|(1<<12);
}

void loop()
{
  digitalWrite(4, 0);
  int32_t pulseCount;
  REG_TC0_CCR0 = TC_CCR_CLKEN | TC_CCR_SWTRG;
  for(int i = 0; i < 50; i++)
  {
    pulseCount = REG_TC0_CV0; //read counter register
    Serial.print(i);
    Serial.print(' ');
    Serial.println(pulseCount);
    delay(100);
  }
  digitalWrite(4, 1);
  delay(10);
  digitalWrite(4, 0);
  for(int i = 0; i < 50; i++)
  {
    pulseCount = REG_TC0_CV0; //read counter register
    Serial.print(i);
    Serial.print(' ');
    Serial.println(pulseCount);
    delay(100);
  }
  while(1);
}

and the output:

0 0
1 0
2 0
3 0
4 0
5 230
6 647
7 1167
8 1752
9 2115
10 2446
11 3077
12 3364
13 3910
14 4464
15 4746
16 5092
17 5289
18 5536
19 5863
20 6260
21 6495
22 6770
23 7115
24 7368
25 7437
26 7437
27 7437
28 7437
29 7437
30 7437
31 7437
32 7437
33 7437
34 7437
35 7437
36 7437
37 7437
38 7437
39 7437
40 7437
41 7437
42 7437
43 7437
44 7437
45 7437
46 7437
47 7437
48 7437
49 7437
0 7437
1 7437
2 7437
3 7437
4 7437
5 7437
6 145
7 1017
8 2282
9 2947
10 3392
11 3654
12 3683
13 3683
14 3683
15 3683
16 3683
17 3683
18 3683
19 3683
20 3683
21 3683
22 3683
23 3683
24 3683
25 3683
26 3683
27 3683
28 3683
29 3683
30 3683
31 3683
32 3683
33 3683
34 3683
35 3683
36 3683
37 3683
38 3683
39 3683
40 3683
41 3683
42 3683
43 3683
44 3683
45 3683
46 3683
47 3683
48 3683
49 3683

ard_newbie:
And try to power OFF/ON TC0 in between the two parts :

PMC->PMC_PCER0 &= ~PMC_PCER0_PID27;
PMC->PMC_PCER0 |= PMC_PCER0_PID27;

OOps...should be:

PMC->PMC_PCDR0 |= PMC_PCDR0_PID27;
PMC->PMC_PCER0 |= PMC_PCER0_PID27;

Thanks. But it still doesn't clear the register until I rotate the encoder.

void setup()
{
  Serial.begin(38400);
  REG_PIOB_PDR = digitalPinToBitMask(2);
  REG_PIOB_ABSR |= digitalPinToBitMask(2);
  REG_PIOB_PDR = digitalPinToBitMask(13);
  REG_PIOB_ABSR |= digitalPinToBitMask(13);
  REG_PMC_PCER0 = (1<<27);
  REG_TC0_CMR0 = 5;
  REG_TC0_BMR = (1<<9)|(1<<8)|(1<<12);
}

void loop()
{
  int32_t pulseCount;
  REG_TC0_CCR0 = TC_CCR_CLKEN | TC_CCR_SWTRG;
  for(int i = 0; i < 50; i++)
  {
    pulseCount = REG_TC0_CV0; //read counter register
    Serial.print(i);
    Serial.print(' ');
    Serial.println(pulseCount);
    delay(100);
  }
  REG_TC0_CCR0 = TC_CCR_CLKDIS;  //disable clock
  PMC->PMC_PCDR0 |= PMC_PCDR0_PID27;
  PMC->PMC_PCER0 |= PMC_PCER0_PID27;
  REG_TC0_CCR0 = TC_CCR_CLKEN | TC_CCR_SWTRG;  //enable clock and reset counter
  //while(REG_TC0_CV0);
  for(int i = 0; i < 50; i++)
  {
    pulseCount = REG_TC0_CV0; //read counter register
    Serial.print(i);
    Serial.print(' ');
    Serial.println(pulseCount);
    delay(100);
  }
  while(1);
}