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.
 
 
 
 
 
 

73 lines
2.1 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. int unused: 6;
  10. };
  11. struct output_pin_map {
  12. int motor1: 4;
  13. int unused: 4;
  14. };
  15. int speed=1, i=0;
  16. boolean direction=0;
  17. struct input_pin_map buttons;
  18. struct input_pin_map buttons_setup;
  19. struct output_pin_map motor;
  20. struct output_pin_map motor_setup;
  21. // lookup table
  22. int const FULL_LOOKUP_TABLE[4] = {
  23. 0x07, 0x06, 0x08, 0x09
  24. };
  25. #byte buttons=0x05 // assign buttons to port a
  26. #byte motor=0x06 // assign motor to port b
  27. #byte buttons_setup=0x85 // assign buttons_setup for port a TRIS
  28. #byte motor_setup=0x86 // assign motor_setup for port b TRIS
  29. motor_setup.motor1=0; // set as Output
  30. buttons_setup.anticlockwise=1; // set as Input
  31. buttons_setup.clockwise=1; // set as Input
  32. setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
  33. while (1) { // REPEAT indefinitely
  34. //determine if a button has been pressed
  35. if((buttons.anticlockwise==FALSE || buttons.clockwise==FALSE) && !(buttons.anticlockwise==FALSE && buttons.clockwise==FALSE)) {
  36. // determine which button was pressed
  37. if(buttons.clockwise==FALSE)
  38. direction=1;
  39. else if (buttons.anticlockwise==FALSE)
  40. direction=0;
  41. for(i=0;i<100;i++) { // REPEAT 100 times
  42. if(direction==1) { // IF button 'C' was pressed THEN
  43. //rotate the motor shaft clockwise
  44. motor.motor1 = FULL_LOOKUP_TABLE[i%4];
  45. }
  46. else if(direction==0) { // OTHERWISE
  47. //rotate the motor shaft anti-clockwise
  48. motor.motor1 = FULL_LOOKUP_TABLE[3-(i%4)];
  49. }
  50. Delay_ms(20*speed); //insert time delay of one step period
  51. }
  52. }
  53. else
  54. motor.motor1 = 0x00;
  55. }
  56. }