-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstepper.cpp
134 lines (112 loc) · 3.18 KB
/
stepper.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* This contains code for controlling the SilentStepStick that drives the stepper motor.
*/
#include "stepper.h"
/* leave this definition to debug motor control */
//#define DEBUG_MOTOR
/*
* initClock
*/
void initClock() {
#ifdef DEBUG_MOTOR
Serial.println("... initClock");
#endif
// set up I/O
pinMode(STEP_STEPPER, OUTPUT); // step pin
pinMode(DIR_STEPPER, OUTPUT); // direction pin
pinMode(ENABLE_STEPPER, OUTPUT); // enable pin
// set initial state to time keeping (normal speed) and STOPPED
digitalWrite(ENABLE_STEPPER, HIGH); // disable
configNormal();
digitalWrite(ENABLE_STEPPER, LOW); // enable
}
/*
* startClock
*/
void startClock() {
#ifdef DEBUG_MOTOR
Serial.println("... startClock");
#endif
digitalWrite(ENABLE_STEPPER, LOW); // enable, in case previously disabled
TCCR3A = RUN;
}
/*
* stopClock
*/
void stopClock() {
#ifdef DEBUG_MOTOR
Serial.println("... stopClock");
#endif
TCCR3A = NO_RUN;
}
/*
* disable
*/
void disable() {
#ifdef DEBUG_MOTOR
Serial.println("... disable");
#endif
TCCR3A = NO_RUN; // turn off step signal
digitalWrite(ENABLE_STEPPER, HIGH); // disable
}
/*
* configNormal
*/
void configNormal() {
#ifdef DEBUG_MOTOR
Serial.println("... configNormal");
#endif
// go clockwise
digitalWrite(DIR_STEPPER, HIGH); // clockwise
// Configure timer 3 for Fast PWM mode via OCR3A setting TOP;
// this is mode 15 WGM3:0 = 1111
// use 8x prescaling; this is CS2:0 = 010
// use the "special" square wave mode, see end of 14.8.3 in data sheet;
// it is "toggle" with COM3A1:0 = 01
TCCR3A = NO_RUN;
TCCR3B = (1 << WGM33) | (1 << WGM32) | (1 << CS31); // 8x prescale
// set the frequency so that we get a 106.666 Hz square wave
// const unsigned int val = FREQ_NOMINAL; // frequency is 106.666 Hz with 8x prescale
OCR3AH = FREQ_NOMINAL >> 8;
OCR3AL = FREQ_NOMINAL & 0x00ff;
}
/*
* configForward
*/
void configForward() {
#ifdef DEBUG_MOTOR
Serial.println("... configForward");
#endif
// go clockwise
digitalWrite(DIR_STEPPER, HIGH); // clockwise
// Configure timer 3 for Fast PWM mode via OCR3A setting TOP;
// this is mode 15 WGM3:0 = 1111
// use 1x prescaling; this is CS2:0 = 001
// use the "special" square wave mode, see end of 14.8.3 in data sheet;
// it is "toggle" with COM3A1:0 = 01
TCCR3A = NO_RUN;
TCCR3B = (1 << WGM33) | (1 << WGM32) | (1 << CS30); // 1x prescale
// set the frequency so that we get a 24 x 106.666 Hz square wave
OCR3AH = FREQ_SET >> 8;
OCR3AL = FREQ_SET & 0x00ff;
}
/*
* configReverse
*/
void configReverse() {
#ifdef DEBUG_MOTOR
Serial.println("... configReverse");
#endif
// go anit-clockwise
digitalWrite(DIR_STEPPER, LOW); // anti-clockwise
// Configure timer 3 for Fast PWM mode via OCR3A setting TOP;
// this is mode 15 WGM3:0 = 1111
// use 1x prescaling; this is CS2:0 = 001
// use the "special" square wave mode, see end of 14.8.3 in data sheet;
// it is "toggle" with COM3A1:0 = 01
TCCR3A = NO_RUN;
TCCR3B = (1 << WGM33) | (1 << WGM32) | (1 << CS30); // 1x prescale
// set the frequency so that we get a 24 x 106.666 Hz square wave
OCR3AH = FREQ_SET >> 8;
OCR3AL = FREQ_SET & 0x00ff;
}