forked from jlin25/MerCEVes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstepper.c
More file actions
43 lines (35 loc) · 1.01 KB
/
stepper.c
File metadata and controls
43 lines (35 loc) · 1.01 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
#include "stepper.h"
#include "pico/stdlib.h"
static uint STEP_PIN;
static uint DIR_PIN;
static uint ENA_PIN;
static absolute_time_t last_step_time;
void stepper_init(uint pin_step, uint pin_dir, uint pin_ena) {
STEP_PIN = pin_step;
DIR_PIN = pin_dir;
ENA_PIN = pin_ena;
gpio_init(STEP_PIN);
gpio_init(DIR_PIN);
gpio_init(ENA_PIN);
gpio_set_dir(STEP_PIN, GPIO_OUT);
gpio_set_dir(DIR_PIN, GPIO_OUT);
gpio_set_dir(ENA_PIN, GPIO_OUT);
stepper_enable(true);
last_step_time = get_absolute_time();
}
void stepper_enable(bool en) {
gpio_put(ENA_PIN, en ? 1 : 0); // Active high enable
}
bool stepper_step(bool dir, int step_us) {
absolute_time_t now = get_absolute_time();
if (absolute_time_diff_us(last_step_time, now) >= step_us) {
last_step_time = now;
gpio_put(DIR_PIN, dir ? 1 : 0);
gpio_put(STEP_PIN, 1);
sleep_us(step_us / 2);
gpio_put(STEP_PIN, 0);
sleep_us(step_us / 2);
return true;
}
return false;
}