From 6d66e5759cfd5f35a8177a7d2b8f981b09be486f Mon Sep 17 00:00:00 2001 From: "Michael D. M. Dryden" Date: Sat, 27 Jun 2015 21:28:05 -0600 Subject: [PATCH] Implements #3, timers for stepping. --- asf/common/boards/user_board/init.c | 15 +++++-- config/conf_board.h | 35 ++++++++------- main.c | 66 ++++++++++++++++++----------- 3 files changed, 74 insertions(+), 42 deletions(-) diff --git a/asf/common/boards/user_board/init.c b/asf/common/boards/user_board/init.c index fcf6247..c62d779 100644 --- a/asf/common/boards/user_board/init.c +++ b/asf/common/boards/user_board/init.c @@ -26,11 +26,20 @@ void board_init(void) ioport_set_pin_dir(PIN_DIR, IOPORT_DIR_OUTPUT); ioport_set_pin_level(PIN_EN, 0); - ioport_set_pin_level(PIN_MS1, 1); - ioport_set_pin_level(PIN_MS2, 1); - ioport_set_pin_level(PIN_MS3, 0); ioport_set_pin_level(PIN_RST, 1); ioport_set_pin_level(PIN_SLP, 1); ioport_set_pin_level(PIN_DIR, 1); + + // 8x microstepping + ioport_set_pin_level(PIN_MS1, 1); + ioport_set_pin_level(PIN_MS2, 1); + ioport_set_pin_level(PIN_MS3, 0); + + ioport_set_pin_dir(PIN_ZMIN, IOPORT_DIR_INPUT); + ioport_set_pin_sense_mode(PIN_ZMIN, IOPORT_SENSE_FALLING); + + #ifdef PIN_ZMIN_PULLUP_ENABLED + ioport_set_pin_mode(PIN_ZMIN, IOPORT_MODE_PULLUP); + #endif // PIN_ZMIN_PULLUP_ENABLED } diff --git a/config/conf_board.h b/config/conf_board.h index b11107a..7dc97ca 100644 --- a/config/conf_board.h +++ b/config/conf_board.h @@ -1,20 +1,25 @@ -/** - * \file - * - * \brief User board configuration template - * - */ - #ifndef CONF_BOARD_H #define CONF_BOARD_H -#define PIN_EN IOPORT_CREATE_PIN(PORTD, 2) -#define PIN_MS1 IOPORT_CREATE_PIN(PORTD, 3) -#define PIN_MS2 IOPORT_CREATE_PIN(PORTD, 4) -#define PIN_MS3 IOPORT_CREATE_PIN(PORTD, 5) -#define PIN_RST IOPORT_CREATE_PIN(PORTD, 6) -#define PIN_SLP IOPORT_CREATE_PIN(PORTD, 7) -#define PIN_STEP IOPORT_CREATE_PIN(PORTB, 0) -#define PIN_DIR IOPORT_CREATE_PIN(PORTB, 1) +#define PIN_EN IOPORT_CREATE_PIN(PORTD, 2) +#define PIN_MS1 IOPORT_CREATE_PIN(PORTD, 3) +#define PIN_MS2 IOPORT_CREATE_PIN(PORTD, 4) +#define PIN_MS3 IOPORT_CREATE_PIN(PORTD, 5) +#define PIN_RST IOPORT_CREATE_PIN(PORTD, 6) +#define PIN_SLP IOPORT_CREATE_PIN(PORTD, 7) +#define PIN_STEP IOPORT_CREATE_PIN(PORTB, 0) +#define PIN_DIR IOPORT_CREATE_PIN(PORTB, 1) + +// Endstop +#define PIN_ZMIN IOPORT_CREATE_PIN(PORTB, 2) +#define PIN_ZMIN_PULLUP_ENABLED + +// TWI +#define PIN_SDA IOPORT_CREATE_PIN(PORTC, 4) +#define PIN_SCL IOPORT_CREATE_PIN(PORTC, 5) + +// TC Settings +#define TC1_DIV 0b00000011 // 1/64 CPU tick (4 us tick) +#define TC1_TICK_US 4.0 #endif // CONF_BOARD_H diff --git a/main.c b/main.c index ba88187..b8c114c 100644 --- a/main.c +++ b/main.c @@ -7,21 +7,24 @@ #include "asf.h" #include -//#include #include +#include //Internal global variables +bool current_dir = 1; +uint16_t step_counter = 0; //Internal function declarations +void single_step(void); +void multi_step(bool dir, uint16_t steps, uint16_t steps_per_s) -void single_step(bool dir); - -void single_step(bool dir){ +void single_step(void){ static bool last_dir = 1; - if (last_dir != dir) { - ioport_set_pin_level(PIN_DIR, dir); - last_dir = dir; + if (last_dir != current_dir) { + ioport_set_pin_level(PIN_DIR, current_dir); + delay_us(10); + last_dir = current_dir; } ioport_set_pin_level(PIN_STEP, 1); @@ -32,28 +35,43 @@ void single_step(bool dir){ return; } +void multi_step(bool dir, uint16_t steps, uint16_t us_per_step){ + current_dir = dir; + step_counter = 0; + + TCCR1A = 0b00000000; // Disable compare + TCCR1C = 0b00000000; // Disable force output compare + + OCR1A = ceil(us_per_step/TC1_TICK_US); + TCCR1B |= (1 << WGM12); // CTC mode + + TIMSK1 |= (1 << OCIE1A); // Enable interrupt + TCNT1 = 0; + TCCR1B |= TC1_DIV; // Start Timer + + while (step_counter < steps); // wait until all steps done + + // stop timer + + TIMSK1 &= ~(1 << OCIEA1); + TCCR1B &= ~(0b00000111); +} + int main(void){ board_init(); - - cpu_irq_enable(); - sysclk_init(); //Disables ALL peripheral clocks D: ioport_init(); + cpu_irq_enable(); // enable interrupts delay_ms(500); - bool dir = 1; - program_loop: - - for (uint16_t i = 0; i < 100; i++) { - single_step(1); - delay_ms(5); - } - - for (uint16_t i = 0; i < 100; i++) { - single_step(0); - delay_ms(5); - } - - goto program_loop; + while (1) { + multi_step(1, 40, 2000); + multi_step(0, 40, 2000); + } +} + +ISR(TIMER1_COMPA_vect){ + single_step(); + step_counter++; } \ No newline at end of file -- GitLab