diff options
Diffstat (limited to 'usb-i2c-sl018')
-rw-r--r-- | usb-i2c-sl018/stepper.c | 29 | ||||
-rw-r--r-- | usb-i2c-sl018/stepper.h | 4 | ||||
-rw-r--r-- | usb-i2c-sl018/tuer-rfid.c | 3 |
3 files changed, 27 insertions, 9 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(); } - - diff --git a/usb-i2c-sl018/stepper.h b/usb-i2c-sl018/stepper.h index 7cc5f3b..257f81b 100644 --- a/usb-i2c-sl018/stepper.h +++ b/usb-i2c-sl018/stepper.h @@ -26,9 +26,9 @@ typedef enum { dir_open = 0, dir_close = 1 } stepper_direction_t; -void handle_step(void); +//uint8_t handle_step(void); void init_stepper(void); void start_stepper(stepper_direction_t direction); -void stop_stepper(void); +//void stop_stepper(void); #endif diff --git a/usb-i2c-sl018/tuer-rfid.c b/usb-i2c-sl018/tuer-rfid.c index 7f41798..c77b6f2 100644 --- a/usb-i2c-sl018/tuer-rfid.c +++ b/usb-i2c-sl018/tuer-rfid.c @@ -439,8 +439,11 @@ int main(void) CDC_Device_CreateStream(&VirtualSerial_CDC_Interface,&usb_stream);
init_heartbeat();
+ init_stepper();
sl018_reset();
+ start_stepper(dir_close);
+
for(;;) {
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
USB_USBTask();
|