summaryrefslogtreecommitdiff
path: root/usb-i2c-sl018/stepper.c
diff options
context:
space:
mode:
authorOthmar Gsenger <otti@wirdorange.org>2013-02-05 23:37:16 +0000
committerOthmar Gsenger <otti@wirdorange.org>2013-02-05 23:37:16 +0000
commit82fc54a1ceb3473e7a79a02dbc65abc556cdf93b (patch)
treef12c0040b42dc4d7a8d608f12b2f4831cd2e85b2 /usb-i2c-sl018/stepper.c
parentadded basic stepper support (diff)
motor with timer working
git-svn-id: https://svn.spreadspace.org/avr/trunk@120 aa12f405-d877-488e-9caf-2d797e2a1cc7
Diffstat (limited to 'usb-i2c-sl018/stepper.c')
-rw-r--r--usb-i2c-sl018/stepper.c29
1 files changed, 22 insertions, 7 deletions
diff --git a/usb-i2c-sl018/stepper.c b/usb-i2c-sl018/stepper.c
index 868fff0..4264a42 100644
--- a/usb-i2c-sl018/stepper.c
+++ b/usb-i2c-sl018/stepper.c
@@ -46,10 +46,18 @@ uint8_t step_table [] =
#define STEPPER_OUTPUT_BITMASK (~(0xF << STEPPER_FIRST_BIT ))
volatile uint16_t step_cnt = 0;
-static uint16_t step_cnt_max = LENGTH_STEP_TABLE*20;
+static uint16_t step_cnt_max = LENGTH_STEP_TABLE*200;
stepper_direction_t step_direction = dir_open;
-void handle_step(void)
+inline void stop_stepper(void)
+{
+ // stop timer here
+ STEPPER_PORT &= ~(0xF << STEPPER_FIRST_BIT | 1<<STEPPER_ENABLE_BIT);
+ TCCR1B = 0; // no clock source
+ TIMSK1 = 0; // disable timer interrupt
+}
+
+inline uint8_t handle_step(void)
{
if(step_cnt < step_cnt_max)
{
@@ -57,8 +65,10 @@ void handle_step(void)
uint8_t stepper_output=step_table[step_cnt % LENGTH_STEP_TABLE];
stepper_output<<=STEPPER_FIRST_BIT;
STEPPER_PORT = (STEPPER_PORT & STEPPER_OUTPUT_BITMASK ) | stepper_output;
+ return 1;
} else {
stop_stepper();
+ return 0;
}
}
@@ -74,12 +84,17 @@ void start_stepper(stepper_direction_t direction)
step_direction = direction;
STEPPER_PORT |= 1<<STEPPER_ENABLE_BIT;
//start timer here
+ // timer 1: 2 ms, between stepper output state changes
+ TCCR1A = 0; // prescaler 1:256, WGM = 4 (CTC)
+ TCCR1B = 1<<WGM12 | 1<<CS12; //
+ OCR1A = 124; // (1+124)*256 = 32000 -> 2 ms @ 16 MHz
+ //OCR1A = 155; // (1+155)*256 = 40000 -> 2 ms @ 20 MHz
+ TCNT1 = 0; // reseting timer
+ TIMSK1 = 1<<OCIE1A; // enable Interrupt
}
-void stop_stepper(void)
+
+ISR(TIMER1_COMPA_vect)
{
- // stop timer here
- STEPPER_PORT &= ~(0xF << STEPPER_FIRST_BIT | 1<<STEPPER_ENABLE_BIT);
+ handle_step();
}
-
-