-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
64 lines (49 loc) · 1.4 KB
/
main.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
#include <stdbool.h>
#include "stm32f446xx.h"
#include "stepper.h"
#include "queue.h"
#define STEP_PIN (1 << 5)
#define STEP_PIN_AF_MODE (1 << 11)
#define STEP_PIN_AF1 (1 << 20)
Stepper motor(400);
Queue commands(10);
int main(){
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
GPIOA->AFR[0] |= STEP_PIN_AF1;
GPIOA->MODER |= STEP_PIN_AF_MODE;
motor.timerInit(TIM2, 1, TIM2_IRQn, 16000000);
motor.setDirPin(GPIOA, 9);
motor.setSleepPin(GPIOA, 8);
motor.setSpeed(150);
motor.enableInterrupt();
commands.push(400);
commands.push(-400);
commands.push(50);
commands.push(-50);
commands.push(400);
commands.push(-400);
while(true){
switch(motor.getState()){
case STEPPER_STATE_IDLE:
if(!commands.isEmpty()){
int value = commands.front();
commands.pop();
motor.nextCommand(value);
}
else{
motor.sleep();
}
break;
case STEPPER_STATE_SLEEPING:
if(!commands.isEmpty()){
motor.wakeup();
}
break;
}
}
}
extern "C" void TIM2_IRQHandler(void);
void TIM2_IRQHandler(void){
motor.interruptHandler();
}