From 2442e4332259e6a774d5873681a3d1d1f3177917 Mon Sep 17 00:00:00 2001 From: "Michael D. M. Dryden" Date: Thu, 13 Aug 2015 19:12:49 -0400 Subject: [PATCH] Added position tracking and homing code --- main.c | 52 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/main.c b/main.c index 3c033d1..dcd1c44 100644 --- a/main.c +++ b/main.c @@ -12,12 +12,15 @@ #include //Internal global variables -bool current_dir = 1; -uint16_t step_counter = 0; +volatile bool current_dir = 1; +volatile uint16_t step_counter = 0; +int32_t z_position = 0; //Internal function declarations void single_step(void); -void multi_step(bool dir, uint16_t steps, uint16_t steps_per_s); +uint16_t multi_step(bool dir, uint16_t steps, uint16_t steps_per_s); +//uint16_t mm_step(double mm, double mm_per_s, bool dir); +bool home(void); void single_step(void){ static bool last_dir = 1; @@ -28,6 +31,13 @@ void single_step(void){ last_dir = current_dir; } + if (current_dir) { + ++z_position; + } + else { + --z_position; + } + ioport_set_pin_level(PIN_STEP, 1); delay_us(10); ioport_set_pin_level(PIN_STEP, 0); @@ -36,7 +46,7 @@ void single_step(void){ return; } -void multi_step(bool dir, uint16_t steps, uint16_t us_per_step){ +uint16_t multi_step(bool dir, uint16_t steps, uint16_t us_per_step){ current_dir = dir; step_counter = 0; @@ -50,14 +60,38 @@ void multi_step(bool dir, uint16_t steps, uint16_t us_per_step){ TCNT1 = 0; TCCR1B |= TC1_DIV; // Start Timer - while (step_counter < steps); // wait until all steps done + while (step_counter < steps){ // wait until all steps done + if (ioport_get_pin_level(PIN_ZMIN) == 0) { + TIMSK1 &= ~(1 << OCIE1A); + TCCR1B &= ~(0b00000111); + return step_counter; + } + } + // stop timer - TIMSK1 &= ~(1 << OCIE1A); TCCR1B &= ~(0b00000111); + + return 0; +} + +//uint16_t mm_step(double mm, double mm_per_s, bool dir){ +// multi_step(dir, (uint16_t)mm, (uint16_t)mm_per_s); // mm/steps_per_mm, mm_per_s -> us_per_step +//} +// +//void home(void){ +// while (multi_step(0, 1000, )) { +// <#statements#> +// } +//} +bool home(void){ + while (multi_step(0, 1600, 400) == 0); + z_position = 0; + return 1; } + int main(void){ board_init(); @@ -65,10 +99,10 @@ int main(void){ cpu_irq_enable(); // enable interrupts delay_ms(500); - + while (1) { - multi_step(1, 40, 2000); - multi_step(0, 40, 2000); + home(); + delay_s(10); } } -- GitLab