0

Below is the entirety of my prototype code, the cycling of pin 13 is what I use to confirm the board is restarting. The board resets continually on this implementation of initialising the interrupt. There is no code implemented for the interrupt, simply initialising the interrupt and specifically enabling the Output Compare Interrupt by setting its bit high is enough to cause the crash. The evidence of the crash is the onboard LED cycling as instructed in the setup() routine.

I have tried setting OCR3A in one and two operations, I have tried initialising TIMSK3 and not, I have tried intialising TCNT3 to zero and non-zero values, and setting the prescaler with the bit shift and the discrete value, nothing seems to work..

What is going on?

void FlashLED() {
  digitalWrite(13, !digitalRead(13));
  delay(100);
  digitalWrite(13, !digitalRead(13));
  delay(100);
  digitalWrite(13, !digitalRead(13));
  delay(100);
  digitalWrite(13, !digitalRead(13));
  delay(100);
}

void setup() {
  // put your setup code here, to run once:
  pinMode(13, OUTPUT);
  FlashLED();
  
  // Initialise timer 3 and interrupt
  noInterrupts();
  TCCR3A = 0;
  TCCR3B = 0;
  TIMSK3 = 0;
  TCNT3 = 0;

  // Initialise OCR3A in two operations, as OCR3A = 62500; Serial.print(OCR3A); yields 36 (low byte only)
  unsigned int count = 62500;
  OCR3AH = count >> 8;
  OCR3AL = count;
  TCCR3B |= 1 << WGM32; // CTC Mode
  //TCCR3B |= 4; // Prescaler 256
  TCCR3B |= 1 << CS32; // Prescaler 256
  
  TIMSK3 |= 1 << OCIE3A; // Output Compare Interrupt Enable
  interrupts();
}

void loop() {
  // put your main code here, to run repeatedly:

}

Update:

I had also been trying with an empty ISR handler and discounted its effect:

ISR(Timer3_COMPA_vect) {

}

However, as the astute viewer would notice, the signature is incorrect in its capitalisation. The correct handler is

ISR(TIMER3_COMPA_vect) {

}

This has resolved the issue.

5
  • You have enabled the timer and also its interrupts but you have no interrupt handler. The interrupt has to go somewhere and as it is it's going who-knows-where. Add an ISR and I believe that will resolve your issue.
    – jwh20
    Jan 13 at 18:42
  • How irritating. I have for hours been working with an empty ISR handler, the whole problem has been incorrect capitalisation!! The Oscar Liang tutorial routinely mixes the case of the ISR, so it never occurred to me to change it: oscarliang.com/arduino-timer-and-interrupt-tutorial Feel free to submit ans answer that I will accept, since your prompt got me home.
    – J Collins
    Jan 13 at 18:56
  • Glad to be of assistance.
    – jwh20
    Jan 13 at 18:59
  • Unrelated; You should be able to use OCR3A = count to set both the H and L register with only one line of code.
    – Gerben
    Jan 13 at 19:19
  • That is what I was doing initially, but using Serial.print(OCR3A); returned 36, indicating that only the low byte was being written. I don't know how my code could possibly be behaving differently to anyone else's.
    – J Collins
    Jan 13 at 19:58
1

You have enabled the timer and also its interrupts but you have no interrupt handler. The interrupt has to go somewhere and as it is it's going who-knows-where. Add an ISR and I believe that will resolve your issue.

Yes, the capitalization is important!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.