Interrupt is not triggering with prescaler > 64

Hi!

I am programming a device on the Arduino Leonardo that should trigger an Interrupt about every 2 seconds. The Interrupt is triggered if i set the prescaler to values 64 and smaller, but this is way too fast. Once I increase the prescaler, nothing happens anymore. can you tell me why?
(TCCR3B is a 16 Bit counter)

void setup()
{
  TCCR3B |= 0x03;  //prescaler 64
  //TCCR3B |= (1<<CS31) | (1<<CS30); //same effect
  TIMSK3 |= (1<<TOIE0);
  TCNT3=0;
  sei();
}

ISR (TIMER3_OVF_vect)
{
  lcdd.clear();
  lcdd.print("1");
}

lcdd is a LCD display. This is working fine, but way too fast.

void setup()
{
  TCCR3B |= 0x04;  //prescaler 256
  //TCCR3B |= (1<<CS32) //same effect
  TIMSK3 |= (1<<TOIE0);
  TCNT3=0;
  sei();
}

ISR (TIMER3_OVF_vect)
{
  lcdd.clear();
  lcdd.print("1");
}

As soon as the prescaler is above 64, nothing happens anymore, means the display is not switching to just showing "1".

I have no idea why this is happening, can you help me out?

thanks, flautzer

Interesting. I modified your sketch to work with the Serial monitor (shown below) and tested it on my Leonardo. I got the same results you did. With x64 prescaler, a barrage of messages. With x256 or x1024, nothing, even though there should be one message every one or four seconds with those prescalers.

volatile byte print_flag = 0; 

void setup()
{
  Serial.begin(9600);
  TCCR3B |= 0x03;  //prescaler 256
  //TCCR3B |= (1<<CS32) //same effect
  TIMSK3 |= (1<<TOIE0);
  TCNT3=0;
  sei();
}

ISR (TIMER3_OVF_vect)
{
  print_flag = 1;
}

void loop()
{
  if( print_flag )
  {
    print_flag = 0;
    Serial.println( "TIMER3_OVF_vect!" );
  }
}

I, too, am interested in the answer to this.

I found out the answer. For some reason, by the time the sketch is running, TCCR3B defaults to 0x03. With the prescaler bits set to 0b011, they default to a /64 prescaler. When you |= 0x4 onto that, you get 0b111 for the CS bits, corresponding to a clock source of "External clock source on Tn pin. Clock on rising edge" (direct from the datasheet). Since you have nothing hooked up to that pin, the timer doesn't increment, and will never overflow.

The Arduino's init() function, which runs before setup(), must be initializing Timer3 for some purpose. You'll have to manually reset the register to 0 before setting your bits, such as in the following sketch:

volatile byte print_flag = 0; 

void setup()
{
  Serial.begin(9600);
  TCCR3B = 0; // <= YOU NEED TO DO THIS BEFORE SETTING THE BITS!!
  TCCR3B |= 0x04;  //prescaler 256
  //TCCR3B |= (1<<CS32) //same effect
  TIMSK3 |= (1<<TOIE0);
  TCNT3=0;
  sei();
}

ISR (TIMER3_OVF_vect)
{
  print_flag = 1;
}

void loop()
{
  //byte test = TCCR3B;
  //Serial.println( test );
  if( print_flag )
  {
    print_flag = 0;
    Serial.println( "TIMER3_OVF_vect!" );
  }
}

Very good work!

This works now as it should, thank you very much for your effort!