Hello, my friend drhex! We are nicked alike and we have the same problem:
CTC mode is not working properly
Forget interrupts! The problem is in Timer itself (or in our understanding, I hope).
Here is a sketch without interrupts:
pinMode( 13, OUTPUT);
TCNT2 = 0;
OCR2A = 100; // It DOES NOT MATTER !!!
TCCR2A=0x02; // CTC mode
TCCR2B=0x07; // START Timer, prescaler = 1024
Serial.print( "\nTimer ready\n");
int stopCounter = 50;
byte b = TCNT2;
Serial.print( b, DEC); Serial.print( " \n");
[glow]TCNT2 = 0;[/glow] // That insane op...
delay( random( 200));
} while( stopCounter > 0);
while(1) ; // = STOP
You see, the Timer is set in CTC mode and it freely runs without any interrupts.
Sometimes I fetch TCNT2 and print it.
The result depends on the insane operation:
without it we see ONLY ZEROES exept the very first time. (if we make Timer to run faster, PRESCALER = 64, we will not see NOZERO even the first time).
So, the Timer starts, comes to zero and stops forever
When we put that TCNT2 = 0 on the stage - we do see several NOZEROES. Timer runs now!
By the way, prescaler matters, So try with the slowest possible frequency of Timer - prescaler 1024.
I fixed the problem with PWM phase correct mode. But it would be great to understand my mistakes with the CTC…
Any forum gurus listening?
My love Deep Purple (Pictures Of Home):
Only my own words return
Nobody’s up there
It’s a deception
When will I ever learn?