Go Down

Topic: Timer2 Interrupt..what am I doing wrong? (Read 414 times) previous topic - next topic

Jeff M

Hello all!

Trying to figure out why this causes my Diecimila (w/ upgraded 328 chip) to freeze.

Code: [Select]

void debugMessage(const char *format, ...) {
int stringSize = strlen(format);
char str[stringSize];
va_list args;
va_start(args, format);
vsprintf(str, format, args);
va_end(args);
Serial.println(str);
Serial.flush();
}

void setup() {               
  pinMode(13, OUTPUT);     
  pinMode(11, OUTPUT);
  Serial.begin(9600);
  delay(500);
  debugMessage("Starting Up...");
  delay(500);
  digitalWrite(13, HIGH);
  debugMessage("Setting Timers...");
  TCNT2 = 0;
  debugMessage("TCNT2 Set to: %d", TCNT2);
  debugMessage("Toggling CTC Mode...");
  TCCR2A |= (1 << WGM21 );
  debugMessage("\nSetting Prescaler to 128...");
  TCCR2B |= (1 << CS22 );
  debugMessage("\nSetting OCR2A to 10.");
  OCR2A = 10;
  debugMessage("OCR2A Set to: %d", OCR2A);
  debugMessage("Setting TIMSK2 to enable Output Compare 2A.");
  TIMSK2 |= (1 << OCIE2A);
  debugMessage("\nsetup() done...");
}

void loop() {
        debugMessage("Loop..."):
digitalWrite(13, HIGH);
Serial.println("Loop...");
digitalWrite(13, LOW);
delay(1000);
}

ISR(TIMER2_COMPA_vect) {
debugMessage("Interrupt/TIMER2_COMPA_vect Triggered!");
}


in my tests, the arduino "hangs" right after I set OCIE2A. I never see the "setup done()" message, or any of the "Loop.." messages, nor does the ISR ever get called. Anyone have any clues as to what I'm doing wrong here? I don't have any specific application in mind at the moment, just trying to learn timers..


PaulS

You can't do Serial output in an ISR.

Jeff M

yep. that was it. As soon as I removed the Serial statements, everything worked. Huh. Thanks!

Go Up