motor_test3.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * david austin
  3. * http://www.embedded.com/design/mcus-processors-and-socs/4006438/Generate-stepper-motor-speed-profiles-in-real-time
  4. * DECEMBER 30, 2004
  5. *
  6. * Demo program for stepper motor control with linear ramps
  7. * Hardware: PIC18F252, L6219
  8. *
  9. * Compile with on Microchip XC8 compiler with the command line:
  10. * XC8 --chip=18F252 motor_test3.c
  11. *
  12. * Copyright (c) 2015 Robert Ramey
  13. *
  14. * Distributed under the Boost Software License, Version 1.0. (See
  15. * accompanying file LICENSE_1_0.txt or copy at
  16. * http://www.boost.org/LICENSE_1_0.txt)
  17. */
  18. #include <xc.h>
  19. // 8 MHz internal clock
  20. #define _XTAL_FREQ 8000000
  21. #include <stdint.h>
  22. #include <stdbool.h> /* For true/false definition */
  23. // ***************************
  24. // nullify macro for literals
  25. #define literal(n) n
  26. // ***************************
  27. // map strong types to appropriate underlying types.
  28. typedef uint16_t position_t;
  29. typedef int16_t step_t;
  30. typedef uint32_t ccpr_t;
  31. typedef int32_t c_t;
  32. typedef uint16_t phase_t;
  33. typedef uint8_t phase_ix_t;
  34. typedef int8_t direction_t;
  35. // 1st step=10ms; max speed=300rpm (based on 1MHz timer, 1.8deg steps)
  36. #define C_MIN literal((1000 << 8))
  37. #define C0 literal((10000 << 8))
  38. #include "motor3.c"
  39. void main() {
  40. initialize();
  41. // move motor to position 200
  42. motor_run(200);
  43. while(busy()) __delay_ms(100);
  44. // move motor to position 1000
  45. motor_run(1000);
  46. while(busy()) __delay_ms(100);
  47. // move back to position 0
  48. motor_run(200);
  49. while(busy()) __delay_ms(100);
  50. } // main()