Interrupt timer for MEGA2560?

Status
Not open for further replies.

Rich D.

Active Member
Once again I am working on a project that could really use an interrupt timer function. I haven't been able to get the MEGA 2560 to do that - the library which worked for the Uno R3 for some reason didn't apply to the 2560. And yet I need the '2560 for it's 4 UART channels. I really don't understand why the Arduino software doesn't provide a timer interrupt function when it does pin interrupts. I know there are timers in there! I almost want to resort to creating a pulse output and running it straight back into an interrupt input! (I'd rather not.) So my official question is:

Does anybody know how to program an interrupt function on the MEGA2560 that can act as a timer? My application needs to create fairly accurate timer ticks - but not incredibly fast - from a range of 105,411 uSec to 375,000 uSec. I would really like to avoid writing assembly code direct to the AVR if possible. This routine could just perform the micros(); function every so often and report when the time has elapsed. I know the micros(); function only has a resolution of 4uSec, and errors of almost 100uSec are tolerable for my application.

At the fastest speeds of around 105.411 milliseconds, I don't want to truncate the timing to 105 mSec because accumulated errors will add up to poor timing accuracy. So basically I would like to pass to this interrupt function a long int value in the range of [105411 to 375000] and have the interrupt count the microseconds and flag whenever it reaches the count.

Any Arduino masters out there who want to tackle this problem?
 
Been more than a month. I guess I have my answer: Impossible. Can't be done.
"Alls I know is the more I get involved in anything "Arduino" the more I learn how useless those things are.
 
Read this **broken link removed**

you just add a little code like this but read the link
Code:
  noInterrupts();           // disable all interrupts
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1  = 0;

  OCR1A = 31250;            // compare match register 16MHz/256/2Hz
  TCCR1B |= (1 << WGM12);   // CTC mode
  TCCR1B |= (1 << CS12);    // 256 prescaler
  TIMSK1 |= (1 << OCIE1A);  // enable timer compare interrupt
  interrupts();             // enable all interrupts
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…