A warning! FYI, UNO, a timer 1 trap to be avoided

In the docs and manuals and examples we always find the OCR1A register
assigned after TCCR1A as shown below (set up timer 1 for CTC WGM mode ‘4’).

But if, like me, you’re new to the system and happen to put the OCR1A assignment before TCCR1A … (as shown) then your UNO will suck the life out of you. Very weird timing happens. No examples, no help, no clue… until eventually you fix it by accident. I’m sure somewhere in the chip circuit there’s a good reason for this. I just couldn’t find it. So, from someone who wishes he had that piece of his life back… here’s heads up on a tricky hole for a newby to fall into.

void setup() {
GTCCR = (1 << TSM) | (0 << PSRASY) | (1 << PSRSYNC); // hold timer 0/1
TCNT1 = 0;
// DO NOT PUT OCR1A ASSIGNMENT HERE!!!
TCCR1A = 0b00000000;
TCCR1C = 0b00000000;
** OCR1A = 3125; // put it here!!!**
TCCR1B |= (1 << WGM12); // WGM mode 4 = CTC

// CS 100, divide by 256
TCCR1B |= ((1 << CS12));
TCCR1B &= ~( (1 << CS11) | (1 << CS10) );

TIMSK1 |= (1 << OCIE1A);
GTCCR = 0; // release timers
sei();
}

loop() {

// loopy stuff here

}

ISR(TIMER1_COMPA_vect) {

// do interrupt COMP A stuff here

}


cheers

Colin

Most noobs won't even know whar TCCR1 is. I still have no idea what it is!

Why is this in installation and troubleshooting?

I could see in programming questions, so you could ask why it behaves like this. It is not useful as a reference for others as is, because you haven't done the necessary legwork to figure out why you get different behavior depending on the ordering of those register writes. Without knowing the cause, you don't know in what situations the order of those register writes will matter, nor have a system to determine what the right order is.

The reason for it not working doesn't jump out at me - but since I have no freaking clue what the rest of your code is doing or how it fails, I hardly feel like I'm in a good position to provide such.

BionicBrain:
In the docs and manuals and examples we always find the OCR1A register assigned after TCCR1A

But if, like me, you’re new to the system and happen to put the OCR1A assignment before TCCR1A … (as shown) then your UNO will suck the life out of you.

If the docs and manuals and examples all show OCR1A being set up after TCCR1A, then why did you do it the other way anyway.

BionicBrain:
No examples, no help, no clue… until eventually you fix it by accident.

But you just said that all examples show OCR1A setup after TCCR1A, so why now say “No examples”?

No need for any special warning to everyone in your very first post on these forums. In reality, it should just be a lesson to you, to look closely at examples and take notice of them.