Arduino UNO Timer issue

arduinoavrinterruptsserial

i seem to be facing a weird issue here. First time working with AVR timers. Basically what I am trying to achieve is to set timer2 (with prescaler val @ 1024) and use CTC mode (with val 78) to get 5ms timer ticks.

Every 5ms i just display the current framebuffer on a row of LEDs and every 1sec (using a counter to keep track of 200 timer ticks) update the frame buffer.

my code is

void setup()
{
  pinMode(dataPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(latchPin, OUTPUT);
  pinMode(row1, OUTPUT); pinMode(row2, OUTPUT); pinMode(row3, OUTPUT); pinMode(row4, OUTPUT); pinMode(row5, OUTPUT); 

  digitalWrite(row1,LOW); digitalWrite(row2,LOW); digitalWrite(row3,LOW); digitalWrite(row4,LOW); digitalWrite(row5,LOW);
  digitalWrite(latchPin,HIGH);
  digitalWrite(clockPin,LOW);

  Serial.begin(9600);
  Serial.println("Program initialized ...");

  //create string map + display the initial frame buffer
  //create_string_map("hello!");
  //generate the first frame_buffer
  Serial.println("1");
  //create_frame_buffer(0);
  Serial.println("2");
  //update_frame_buffer();
  Serial.println("3");

  //disable global interrupts
  cli();
  //init timer2
  TCCR2A = 0;
  TCCR2B = 0;
  //set ctc mode
  TCCR2B |= (1 << WGM12);
  //set CTC value
  OCR2A = 78;
  //set prescaler @ 1024
  TCCR2B |= (1<<CS10);
  TCCR2B |= (1<<CS12);
  //enable timer
  TIMSK2 |= (1<<TOIE2);
  sei();
}


//install the ISR
ISR(TIMER2_OVF_vect)
{
  timer_counter++;
  //disable timer
  TIMSK2 |= (0<<TOIE2);
  if(timer_counter==200) //200 = 1 sec
  {
    //reset timer_counter
    timer_counter=0;
    //update frame
    /*Serial.println("Frame update event ...");
    string_map_col_counter++;
    if(string_map_col_counter>=30){string_map_col_counter=0;}
    create_frame_buffer(string_map_col_counter);
    update_frame_buffer();
    display_and_hold_frame();*/
    Serial.println("1 second tick");
  }
  else
  {
    //display_and_hold_frame();
    Serial.println("timer tick"); 
  }
  //enable timer
  TIMSK2 |= (1<<TOIE2);
}

as you see i have commented most of the stuff out because i am trying to debug the issue. when i open the serial port the only output i get is "prog" and just stops there.

the only thing i can think of that might be going wrong is that the timer is cont configured with ticks too fast so the serial.println never gets the time to finish. but as you can see i start the timer AFTER all these serial print statements.

also when i press the reset button on the board does it clear all the timer configs on board or does it remember them b/w resets ?
any ideas what might be causing this.

Best Answer

At 9600 bps, it takes over 1 ms to transmit one character, so getting just "Prog" in 5 ms sounds about right. Try increasing the serial port speed and/or sending shorter messages (e.g., just one character per timer tick).

I don't know much about how the Arduino Serial module works internally, but my guess would be that you're simply overloading its transmit buffer with all the stuff you're printing, and it's just "giving up".