打印
[华山论剑]

51单片机的接收上位机指令,并控制步进电机

[复制链接]
1093|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
潘宁不一般|  楼主 | 2022-4-25 09:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
跪求大神看一下程序解答一下。我的程序是单片机接收上位机发来的数字,后对应不同的数字指令通过两个电机做出上下左右的模拟转动。程序语法都没问题,没有错误和警告,也能正常接收上位机的数据,但是就是不执行命令。能看到上位机发送命令后单片机接收灯闪烁一下,但是单片机接的步进电机不动。程序单独拿出去无限循环都能执行。
[color=rgba(0, 0, 0, 0.75)]
  • #include<reg52.h>

  • [color=rgb(119, 120, 136) !important]

    #define uchar unsigned char

  • [color=rgb(119, 120, 136) !important]

    #define uint  unsigned int

  • [color=rgb(119, 120, 136) !important]

    #define Motor1Data P0                    //步进电机控制接口定义

  • [color=rgb(119, 120, 136) !important]

    #define Motor2Data P2                    //步进电机控制接口定义

  • [color=rgb(119, 120, 136) !important]

    void init(void);                    //串口初始化

  • [color=rgb(119, 120, 136) !important]

    unsigned char a;

  • [color=rgb(119, 120, 136) !important]

    uchar phasecw[4] ={0x08,0x04,0x02,0x01};//正转 电机导通相序 D-C-B-A

  • [color=rgb(119, 120, 136) !important]

    uchar phaseccw[4]={0x01,0x02,0x04,0x08};//反转 电机导通相序 A-B-C-D

  • [color=rgb(119, 120, 136) !important]

    //串口初始化,设定传输波特率为9600

  • [color=rgb(119, 120, 136) !important]

    void init(void)

  • [color=rgb(119, 120, 136) !important]

    {

  • [color=rgb(119, 120, 136) !important]

        TMOD = 0x20;    //定时器1,模式2工作模式     

  • [color=rgb(119, 120, 136) !important]

        SCON = 0x50;    //串口工作模式1

  • [color=rgb(119, 120, 136) !important]

        TH1 = 0xfa; //9600

  • [color=rgb(119, 120, 136) !important]

        TL1 = 0xfa;

  • [color=rgb(119, 120, 136) !important]

        ES = 1;

  • [color=rgb(119, 120, 136) !important]

        EA = 1;

  • [color=rgb(119, 120, 136) !important]

        TR1 = 1;

  • [color=rgb(119, 120, 136) !important]

    }

  • [color=rgb(119, 120, 136) !important]

    //jie shou

  • [color=rgb(119, 120, 136) !important]

    void rece(void)

  • [color=rgb(119, 120, 136) !important]

    {

  • [color=rgb(119, 120, 136) !important]

    q1:  if(RI==0) goto q1;

  • [color=rgb(119, 120, 136) !important]



  • [color=rgb(119, 120, 136) !important]

          a=SBUF;

  • [color=rgb(119, 120, 136) !important]

          RI=0;

  • [color=rgb(119, 120, 136) !important]



  • [color=rgb(119, 120, 136) !important]

         return;

  • [color=rgb(119, 120, 136) !important]

    }

  • [color=rgb(119, 120, 136) !important]

    //ms延时函数

  • [color=rgb(119, 120, 136) !important]

    void Delay_xms(uint x)

  • [color=rgb(119, 120, 136) !important]

    {

  • [color=rgb(119, 120, 136) !important]

    uint i,j;

  • [color=rgb(119, 120, 136) !important]

    for(i=0;i<x;i++)

  • [color=rgb(119, 120, 136) !important]

      for(j=0;j<112;j++);

  • [color=rgb(119, 120, 136) !important]

    }

  • [color=rgb(119, 120, 136) !important]

    //X顺时针转动

  • [color=rgb(119, 120, 136) !important]

    void Motor1CW(void)

  • [color=rgb(119, 120, 136) !important]

    {

  • [color=rgb(119, 120, 136) !important]

    uchar i;

  • [color=rgb(119, 120, 136) !important]

    for(i=0;i<4;i++)

  • [color=rgb(119, 120, 136) !important]

      {

  • [color=rgb(119, 120, 136) !important]

       Motor1Data=phasecw;

  • [color=rgb(119, 120, 136) !important]

       Delay_xms(15);//转速调节

  • [color=rgb(119, 120, 136) !important]

      }

  • [color=rgb(119, 120, 136) !important]

    }

  • [color=rgb(119, 120, 136) !important]

    //X逆时针转动

  • [color=rgb(119, 120, 136) !important]

    void Motor1CCW(void)

  • [color=rgb(119, 120, 136) !important]

    {

  • [color=rgb(119, 120, 136) !important]

    uchar i;

  • [color=rgb(119, 120, 136) !important]

    for(i=0;i<4;i++)

  • [color=rgb(119, 120, 136) !important]

      {

  • [color=rgb(119, 120, 136) !important]

       Motor1Data=phaseccw;

  • [color=rgb(119, 120, 136) !important]

       Delay_xms(15);//转速调节

  • [color=rgb(119, 120, 136) !important]

      }

  • [color=rgb(119, 120, 136) !important]

    }

  • [color=rgb(119, 120, 136) !important]

    //Y顺时针转动

  • [color=rgb(119, 120, 136) !important]

    void Motor2CW(void)

  • [color=rgb(119, 120, 136) !important]

    {

  • [color=rgb(119, 120, 136) !important]

    uchar i;

  • [color=rgb(119, 120, 136) !important]

    for(i=0;i<4;i++)

  • [color=rgb(119, 120, 136) !important]

      {

  • [color=rgb(119, 120, 136) !important]

       Motor2Data=phasecw;

  • [color=rgb(119, 120, 136) !important]

       Delay_xms(15);//转速调节

  • [color=rgb(119, 120, 136) !important]

      }

  • [color=rgb(119, 120, 136) !important]

    }

  • [color=rgb(119, 120, 136) !important]

    //Y逆时针转动

  • [color=rgb(119, 120, 136) !important]

    void Motor2CCW(void)

  • [color=rgb(119, 120, 136) !important]

    {

  • [color=rgb(119, 120, 136) !important]

    uchar i;

  • [color=rgb(119, 120, 136) !important]

    for(i=0;i<4;i++)

  • [color=rgb(119, 120, 136) !important]

      {

  • [color=rgb(119, 120, 136) !important]

       Motor2Data=phaseccw;

  • [color=rgb(119, 120, 136) !important]

       Delay_xms(15);//转速调节

  • [color=rgb(119, 120, 136) !important]

      }

  • [color=rgb(119, 120, 136) !important]

    }

  • [color=rgb(119, 120, 136) !important]

    //停止转动

  • [color=rgb(119, 120, 136) !important]

    void MotorStop(void)

  • [color=rgb(119, 120, 136) !important]

    {

  • [color=rgb(119, 120, 136) !important]

    Motor1Data=0x00;

  • [color=rgb(119, 120, 136) !important]

    Motor2Data=0x00;

  • [color=rgb(119, 120, 136) !important]

    }

  • [color=rgb(119, 120, 136) !important]

    //主函数

  • [color=rgb(119, 120, 136) !important]

    void main(void)

  • [color=rgb(119, 120, 136) !important]

    {

  • [color=rgb(119, 120, 136) !important]

        uint i;

  • [color=rgb(119, 120, 136) !important]

        init();

  • [color=rgb(119, 120, 136) !important]

    q2: rece();

  • [color=rgb(119, 120, 136) !important]

        Delay_xms(50);//等待系统稳定

  • [color=rgb(119, 120, 136) !important]

    switch(a)

  • [color=rgb(119, 120, 136) !important]

    {

  • [color=rgb(119, 120, 136) !important]

        case 1:

  • [color=rgb(119, 120, 136) !important]

          for(i=0;i<100;i++)

  • [color=rgb(119, 120, 136) !important]

          {

  • [color=rgb(119, 120, 136) !important]

           Motor2CW(); //Y顺时针转动

  • [color=rgb(119, 120, 136) !important]

          }

  • [color=rgb(119, 120, 136) !important]

          MotorStop();  //停止转动

  • [color=rgb(119, 120, 136) !important]

          Delay_xms(500);

  • [color=rgb(119, 120, 136) !important]

        case 2:

  • [color=rgb(119, 120, 136) !important]

          for(i=0;i<100;i++)

  • [color=rgb(119, 120, 136) !important]

          {

  • [color=rgb(119, 120, 136) !important]

           Motor2CCW();//Y逆时针转动

  • [color=rgb(119, 120, 136) !important]

          }

  • [color=rgb(119, 120, 136) !important]

          MotorStop();  //停止转动

  • [color=rgb(119, 120, 136) !important]

          Delay_xms(500);

  • [color=rgb(119, 120, 136) !important]

        case 3:

  • [color=rgb(119, 120, 136) !important]

          for(i=0;i<100;i++)

  • [color=rgb(119, 120, 136) !important]

          {

  • [color=rgb(119, 120, 136) !important]

           Motor1CCW();//X逆时针转动

  • [color=rgb(119, 120, 136) !important]

          }

  • [color=rgb(119, 120, 136) !important]

          MotorStop();  //停止转动

  • [color=rgb(119, 120, 136) !important]

          Delay_xms(500);

  • [color=rgb(119, 120, 136) !important]

        case 4:

  • [color=rgb(119, 120, 136) !important]

          for(i=0;i<100;i++)

  • [color=rgb(119, 120, 136) !important]

          {

  • [color=rgb(119, 120, 136) !important]

           Motor1CW();//X顺时针转动

  • [color=rgb(119, 120, 136) !important]

          }

  • [color=rgb(119, 120, 136) !important]

          MotorStop();  //停止转动

  • [color=rgb(119, 120, 136) !important]

          Delay_xms(500);

  • [color=rgb(119, 120, 136) !important]

            }goto q2;

  • [color=rgb(119, 120, 136) !important]

    }







使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

15

主题

16

帖子

0

粉丝