概要
arduino megaでマルチタスクやってみた。
写真
サンプルコード
volatile int t = 0;
ISR(TIMER3_COMPA_vect)
{
t |= _BV(1);
}
ISR(TIMER3_COMPB_vect)
{
t |= _BV(2);
}
ISR(TIMER3_COMPC_vect)
{
t |= _BV(3);
}
ISR(TIMER4_COMPA_vect)
{
t |= _BV(4);
}
ISR(TIMER4_COMPB_vect)
{
t |= _BV(5);
}
ISR(TIMER4_COMPC_vect)
{
t |= _BV(6);
}
ISR(TIMER5_COMPA_vect)
{
t |= _BV(7);
}
ISR(TIMER5_COMPB_vect)
{
t |= _BV(8);
}
ISR(TIMER5_COMPC_vect)
{
t |= _BV(9);
}
void setup()
{
Serial.begin(115200);
while (!Serial) delay(250);
Serial.print("ok");
Serial.println();
TCCR3A = 0;
TCCR3B = _BV(CS31);
TCNT3 = 0;
TIMSK3 = _BV(OCIE3A) | _BV(OCIE3B) | _BV(OCIE3C);
TCCR4A = 0;
TCCR4B = _BV(CS42);
TCNT4 = 0;
TIMSK4 = _BV(OCIE4A) | _BV(OCIE4B) | _BV(OCIE4C);
TCCR5A = 0;
TCCR5B = _BV(CS52) | _BV(CS50);
TCNT5 = 0;
TIMSK5 = _BV(OCIE5A) | _BV(OCIE5B) | _BV(OCIE5C);
}
void loop()
{
if (t >> 0 & 0x1) Serial.print("task0 ");
if (t >> 1 & 0x1) Serial.print("task1 ");
if (t >> 2 & 0x1) Serial.print("task2 ");
if (t >> 3 & 0x1) Serial.print("task3 ");
if (t >> 4 & 0x1) Serial.print("task4 ");
if (t >> 5 & 0x1) Serial.print("task5 ");
if (t >> 6 & 0x1) Serial.print("task6 ");
if (t >> 7 & 0x1) Serial.print("task7 ");
if (t >> 8 & 0x1) Serial.print("task8 ");
if (t >> 9 & 0x1) Serial.print("task9 ");
t = 0;
Serial.println(" ");
delay(400);
}