Pages: [1]   Go Down
Author Topic: Timer Interrupts on Mega 2560  (Read 1702 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am looking for an example on how to configure and use the internal timer interrupts of the 2560.  I need to do something similar to the MsTimer2 library, that calls a defined function after a set interval.  I know the 2560 has 6 timers, how many of these can I actually use?  And can I have each one call a different function?

Thanks!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is another case of proposing a solution to an undefined problem. Why do you think you need timers to trigger these functions? What are these functions doing?

Describe your problem, not your proposed solution.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am building an Engine control unit and stability control system.  There are several events that need to take place at different intervals for things like wheel speed sensors, ignition, and various other sensors.  I've used implementations with if statements and millis() however I think it is a very inefficient use of timers.

Edit: Once the Due comes out I will be using that instead of the 2560, however I can't waste development time right now, so I would like to figure out how this is done on the 2560, as I have many projects that can implement this technique, then I can port it to the Due when that time comes.
« Last Edit: October 25, 2011, 07:52:09 am by goliathdrakken » Logged

Istanbul
Offline Offline
Newbie
*
Karma: 0
Posts: 40
59 20 55 20 4E 4F 20 52 45 41 44 20 48 45 58 3F
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It depends on how often you'll need the recent data. If you need very high sampling rates, it gets harder to implement.

Let's say you can read your slowest sensor data in x microseconds. If you can spare 2x microseconds for each read separately, set Timer1's period to 2x. Then you can write a function that handles all the reads and attach it to Timer1's overflow interrupt. There are examples on Timer1 playground page. You can switch between sensors everytime the function called by simply using a counter. That's what I have been doing so far and it seems to work. I could write something on LCD while Timer1 triggered the function that reads data from sensors on different periods (function triggered every 500 microseconds in my example). Counter (i=0 to 1999), which is increased on every interrupt, helps me to determine whether I need to read any sensor in this interrupt or not. For example, I check buttons 18 times a second (if i%111==0), encoders 500 times a sec. (if i%4==0),etc.
Logged

Pages: [1]   Go Up
Jump to: