University projects
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

124 lines
4.3 KiB

  1. /**********************************************************************/
  2. /* Sam Black, Sam Burras and Ben Francis */
  3. /* 18/01/07 stepper_motor_half.c Fuse: INTRC RA6-IO */
  4. /* */
  5. /* Stepper motor controller for the 16F648A.h PIC */
  6. /* Using half step with variable speed */
  7. /* */
  8. /* Device pin Layout ___________ */
  9. /* Speed MSB |1 \/ 18| Input clockwise */
  10. /* Speed LSB |2 17| Input anticlockwise */
  11. /* |3 16| */
  12. /* |4 15| */
  13. /* V++ |5 14| 0V */
  14. /* Motor pin 1 |6 13| */
  15. /* Motor pin 2 |7 12| */
  16. /* Motor pin 3 |8 11| */
  17. /* Motor pin 4 |9 10| */
  18. /* |___________| */
  19. /* */
  20. /**********************************************************************/
  21. #include <16F648A.h>
  22. #use delay(clock=4000000)
  23. #fuses NOWDT, INTRC_IO, NOPUT, NOPROTECT, NOLVP, NOMCLR
  24. void main()
  25. {
  26. /* Input pin map */
  27. struct input_pin_map {
  28. boolean anticlockwise;
  29. boolean clockwise;
  30. int speed: 2;
  31. int unused: 4;
  32. };
  33. /* Output pin map */
  34. struct output_pin_map {
  35. int motor1: 4;
  36. int unused: 4;
  37. };
  38. int speed=0; //delay in ms
  39. int i=0;
  40. boolean direction=0;
  41. struct input_pin_map buttons;
  42. struct input_pin_map buttons_setup;
  43. struct output_pin_map motor;
  44. struct output_pin_map motor_setup;
  45. // lookup table
  46. int const FULL_LOOKUP_TABLE[4] = {
  47. 0x07, 0x06, 0x08, 0x09
  48. };
  49. int const HALF_LOOKUP_TABLE[8] = {
  50. 0x08, 0x0C, 0x04, 0x06, 0x02, 0x03, 0x01, 0x09
  51. };
  52. #byte buttons=0x05 // assign buttons to port a
  53. #byte motor=0x06 // assign motor to port b
  54. #byte buttons_setup=0x85 // assign buttons_setup for port a TRIS
  55. #byte motor_setup=0x86 // assign motor_setup for port b TRIS
  56. motor_setup.motor1=0; // set as Output
  57. buttons_setup.anticlockwise=1; // set as Input
  58. buttons_setup.clockwise=1; // set as Input
  59. buttons_setup.speed=1; // set as Input
  60. setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
  61. while (1) { // REPEAT indefinitely
  62. //determine if a button has been pressed
  63. if((buttons.anticlockwise==FALSE || buttons.clockwise==FALSE) && !(buttons.anticlockwise==FALSE && buttons.clockwise==FALSE)) {
  64. // read speed setting from PORT A pins 2 & 3. Pin 2 is the MSB.
  65. switch(buttons.speed) {
  66. case 0x00:
  67. speed = 16;
  68. break;
  69. case 0x01:
  70. speed = 8;
  71. break;
  72. case 0x02:
  73. speed = 4;
  74. break;
  75. case 0x03:
  76. speed = 2;
  77. break;
  78. default:
  79. speed = 200;
  80. break;
  81. }
  82. // determine which button was pressed
  83. if(buttons.clockwise==FALSE)
  84. direction=1;
  85. else if (buttons.anticlockwise==FALSE)
  86. direction=0;
  87. for(i=0;i<200;i++) { // REPEAT 100 times
  88. if(direction==1) { // IF button 'C' was pressed THEN
  89. //rotate the motor shaft clockwise
  90. motor.motor1 = HALF_LOOKUP_TABLE[i%8];
  91. }
  92. else if(direction==0) { // OTHERWISE
  93. //rotate the motor shaft anti-clockwise
  94. motor.motor1 = HALF_LOOKUP_TABLE[7-(i%8)];
  95. }
  96. delay_ms(speed); //insert time delay of one step period
  97. }
  98. }
  99. else
  100. /* Don't move the rotor */
  101. motor.motor1 = 0x00;
  102. }
  103. }