From: vl_garistov Date: Tue, 24 Nov 2020 16:44:06 +0000 (+0200) Subject: added stepper example X-Git-Url: https://kolegite.com/gitweb/?a=commitdiff_plain;h=2f234de8d4e280f8544186f453c3c06da65125e7;p=vmks.git added stepper example --- diff --git a/Examples/Stepper_demo/Backup Of Stepper_demo.pdsbak b/Examples/Stepper_demo/Backup Of Stepper_demo.pdsbak new file mode 100644 index 0000000..6ba6106 Binary files /dev/null and b/Examples/Stepper_demo/Backup Of Stepper_demo.pdsbak differ diff --git a/Examples/Stepper_demo/Drive.png b/Examples/Stepper_demo/Drive.png new file mode 100644 index 0000000..8c3a0a3 Binary files /dev/null and b/Examples/Stepper_demo/Drive.png differ diff --git a/Examples/Stepper_demo/Last Loaded Stepper_demo.pdsbak b/Examples/Stepper_demo/Last Loaded Stepper_demo.pdsbak new file mode 100644 index 0000000..5d39a8f Binary files /dev/null and b/Examples/Stepper_demo/Last Loaded Stepper_demo.pdsbak differ diff --git a/Examples/Stepper_demo/Stepper_demo.PDF b/Examples/Stepper_demo/Stepper_demo.PDF new file mode 100644 index 0000000..101ca36 Binary files /dev/null and b/Examples/Stepper_demo/Stepper_demo.PDF differ diff --git a/Examples/Stepper_demo/Stepper_demo.pdsprj b/Examples/Stepper_demo/Stepper_demo.pdsprj new file mode 100644 index 0000000..d0b5607 Binary files /dev/null and b/Examples/Stepper_demo/Stepper_demo.pdsprj differ diff --git a/Examples/Stepper_demo/Stepper_demo.pdsprj.DESKTOP-Q6QBCIN.Cartogan.workspace b/Examples/Stepper_demo/Stepper_demo.pdsprj.DESKTOP-Q6QBCIN.Cartogan.workspace new file mode 100644 index 0000000..b54f9c5 --- /dev/null +++ b/Examples/Stepper_demo/Stepper_demo.pdsprj.DESKTOP-Q6QBCIN.Cartogan.workspace @@ -0,0 +1,127 @@ + + + + 2c0000000000000001000000ffffffffffffffffffffffffffffffff670000001a000000de06000010040000 + + + + + + + 524 + No + 107 + 100 + + + + + 87 + No + Yes + 87 + Yes + No + No + Yes + 0 + 87 + 100 + 87 + 0 + + + + + + + + 2 + Yes + 2 + Yes + Yes + 00000000 + + + + + 2 + Yes + 2 + Yes + Yes + 00000000 + + + + + 2 + Yes + 2 + Yes + Yes + 00000000 + + + + + 2 + Yes + 2 + Yes + Yes + 00000000 + + + + + 2 + Yes + 2 + Yes + Yes + 00000100 + + + + + 2 + Yes + 2 + Yes + Yes + 00000020 + + + + + No + Yes + No + No + Yes + No + 0 + + + + + 636 + No + Yes + 636 + Yes + Yes + No + No + No + 0 + 636 + 100 + 0 + + + + + diff --git a/Examples/Stepper_demo/bipolar-vs-unipolar-stepper-motors.gif b/Examples/Stepper_demo/bipolar-vs-unipolar-stepper-motors.gif new file mode 100644 index 0000000..a13be32 Binary files /dev/null and b/Examples/Stepper_demo/bipolar-vs-unipolar-stepper-motors.gif differ diff --git a/Examples/Stepper_demo/stepper_demo_delay/stepper_demo_delay.ino b/Examples/Stepper_demo/stepper_demo_delay/stepper_demo_delay.ino new file mode 100644 index 0000000..b7ee2b0 --- /dev/null +++ b/Examples/Stepper_demo/stepper_demo_delay/stepper_demo_delay.ino @@ -0,0 +1,68 @@ +#include + +#define A1_PIN 9 +#define A2_PIN 8 +#define B1_PIN 11 +#define B2_PIN 10 +#define EN_PIN 12 + +#define STEP_TIME 1000 // ms + +//#define FULL_STEP +#define HALF_STEP + +#ifdef FULL_STEP +#define NUM_STEPS 4 +const uint8_t coil_current[NUM_STEPS][4] = +{ + {1, 0, 0, 1}, + {1, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 1, 1} +}; +#endif + +#ifdef HALF_STEP +#define NUM_STEPS 8 +const uint8_t coil_current[NUM_STEPS][4] = +{ + {1, 0, 0, 1}, + {1, 0, 0, 0}, + {1, 1, 0, 0}, + {0, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 1, 0}, + {0, 0, 1, 1}, + {0, 0, 0, 1} +}; +#endif + +const uint8_t stepper_pins[4] = {A2_PIN, A1_PIN, B1_PIN, B2_PIN}; + +void setup() +{ + for (uint8_t i = 0; i < 4; i++) + { + pinMode(stepper_pins[i], OUTPUT); + } + pinMode(EN_PIN, OUTPUT); + digitalWrite(EN_PIN, HIGH); +} + +void loop() +{ + static uint8_t step = 0; + + for (uint8_t i = 0; i < 4; i++) + { + digitalWrite(stepper_pins[i], coil_current[step][i]); + } + + step++; + if (step >= NUM_STEPS) + { + step = 0; + } + + delay(STEP_TIME); +} diff --git a/Examples/Stepper_demo/stepper_demo_timer/stepper_demo_timer.ino b/Examples/Stepper_demo/stepper_demo_timer/stepper_demo_timer.ino new file mode 100644 index 0000000..a4e8741 --- /dev/null +++ b/Examples/Stepper_demo/stepper_demo_timer/stepper_demo_timer.ino @@ -0,0 +1,99 @@ +#include + +#define A1_PIN 9 +#define A2_PIN 8 +#define B1_PIN 11 +#define B2_PIN 10 +#define EN_PIN 12 + +#define STEP_TIME 200 // ms, max 1000 + +#define FULL_STEP +//#define HALF_STEP + +#ifdef FULL_STEP +#define NUM_STEPS 4 +const uint8_t coil_current[NUM_STEPS][4] = +{ + {1, 0, 0, 1}, + {1, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 1, 1} +}; +#endif + +#ifdef HALF_STEP +#define NUM_STEPS 8 +const uint8_t coil_current[NUM_STEPS][4] = +{ + {1, 0, 0, 1}, + {1, 0, 0, 0}, + {1, 1, 0, 0}, + {0, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 1, 0}, + {0, 0, 1, 1}, + {0, 0, 0, 1} +}; +#endif + +void timer1_init(void); +void stepper_init(void); + +const uint8_t stepper_pins[4] = {A2_PIN, A1_PIN, B1_PIN, B2_PIN}; + +void setup() +{ + stepper_init(); + timer1_init(); +} + +void loop() +{} + +void timer1_init(void) +{ + // Disable interrupts during timer configuration + noInterrupts(); + // Select CTC mode (4) and set the prescaler to divide the clock by 256 + // resulting in timer frequency of 62.5 kHz + TCCR1A = 0; + TCCR1B = (1 << WGM12) | (1 << CS12); + // Enable interrupt when reaching the maximum value (OCR1A) + TIMSK1 = (1 << OCIE1A); + // Set the timer period to 50 ms (50 * 62.5 = 3125) + OCR1A = (int) STEP_TIME * 62.5; + // Reset OC register B and the input capture register. They are not used + ICR1 = OCR1B = 0; + // Reset the counter register + TCNT1 = 0; + // Enable inerrupts + interrupts(); +} + +ISR(TIMER1_COMPA_vect) +{ + static volatile uint8_t step = 0; + + for (uint8_t i = 0; i < 4; i++) + { + digitalWrite(stepper_pins[i], coil_current[step][i]); + } + + step++; + if (step >= NUM_STEPS) + { + step = 0; + } +} + +void stepper_init(void) +{ + for (uint8_t i = 0; i < 4; i++) + { + pinMode(stepper_pins[i], OUTPUT); + } + pinMode(EN_PIN, OUTPUT); + digitalWrite(EN_PIN, HIGH); +} + diff --git a/Examples/Stepper_demo/stepper_motor.jpg b/Examples/Stepper_demo/stepper_motor.jpg new file mode 100644 index 0000000..b760c51 Binary files /dev/null and b/Examples/Stepper_demo/stepper_motor.jpg differ diff --git a/Examples/Stepper_demo/stepper_motor_animated.gif b/Examples/Stepper_demo/stepper_motor_animated.gif new file mode 100644 index 0000000..e0ec1bb Binary files /dev/null and b/Examples/Stepper_demo/stepper_motor_animated.gif differ