1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Interrupt timer for MEGA2560?

Discussion in 'Arduino' started by Rich D., Mar 18, 2015.

  1. Rich D.

    Rich D. Member

    Joined:
    Feb 3, 2014
    Messages:
    210
    Likes:
    15
    Location:
    West Chester, PA
    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?
     
  2. Rich D.

    Rich D. Member

    Joined:
    Feb 3, 2014
    Messages:
    210
    Likes:
    15
    Location:
    West Chester, PA
    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.
     
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,163
    Likes:
    909
    Location:
    Rochdale UK
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,804
    Likes:
    135
    Location:
    morristown,tn

    Read this https://arduino-info.wikispaces.com/Timers-Arduino

    you just add a little code like this but read the link
    Code (text):
      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: Apr 21, 2015

Share This Page