scoutos / scout_avr / src / stepper.cpp @ 64aea12e
History | View | Annotate | Download (1.97 KB)
1 | a07a0b55 | Julian Binder | extern "C" |
---|---|---|---|
2 | { |
||
3 | #include <avr/io.h> |
||
4 | 85dff67b | Julian Binder | #include <util/delay.h> |
5 | a07a0b55 | Julian Binder | } |
6 | #include "stepper.h" |
||
7 | |||
8 | /* Stepper Motor:
|
||
9 | * Provides interface to stepper motor.
|
||
10 | * Can set direction. Outputs pulse to stepper upon call of step func
|
||
11 | * Also provides variable speed sweep
|
||
12 | */
|
||
13 | |||
14 | struct step_t {
|
||
15 | int pos; // position in rotation. |
||
16 | int dir; // direction. -1 CCW. 1 CW. 0 OFF |
||
17 | 2853d46b | Tom Mullins | int step_size; // amount to add to position each step |
18 | a07a0b55 | Julian Binder | int ccw;
|
19 | int cw;
|
||
20 | 2853d46b | Tom Mullins | } step; |
21 | a07a0b55 | Julian Binder | |
22 | |||
23 | 2853d46b | Tom Mullins | void step_init()
|
24 | a07a0b55 | Julian Binder | { |
25 | 2853d46b | Tom Mullins | /* init pos and dir to 0 */
|
26 | a07a0b55 | Julian Binder | step.pos = 0;
|
27 | 85dff67b | Julian Binder | step.dir = 0;
|
28 | a07a0b55 | Julian Binder | |
29 | //set control pins as output
|
||
30 | DDRD |= ((1<<S_STEP) | (1<<S_DIR)); |
||
31 | DDRB |= ((1<<S_MS));
|
||
32 | 64aea12e | Tom Mullins | |
33 | /* this is connected to ENABLE temporarily */
|
||
34 | DDRF |= _BV(S_EN); |
||
35 | step_disable(); |
||
36 | a07a0b55 | Julian Binder | |
37 | //initiate to full steps
|
||
38 | 2853d46b | Tom Mullins | step_set_size(STEP_WHOLE); |
39 | a07a0b55 | Julian Binder | |
40 | //initiate the step pin to be low. stepper steps on low to high
|
||
41 | PORTD &= (~(1<<S_STEP));
|
||
42 | 2853d46b | Tom Mullins | } |
43 | a07a0b55 | Julian Binder | |
44 | 64aea12e | Tom Mullins | void step_enable()
|
45 | { |
||
46 | PORTF &= ~_BV(S_EN); |
||
47 | } |
||
48 | |||
49 | void step_disable()
|
||
50 | { |
||
51 | PORTF |= _BV(S_EN); |
||
52 | } |
||
53 | |||
54 | 2853d46b | Tom Mullins | void step_set_size(char size) |
55 | { |
||
56 | if (size == STEP_WHOLE)
|
||
57 | { |
||
58 | PORTB &= ~_BV(S_MS); |
||
59 | step.step_size = 2;
|
||
60 | } |
||
61 | else
|
||
62 | { |
||
63 | PORTB |= _BV(S_MS); |
||
64 | step.step_size = 1;
|
||
65 | } |
||
66 | a07a0b55 | Julian Binder | } |
67 | |||
68 | /* set direction pin */
|
||
69 | void step_dir(int dir) |
||
70 | { |
||
71 | step.dir = dir; |
||
72 | switch(dir)
|
||
73 | { |
||
74 | case 1: |
||
75 | 2853d46b | Tom Mullins | PORTD |= (1<<S_DIR);
|
76 | a07a0b55 | Julian Binder | break;
|
77 | case -1: |
||
78 | 2853d46b | Tom Mullins | PORTD &= (~(1<<S_DIR));
|
79 | a07a0b55 | Julian Binder | break;
|
80 | } |
||
81 | } |
||
82 | |||
83 | 2853d46b | Tom Mullins | void step_do_step()
|
84 | a07a0b55 | Julian Binder | { |
85 | 85dff67b | Julian Binder | if(step.dir==0) return; //do not step if not enabled |
86 | a07a0b55 | Julian Binder | PORTD |= (1<<S_STEP); //step once |
87 | 85dff67b | Julian Binder | _delay_us(1); //conform with step timing |
88 | a07a0b55 | Julian Binder | PORTD &= (~(1<<S_STEP)); //bring the step bin back down |
89 | 2853d46b | Tom Mullins | if(step.dir==1) step.pos += step.step_size; |
90 | else step.pos -= step.step_size;
|
||
91 | a07a0b55 | Julian Binder | } |
92 | |||
93 | void step_flush()
|
||
94 | { |
||
95 | PORTD &= (~(1<<S_STEP)); //bring the step bin back down |
||
96 | } |
||
97 | |||
98 | //ccw must be less than 0 and cw must be greater than 0
|
||
99 | void step_sweep_bounds(int ccw, int cw) |
||
100 | { |
||
101 | step.ccw = ccw; |
||
102 | step.cw = cw; |
||
103 | } |
||
104 | |||
105 | void step_sweep()
|
||
106 | { |
||
107 | 2853d46b | Tom Mullins | step_do_step(); |
108 | if((step.dir == 1) && (step.cw <= step.pos)) step_dir(-1); |
||
109 | else if((step.dir == -1) && (step.ccw >= step.pos)) step_dir(1); |
||
110 | a07a0b55 | Julian Binder | } |