Timers, Interrupts, and Servos

Hi, I have an Arduino MEGA and I'm trying to setup a timer interrupt that triggers an ISR every 10ms.

Firstly, I'm really confused as to what timer I can use. So far, it seems like timer0 and timer1 are being used for several misc. functions such as millis. I'm also using the Servo library included in 0017 to control 7 servos simultaneously. I know this library uses at least one timer to update the PWM signal, but I'm not sure which one. So my question is, what timers can I use that will not stop other functions from working?

Secondly, I've been searching for sample code on how to setup a timer interrupt on the MEGA. I found a few lines here and there, but nothing that really explains how everything is setup. I tried searching through the 1280 datasheet, but I can't seem to locate the right information. Does anyone have some code, a link, or a guide on how to setup a timer interrupt to activate a function every X milliseconds?

I also found the timer3 library for MEGA, but again, I don't know if timer3 is being used for servos or any other basic functions included with the Arudino.

Thanks for your help

I originally found another FAQ similar to this.
I was planning to post my results, which are finally satisfactory, but can’t find the original.
So you get the results:

Basic notes: I wanted a Real Time Clock (RTC) for other purposes.
I have done this before on a different microcontroller but this was new.
It is possible to access the setting registers, TCCR1A, TCCR1B, etc.
Also and ISR apparently needs to be of type ISR and put into the code between Setup() and Loop() functions.
Also you have to set the Interrupt Enable.
I used Timer1 on ArduinoMega.
And lots of SerialPrints for debugging.
The code:
/* **************************************************************************/
/
B Conley, Circuitsville Engineering LLC 15 February 2010 */

/* **************************************************************************/
/
Real Time Clock application in Arduino Mega, using ATMega1280 /
/
Inputs: None (?) /
/
Outputs: Digital: D7 (PH4) Seconds blink LED /
/
D8 (PH5) Minutes blink LED /
/
D9 (PH6) Hours Blink LED /
/
Variables: Secs, Mins, Hrs /
/
Requirements: /
/
Set ORC1A register to 0xF424 to cause interrupt once per second /
/
Use Prescale of 256 (CS12:CS10 = 100, these TCCR1B LSBs) /
/
Set TCCR1B to bxxx0 1100 /
/
Set TCCR1A to bxxxx xx00 /
/
NOTES: /
/
Decimal 60 = b0011 1100 = 0x3C /
/
Decimal 59 = b0011 1011 = 0x3b /
/
Decimal 24 = b0001 1000 = 0x18 /
/
Decimal 23 = b0001 0111 = 0x17 /
/
***************************************************************************/
#define D7 7 /
Seconds, Green LED, PH4 /
#define D8 8 /
Minutes, Yellow LED, PH5 /
#define D9 9 /
Hours, Red LED, PH6 */
int Secs =0;
int Mins =0;
int Hrs =0;
int OutS =0;
int OutM =0;
int OutH =0;
byte IFMask = B00000010;
byte IFClrMask = B11111101;
byte result;
byte IFreset;

void setup()
{
Serial.begin(9600);
pinMode(D7,OUTPUT);
pinMode(D8,OUTPUT);
pinMode(D9,OUTPUT);
digitalWrite(D7,HIGH);
digitalWrite(D8,HIGH);
digitalWrite(D9,HIGH);
delay(3000);
digitalWrite(D9,LOW);
delay(3000);
digitalWrite(D8,LOW);
delay(3000);
digitalWrite(D7,LOW);
Secs =0;
Mins =57;
Hrs =0;
TCCR1A = B01000000;
TCCR1B = B00001100;
TIMSK1 = B00000010; /* Enables the Timer1 Compare A interrupt /
OCR1AH = 0xF4;
OCR1AL = 0x24;
} /
End Setup() /
/
/
/
timer1CompareFlag Function() An ISR (Interrupt Service Routine /
/
Deals with Timer1A compare flag which should happen when one second is
/
/
has passed /
/
Essential Function: /
/
Update seconds until seconds is >59 /
/
At Seconds >59 update Seconds, {reset to 00},(increment) Minutes /
/
Check if Minutes >59 if so update Minutes {reset to 00}, /
/
Increment hours /
/
Check Hours if >23 if so update Hours {reset to 00} /
/
Is actually an Interrupt Service Routine, so should be named & treated thus /
/
*/
ISR (TIMER1_COMPA_vect){
noInterrupts();
Serial.print("Beg ISR Seconds ");
Serial.println(Secs,DEC);
Secs = Secs +1;
if (OutS == 0) /
Begin Blink Green (Seconds) LED /
{
digitalWrite(D7,LOW);
OutS = 1;
}
else
{
digitalWrite(D7,HIGH);
OutS = 0;
} /
End Blink Green LED /
if (Secs > 59)
{
Serial.print("Inc Mins ISR Seconds ");
Serial.println(Secs,DEC);
Secs = 0;
Mins += 1;
if (OutM == 0) /
Begin Blink Yellow (minutes) LED /
{
digitalWrite(D8,HIGH);
OutM = 1;
}
else
{
digitalWrite(D8,LOW);
OutM = 0;
} /
End Blink Yellow LED /
}
else
{
interrupts();
// Serial.print("Seconds ");
// Serial.println(Secs,DEC);
// Serial.print("Minutes ");
// Serial.println(Mins,DEC);
// Serial.print("Hours ");
// Serial.println(Hrs,DEC);
return;
} /
End Seconds If/Else /
if(Mins > 59)
{
Mins = 0;
Hrs += 1;
if (OutH == 0) /
Blink Hours (Red) LED /
{
digitalWrite(D9,HIGH);
OutH = 1;
}
else
{
digitalWrite(D9,LOW);
OutH = 0;
} /
End Hours (Red) LED Blink /
} /
End minutes If /
else
{
interrupts();
Serial.print("End Ms ISR Seconds ");
Serial.println(Secs,DEC);
//Serial.print("Minutes ");
//Serial.println(Mins,DEC);
//Serial.print("Hours ");
//Serial.println(Hrs,DEC);
return;
} /
End Minutes If/Else, end Minutes Else /
if(Hrs > 23)
{
Hrs = 0;
} /
End Hours If /
else
{
interrupts();
return;
} /
End Hours Else, end Hours IF/ELSE /
//Serial.print("End Hrs ISR Seconds ");
//Serial.println(Secs,DEC);
//Serial.print("Minutes ");
//Serial.println(Mins,DEC);
//Serial.print("Hours ");
//Serial.println(Hrs,DEC);
interrupts();
return;
} /
End function timer1CompareFlag() ISR /
void loop()
{
while(1){
interrupts();
Serial.print(‘B’, BYTE); // send a capital B
Serial.print(TIFR1,BIN);
Serial.print(’ Secs ‘, BYTE); // send seconds
Serial.print(Secs, DEC);
Serial.print(’ Mins ‘, BYTE); // send minutes
Serial.print(Mins, DEC);
Serial.print(’ Hrs ', BYTE); // send hours
Serial.println(Hrs, DEC);
result = TIFR1&IFMask;
if (result > 0){
//timer1CompareFlag();
IFreset = TIFR1&IFClrMask; /
This should clear the Timer 1 Flag (OCF1A)
/
TIFR1 = IFreset;

}
establishContact();
//delay(100);
}

} /* End Loop() */

/* ***************************************************** /
/
Function establishContact() /
/
It sends out an ASCII character via USB to rcvr /
/
***************************************************** */

void establishContact() {
while (Serial.available() <= 0) {
Serial.print(‘A’, BYTE); // send a capital A
Serial.print(’ ‘,BYTE);
Serial.print(Secs,DEC);
Serial.print(’ ‘,BYTE);
Serial.print(Mins,DEC);
Serial.print(’ ',BYTE);
Serial.println(Hrs,DEC);
} /* End while loop /
return;
} /
End function establishContact() */