Pages: [1]   Go Down
Author Topic: Timer2 Interrupt..what am I doing wrong?  (Read 371 times)
0 Members and 1 Guest are viewing this topic.
Chicago, IL US
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all!

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

Code:
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..

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49299
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can't do Serial output in an ISR.
Logged

Chicago, IL US
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: