概要
arduino megaでtimer5をctcモードで使って見た。
クロック16MHzなので、プリスケーラ1024、15625カウントで1秒。
写真
サンプルコード
volatile int sec1 = 0;
volatile int sec2 = 0;
volatile int sec3 = 0;
ISR(TIMER5_COMPA_vect)
{
sec1++;
}
ISR(TIMER5_COMPB_vect)
{
sec2++;
}
ISR(TIMER5_COMPC_vect)
{
sec3++;
}
void setup()
{
Serial.begin(115200);
TCCR5A = 0;
TCCR5B = 0;
TCCR5B |= (1 << WGM52);
TCCR5B |= (1 << CS52) | (1 << CS50);
TCNT5 = 0;
OCR5A = 15624;
OCR5B = 15624;
OCR5C = 15624;
TIMSK5 = 0;
TIMSK5 |= (1 << OCIE5A);
TIMSK5 |= (1 << OCIE5B);
TIMSK5 |= (1 << OCIE5C);
}
void loop()
{
int a1;
int a2;
int a3;
long b;
a1 = sec1;
a2 = sec2;
a3 = sec3;
b = millis();
Serial.print(a1);
Serial.print("\t");
Serial.print(a2);
Serial.print("\t");
Serial.print(a3);
Serial.print("\t");
Serial.println(b);
delay(1000);
}