-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstepper.ino
63 lines (41 loc) · 1.21 KB
/
stepper.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Notes:
Timer Counter Control Register 1A
TCCR1A: 7 6 5 4 3 2 1 0
COM1A1 COM1A0 COM1B1 COM1B0 - - WGM11 WGM10
*/
const byte LED = 13;
void setup(){
pinMode(LED, OUTPUT);
Serial.begin(115200);
// Turn off Interrupts so we can set one up:
cli();
//Set Timer/Counter Control Register 1 A and B to Zero as initialization:
TCCR1A = 0;
TCCR1B = 0;
// Set Clock Prescaler to 1024. This turns our 16MHz clock to ~16KHz
TCCR1B |= B00000101;
// Set the Mode of the Counter to Clear on Compare Match (CTC)
TCCR1B |= B00001000;
// Enable this interrupt and use the OCR1A register to Compare.
TIMSK1 |= B00000010;
// Set the Compare Value for the MAX counter value
OCR1A = 16000;
// Enable Interrupts:
sei();
}
ISR(TIMER1_COMPA_vect){
static boolean state = true;
state = !state;
digitalWrite(LED, state ? HIGH : LOW);
}
void printToScreen(){
static int16_t count = 0;
// count = (TCNT1H << 8) + TCNT1L; // Accessing the registers (compiler can do this for us)
count = TCNT1;
Serial.println(count, DEC);
delay(250);
}
void loop() {
printToScreen();
}