Go Down

Topic: Timer Interrupts on Mega 2560 (Read 2253 times) previous topic - next topic

goliathdrakken

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!

PaulS

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.

goliathdrakken

#2
Oct 25, 2011, 02:48 pm Last Edit: Oct 25, 2011, 02:52 pm by goliathdrakken Reason: 1
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.

acarb

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.

Go Up