This shows you the differences between two versions of the page.
| et:examples:motor:stepper:code1 [2010/11/05 00:23] – tekitatud raivo.sell | et:examples:motor:stepper:code1 [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include " | ||
| + | pin led_red | ||
| + | |||
| + | static pin xunipolar_pins[2][4] = | ||
| + | { | ||
| + | { | ||
| + | PIN(E, 2), PIN(E, 3), | ||
| + | PIN(E, 4), PIN(E, 5) | ||
| + | }, | ||
| + | { | ||
| + | PIN(E, 0), PIN(E, 1), | ||
| + | PIN(E, 6), PIN(E, 7) | ||
| + | } | ||
| + | }; | ||
| + | |||
| + | void xunipolar_init(unsigned char xindex) | ||
| + | { | ||
| + | | ||
| + | |||
| + | for (xi = 0; xi < 4; xi++) | ||
| + | { | ||
| + | // Set control pins as output | ||
| + | pin_setup_output(xunipolar_pins[xindex][xi]); | ||
| + | | ||
| + | // Initially stop | ||
| + | pin_clear(xunipolar_pins[xindex][xi]); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | |||
| + | void xunipolar_halfstep(unsigned char index, signed char direction, unsigned short num_steps, unsigned char speed) | ||
| + | { | ||
| + | unsigned short i; | ||
| + | unsigned char state1 = 0, state2 = 1; | ||
| + | unsigned char pattern; | ||
| + | unsigned char timeout; | ||
| + | |||
| + | // Ensure +-1 | ||
| + | direction = ((direction < 0) ? -1 : +1); | ||
| + | |||
| + | // Ensure speed <= 100 | ||
| + | speed = (speed > 100 ? 100 : speed); | ||
| + | |||
| + | // Complete steps | ||
| + | for (i = 0; i < num_steps; i++) | ||
| + | { | ||
| + | state1 += direction; | ||
| + | state2 += direction; | ||
| + | |||
| + | // Make pattern | ||
| + | pattern = | ||
| + | (1 << ((state1 % 8) >> 1)) | | ||
| + | (1 << ((state2 % 8) >> 1)); | ||
| + | | ||
| + | // Set output | ||
| + | PIN_ARRAY_OF_4_MASK(xunipolar_pins[index], | ||
| + | |||
| + | // Wait for the motor to complete the step | ||
| + | timeout = speed; | ||
| + | | ||
| + | while (timeout-- > 0) | ||
| + | { | ||
| + | _delay_ms(1); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Turn off motor | ||
| + | PIN_ARRAY_OF_4_MASK(xunipolar_pins[index], | ||
| + | } | ||
| + | |||
| + | |||
| + | ///////////////// | ||
| + | int main(void){ | ||
| + | | ||
| + | |||
| + | pin_setup_output(led_red); | ||
| + | | ||
| + | xunipolar_init(0); | ||
| + | xunipolar_init(1); | ||
| + | |||
| + | bipolar_init(); | ||
| + | |||
| + | | ||
| + | while(1){ | ||
| + | |||
| + | pin_toggle(led_red); | ||
| + | | ||
| + | // | ||
| + | // | ||
| + | |||
| + | xunipolar_halfstep(0, | ||
| + | xunipolar_halfstep(0, | ||
| + | |||
| + | |||
| + | //see kanal ei tööta miskipärast | ||
| + | // | ||
| + | |||
| + | } | ||
| + | } | ||
| + | </ | ||