Mega 2560 - millis() and 16-bit timer counter interrupts

Hi All,

I'm trying to get a 16-bit timer counter running, with interrupts on a Mega 2560 without destroying millis().

I've tried all 16-bit timer counters (1,3,4,5) with the following code, and all destroy millis() and make it run slow.

Any suggestions?

void timers_and_pwm_setup(){
  noInterrupts();
  //SETUP INTERRUPT FOR INPUT AVERAGING ///////////////////
  //set timer2 interrupt at 16mS (62.5Hz)
  TCCR4A = B00000000;// set entire TCCR4A register to 0
  TCCR4B = B00000000;// set entire TCCR4B register to 0
  TCNT4 = 33535;
  TCCR4B = B00000010;// Set CLK prescaler of div8. 16MHz / 8 = 2MHz
  // enable overflow interrupt
  TIMSK4 = B00000001;    
  interrupts();
  }

and the ISR...

ISR(TIMER4_OVF_vect)
  {
  TCNT4 = 33535;
  //MORE CODE HERE...
  }

Both my 8-bit timer counters are already in use, and I need another with an interrupt enabled.

I should also mention I've got a WDT enabled, and quite a bit of code in my setup script. It's definitely not rebooting.

Bevan

Please post your full code. If your ISR is running too long, it influences all other interrupt based functionality. ISR's have to be short.

1 Like

Thanks MicroBahner,

This is exactly what was causing it. I was spitting some data out via UART in the interrupt, and it was causing the fore-mentioned problems.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.