标题: MCI8051单片机控制步进电机 [打印本页]

作者: 哈哈哈你的微博    时间: 2019-6-7 16:13
标题: MCI8051单片机控制步进电机


单片机源程序如下:
  1. #include <reg52.h>
  2. unsigned char code FFW[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};
  3. unsigned char code REV[]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};
  4. sbit K1 = P3^0;
  5. sbit K2 = P3^1;
  6. sbit K3 = P3^2;
  7. void DelayMS(unsigned char ms)
  8. {
  9.   unsigned char i;
  10. while(ms--)
  11. {
  12.    for(i=0;i<120;i++);
  13. }
  14. }
  15. void moter_FFW(unsigned char n)
  16. {
  17.   unsigned char i,j;
  18. for(i=0;i<5*n;i++)
  19. {
  20.    for(j=0;j<8;j++)
  21.   {
  22.     if(K3 == 0) break;
  23.    P1 = FFW[j];
  24.    DelayMS(25);
  25.   }
  26. }
  27. }
  28. void moter_REV(unsigned char n)
  29. {
  30.   unsigned char i,j;
  31. for(i=0;i<5*n;i++)
  32. {
  33.    for(j=0;j<8;j++)
  34.   {
  35.     if(K3 == 0) break;
  36.    P1 = REV[j];
  37.    DelayMS(25);
  38.   }
  39. }
  40. }
  41. void main()
  42. {
  43.   unsigned char x = 3;

  44. while(1)
  45. {
  46.    if(K1 == 0)
  47.   {
  48.     P0 = 0xfe;
  49.    moter_FFW(x);
  50.    if(K3 == 0)
  51.     break;
  52.   }
  53.   else if(K2 == 0)
  54.   {
  55.     P0 = 0xfd;
  56.    moter_REV(x);
  57.    if(K3 == 0)
  58.     break;
  59.   }
  60.   else
  61.   {
  62.     P0 = 0xfb;
  63.    P1 = 0x03;
  64.   }
  65. }
复制代码








欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1