Skip to content

Commit b489a84

Browse files
authored
Create CheckTimer2.ino
1 parent 404780b commit b489a84

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

examples/CheckTimer2/CheckTimer2.ino

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
volatile boolean state;
2+
boolean state13;
3+
4+
#define LEDPIN 6
5+
6+
// toggles every 2ms
7+
ISR(TIMER2_COMPA_vect)
8+
{
9+
state = !state;
10+
digitalWrite(LEDPIN, state);
11+
}
12+
13+
void setup()
14+
{
15+
pinMode(LEDPIN, OUTPUT);
16+
pinMode(13, OUTPUT);
17+
state = true;
18+
state13 = true;
19+
20+
cli();
21+
// set up Timer 2:
22+
TCCR2A = 0; // normal operation
23+
TCCR2B = bit(WGM22) | bit(CS20) | bit(CS22); // CTC, pre-scaling = 128
24+
OCR2A = 249 * clockCyclesPerMicrosecond() / 16; // compare A register value (8-Bit!) (250 * clock speed / 128)
25+
TIMSK2 = bit(OCIE2A); // interrupt on Compare A Match
26+
27+
sei(); // set interrupts flag
28+
}
29+
30+
void loop()
31+
{
32+
// toggles prox. all 4.1µs
33+
state13 = !state13;
34+
digitalWrite(13, state13);
35+
}

0 commit comments

Comments
 (0)