diff options
Diffstat (limited to 'usb-i2c-sl018/stepper.c')
-rw-r--r-- | usb-i2c-sl018/stepper.c | 29 |
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(); } - - |