去评论
距米网-精简版

正反转可控步进电机(仿真图+代码)

JUMU
2019/12/30 23:21:57

proteus电子元器件:80C51 BUTTON CAP CAP-ELEC CRYSTAL LED-RRD MOTOR-STEPPER RES ULN2003A
源代码如下:
  1. #include <reg52.h>
  2. #define uint unsigned int
  3. #define uchar unsigned char
  4. uchar code FFW[]=
  5. {
  6. 0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09
  7. };
  8. uchar code REV[]=
  9. {
  10. 0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01
  11. };
  12. sbit K1 = P3^0;
  13. sbit K2 = P3^1;
  14. sbit K3 = P3^2;
  15. void DelayMS(uint ms)
  16. {
  17. uchar i;
  18. while(ms--)
  19. {
  20. for(i=0;i<120;i++);
  21. }
  22. }
  23. void SETP_MOTOR_FFW(uchar n)
  24. {
  25. uchar i,j;
  26. for(i=0;i<5*n;i++)
  27. {
  28. for(j=0;j<8;j++)
  29. {
  30. if(K3 == 0) break;
  31. P1 = FFW[j];
  32. DelayMS(25);
  33. }
  34. }
  35. }
  36. void SETP_MOTOR_REV(uchar n)
  37. {
  38. uchar i,j;
  39. for(i=0;i<5*n;i++)
  40. {
  41. for(j=0;j<8;j++)
  42. {
  43. if(K3 == 0) break;
  44. P1 = REV[j];
  45. DelayMS(25);
  46. }
  47. }
  48. }
  49. void main()
  50. {
  51. uchar N = 3;
  52. while(1)
  53. {
  54. if(K1 == 0)
  55. {
  56. P0 = 0xfe;
  57. SETP_MOTOR_FFW(N);
  58. if(K3 == 0) break;
  59. }
  60. else if(K2 == 0)
  61. {
  62. P0 = 0xfd;
  63. SETP_MOTOR_REV(N);
  64. if(K3 == 0) break;
  65. }
  66. else
  67. {
  68. P0 = 0xfb;
  69. P1 = 0x03;
  70. }
  71. }
  72. }