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.
 
 
 
 
 
 

93 lines
2.7 KiB

  1. #include <16F648A.h>
  2. #use delay(clock=4000000)
  3. #fuses NOWDT, INTRC_IO, NOPUT, NOPROTECT, NOLVP, NOMCLR
  4. void main()
  5. {
  6. struct input_pin_map {
  7. boolean anticlockwise;
  8. boolean clockwise;
  9. boolean speed1;
  10. boolean speed2;
  11. int unused: 4;
  12. };
  13. struct output_pin_map {
  14. int motor1: 4;
  15. int unused: 4;
  16. };
  17. int speed=500, i=0;
  18. boolean direction=0;
  19. struct input_pin_map buttons;
  20. struct input_pin_map buttons_setup;
  21. struct output_pin_map motor;
  22. struct output_pin_map motor_setup;
  23. // lookup table
  24. int const FULL_LOOKUP_TABLE[4] = {
  25. 0x07, 0x06, 0x08, 0x09
  26. };
  27. int const HALF_LOOKUP_TABLE[8] = {
  28. 0x01, 0x09, 0x0A, 0x02, 0x06, 0x04, 0x05
  29. };
  30. #byte buttons=0x05 // assign buttons to port a
  31. #byte motor=0x06 // assign motor to port b
  32. #byte buttons_setup=0x85 // assign buttons_setup for port a TRIS
  33. #byte motor_setup=0x86 // assign motor_setup for port b TRIS
  34. motor_setup.motor1=0; // set as Output
  35. buttons_setup.anticlockwise=1; // set as Input
  36. buttons_setup.clockwise=1; // set as Input
  37. buttons_setup.speed1=1; // set as Input
  38. buttons_setup.speed2=1; // set as Input
  39. setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
  40. while (1) { // REPEAT indefinitely
  41. //determine if a button has been pressed
  42. if((buttons.anticlockwise==FALSE || buttons.clockwise==FALSE) && !(buttons.anticlockwise==FALSE && buttons.clockwise==FALSE)) {
  43. if(buttons.speed1==FALSE && buttons.speed2==FALSE)
  44. speed=40;
  45. else if(buttons.speed1==FALSE && buttons.speed2=TRUE)
  46. speed=20;
  47. else if(buttons.speed1==TRUE && buttons.speed2==FALSE)
  48. speed=10;
  49. else if(buttons.speed1==TRUE && buttons.speed2==TRUE)
  50. speed=5;
  51. else
  52. speed=250;
  53. // determine which button was pressed
  54. if(buttons.clockwise==FALSE)
  55. direction=1;
  56. else if (buttons.anticlockwise==FALSE)
  57. direction=0;
  58. for(i=0;i<100;i++) { // REPEAT 100 times
  59. if(direction==1) { // IF button 'C' was pressed THEN
  60. //rotate the motor shaft clockwise
  61. motor.motor1 = HALF_LOOKUP_TABLE[i%4];
  62. }
  63. else if(direction==0) { // OTHERWISE
  64. //rotate the motor shaft anti-clockwise
  65. motor.motor1 = HALF_LOOKUP_TABLE[3-(i%4)];
  66. }
  67. Delay_ms(speed); //insert time delay of one step period
  68. }
  69. }
  70. else
  71. motor.motor1 = 0x00;
  72. }
  73. }