Revision 85dff67b scout_avr/src/stepper.cpp

View differences:

scout_avr/src/stepper.cpp
1 1
extern "C"
2 2
{
3 3
#include <avr/io.h>
4
#include <util/delay.h>
4 5
}
5 6
#include "stepper.h"
6 7

  
......
26 27
  /* set update time for sweep */
27 28
  step.sweep_us = call_us;
28 29
  
29
  /* init pos and time to 0 */
30
  /* init pos, time, and dir to 0 */
30 31
  step.pos = 0;
31 32
  step.time = 0;
33
  step.dir = 0;
32 34

  
33 35
  //set control pins as output
34 36
  DDRD |= ((1<<S_STEP) | (1<<S_DIR));
......
61 63

  
62 64
void step_halfstep()
63 65
{
64
  if(dir==0) return; //do not step if not enabled
66
  if(step.dir==0) return; //do not step if not enabled
65 67
  PORTB |= (1<<S_MS); //enable microstepping
66
  _asm_(
67
    "nop"
68
    "nop"
69
    "nop"
70
    "nop"
71
  );//wait 250 ns for microstepping to enable
68
  __asm__ __volatile__(
69
    "nop\n\t"
70
    "nop\n\t"
71
    "nop\n\t"
72
    "nop\n\t"
73
  ::);//wait 250 ns for microstepping to enable
72 74
  PORTD |= (1<<S_STEP); //step once
73
  WAIT_US(1); //conform with step timing
75
  _delay_us(1); //conform with step timing
74 76
  PORTD &= (~(1<<S_STEP)); //bring the step bin back down
75 77
  PORTB &= (~(1<<S_MS)); //disable microstepping
76
  if(dir==1) step.pos++; //keep track of position. 1/2 step so add/sub 1
78
  if(step.dir==1) step.pos++; //keep track of pos. 1/2 step so add/sub 1
77 79
  else step.pos--;
78 80
}
79 81

  
80 82
void step_fullstep()
81 83
{
82
  if(dir==0) return; //do not step if not enabled
84
  if(step.dir==0) return; //do not step if not enabled
83 85
  PORTD |= (1<<S_STEP); //step once 
84
  WAIT_US(2); //conform with step timing
86
  _delay_us(1); //conform with step timing
85 87
  PORTD &= (~(1<<S_STEP)); //bring the step bin back down
86
  if(dir==1) step.pos+=2; // full step so add/sub 2
88
  if(step.dir==1) step.pos+=2; // full step so add/sub 2
87 89
  else step.pos-=2;
88 90
}
89 91

  
......
109 111

  
110 112
void step_sweep()
111 113
{
112
  step.time += sweep_us;
113
  if(step.time >= speed)
114
  step.time += step.sweep_us;
115
  if(step.time >= step.speed)
114 116
  {
115 117
    step_halfstep();
116
    if((dir == 1) && (step.cw <= pos)) dir=-1;
117
    else if((dir == -1) && (step.ccw >= pos)) dir=1;
118
    if((step.dir == 1) && (step.cw <= step.pos)) step.dir=-1;
119
    else if((step.dir == -1) && (step.ccw >= step.pos)) step.dir=1;
118 120
  }
119 121
}

Also available in: Unified diff