Project

General

Profile

Statistics
| Branch: | Revision:

scoutos / scout_avr / src / stepper.cpp @ 53349043

History | View | Annotate | Download (2.12 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 53349043 Tom Mullins
  return; // stepper is temporarily disabled for demo
47 64aea12e Tom Mullins
  PORTF &= ~_BV(S_EN);
48
}
49
50
void step_disable()
51
{
52
  PORTF |= _BV(S_EN);
53
}
54
55 2853d46b Tom Mullins
void step_set_size(char size)
56
{
57
  if (size == STEP_WHOLE)
58
  {
59
    PORTB &= ~_BV(S_MS);
60
    step.step_size = 2;
61
  }
62
  else
63
  {
64
    PORTB |= _BV(S_MS);
65
    step.step_size = 1;
66
  }
67 a07a0b55 Julian Binder
}
68
69
/* set direction pin */
70
void step_dir(int dir)
71
{
72
  step.dir = dir;
73
  switch(dir)
74
  {
75
    case 1:
76 2853d46b Tom Mullins
      PORTD |= (1<<S_DIR);
77 a07a0b55 Julian Binder
      break;
78
    case -1:
79 2853d46b Tom Mullins
      PORTD &= (~(1<<S_DIR));
80 a07a0b55 Julian Binder
      break;
81
  }
82
}
83
84 2853d46b Tom Mullins
void step_do_step()
85 a07a0b55 Julian Binder
{
86 53349043 Tom Mullins
  return; // stepper is temporarily disabled for demo
87 85dff67b Julian Binder
  if(step.dir==0) return; //do not step if not enabled
88 a07a0b55 Julian Binder
  PORTD |= (1<<S_STEP); //step once 
89 85dff67b Julian Binder
  _delay_us(1); //conform with step timing
90 a07a0b55 Julian Binder
  PORTD &= (~(1<<S_STEP)); //bring the step bin back down
91 2853d46b Tom Mullins
  if(step.dir==1) step.pos += step.step_size;
92
  else step.pos -= step.step_size;
93 a07a0b55 Julian Binder
}
94
95
void step_flush()
96
{
97
  PORTD &= (~(1<<S_STEP)); //bring the step bin back down
98
}
99
100
//ccw must be less than 0 and cw must be greater than 0
101
void step_sweep_bounds(int ccw, int cw)
102
{
103
  step.ccw = ccw;
104
  step.cw = cw;
105
}
106
107
void step_sweep()
108
{
109 2853d46b Tom Mullins
  step_do_step();
110
  if((step.dir == 1) && (step.cw <= step.pos)) step_dir(-1);
111
  else if((step.dir == -1) && (step.ccw >= step.pos)) step_dir(1);
112 a07a0b55 Julian Binder
}
113 daf6a575 Tom Mullins
114
int step_get_pos()
115
{
116
  return step.pos;
117
}