-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstepper.cpp
More file actions
276 lines (221 loc) · 7.02 KB
/
stepper.cpp
File metadata and controls
276 lines (221 loc) · 7.02 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//*****************************************************************************
//
// Stepper drive code based on TI's timer.c
//
//*****************************************************************************
#include "src/TM1638.h"
#include "stepper.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include <string.h>
#include <stdlib.h>
// Set variable for stepper speed
unsigned int stepSpeed;
//*****************************************************************************
//
// Flags that contain the current value of the interrupt indicator as displayed
// on the UART.
//
//*****************************************************************************
unsigned long g_ulFlags;
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif
/* reverse: reverse string s in place */
void strreverse(char s[])
{
int c, i, j;
for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
/* itoa: convert n to characters in s */
char *
itoa (char *nptr, int n)
{
char *cptr = nptr;
if (n < 0)
*cptr++ = '-';
while (n)
{
*cptr++ = '0' + abs (n) % 10;
n /= 10;
}
*cptr = '\0';
if (n < 0)
cptr = nptr + 1;
else
cptr = nptr;
strreverse (cptr);
return nptr;
}
void
ramp(int targetSpeed) {
while(1){
if (stepSpeed < targetSpeed && stepSpeed < (targetSpeed - 500)) {
stepSpeed += 15;
} else if (stepSpeed < targetSpeed && stepSpeed >= (targetSpeed - 500)) {
stepSpeed += 5;
} else if (stepSpeed < targetSpeed && stepSpeed >= (targetSpeed - 50)) {
stepSpeed += 1;
} else if(stepSpeed > targetSpeed && stepSpeed > (targetSpeed + 500)) {
stepSpeed -= 15;
} else if(stepSpeed > targetSpeed && stepSpeed <= (targetSpeed + 500)) {
stepSpeed -= 5;
} else if(stepSpeed > targetSpeed && stepSpeed <= (targetSpeed + 50)) {
stepSpeed -= 1;
} else {
break;
}
ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, ROM_SysCtlClockGet() / stepSpeed);
SysCtlDelay(SysCtlClockGet()/250);
}
}
//*****************************************************************************
//
// Set up the timer and update/read the display/buttons
//
//*****************************************************************************
int
main(void)
{
stepSpeed = 0; // Begin stopped
//
// Enable lazy stacking for interrupt handlers. This allows floating-point
// instructions to be used within interrupt handlers, but at the expense of
// extra stack usage.
//
ROM_FPULazyStackingEnable();
//
// Set the clocking to run directly from the crystal.
//
ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
//
// Enable the GPIO port that is used for the on-board LED.
//
ROM_SysCtlPeripheralEnable(LED_PERIPH);
ROM_SysCtlPeripheralEnable(STP1_PERIPH);
ROM_SysCtlPeripheralEnable(TM1_PERIPH);
TM1638 segmentDisplay(TM1_PERIPH, TM1_BASE, TM1_CLK, TM1_DIO, TM1_STB, true, 7);
//
// Enable the GPIO pins for the Stepper and LEDs (PF1 & PF2).
//
ROM_GPIOPinTypeGPIOOutput(LED_BASE, RED_LED|BLUE_LED|GREEN_LED);
ROM_GPIOPinTypeGPIOOutput(STP1_BASE, STP1_PLS | STP1_DIR | STP1_ENA);
GPIOPinWrite(LED_BASE, RED_LED|BLUE_LED|GREEN_LED, GREEN_LED);
GPIOPinWrite(STP1_BASE, STP1_PLS | STP1_DIR, 0x00);
GPIOPinWrite(STP1_BASE, STP1_ENA, STP1_ENA);
//
// Enable the peripherals used by this example.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
//
// Enable processor interrupts.
//
ROM_IntMasterEnable();
//
// Configure the two 32-bit periodic timers.
//
ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, ROM_SysCtlClockGet() / stepSpeed);
//
// Setup the interrupts for the timer timeouts.
//
ROM_IntEnable(INT_TIMER0A);
ROM_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
//
// Enable the timers.
//
ROM_TimerEnable(TIMER0_BASE, TIMER_A);
//
// Loop forever while the timers run.
//
unsigned char buttons;
while (true) {
buttons = segmentDisplay.getButtons();
char buf[5];
itoa(buf,stepSpeed);
if(buttons == 1){
//Drive Stepper Forward
GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, GREEN_LED);
int oldStepSpeed = stepSpeed;
ramp(500);
GPIOPinWrite(STP1_BASE, STP1_DIR, STP1_DIR);
ramp(oldStepSpeed);
segmentDisplay.clearDisplay();
segmentDisplay.setDisplay(std::string("FWD"), 0x00, 0);
SysCtlDelay(700000);
segmentDisplay.clearDisplay();
} else if(buttons == 2){
//Drive Stepper Reverse
GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, RED_LED);
int oldStepSpeed = stepSpeed;
ramp(500);
GPIOPinWrite(STP1_BASE, STP1_DIR, 0x00);
ramp(oldStepSpeed);
segmentDisplay.clearDisplay();
segmentDisplay.setDisplay(std::string("REV"), 0x00, 0);
SysCtlDelay(700000);
segmentDisplay.clearDisplay();
} else if(buttons == 4){
//Increase speed by 50
GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, GREEN_LED);
segmentDisplay.clearDisplay();
segmentDisplay.setDisplay(buf, 0, 0);
segmentDisplay.setDisplay(std::string("+50"), 0, 5);
ramp(stepSpeed + 50);
ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, ROM_SysCtlClockGet() / stepSpeed);
SysCtlDelay(SysCtlClockGet()/20);
segmentDisplay.clearDisplay();
} else if(buttons == 8){
//Decrease speed by 50
GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, RED_LED);
segmentDisplay.clearDisplay();
segmentDisplay.setDisplay(buf, 0, 0);
segmentDisplay.setDisplay(std::string("-50"), 0, 5);
ramp(stepSpeed - 50);
ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, ROM_SysCtlClockGet() / stepSpeed);
SysCtlDelay(SysCtlClockGet()/20);
segmentDisplay.clearDisplay();
} else if(buttons == 16){
//Ramp speed to 3500
GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, GREEN_LED);
segmentDisplay.clearDisplay();
segmentDisplay.setDisplay(buf, 0, 0);
segmentDisplay.setDisplay(std::string("R H"), 0, 5);
ramp(3500);
segmentDisplay.clearDisplay();
} else if(buttons == 32){
//Ramp speed to 1500
GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, GREEN_LED);
segmentDisplay.clearDisplay();
segmentDisplay.setDisplay(buf, 0, 0);
segmentDisplay.setDisplay(std::string("R L"), 0, 5);
ramp(1500);
segmentDisplay.clearDisplay();
} else {
GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, BLUE_LED);
segmentDisplay.setDisplay(buf, 0, 0);
}
}
}