关于步进电机速度仿真

2019-07-18 09:44发布

本帖最后由 hhjwtc 于 2016-11-3 12:36 编辑

#include <reg51.h>
#define uchar unsigned char
sbit P1_0=P1^0;
sbit P1_1=P1^1;
sbit P1_2=P1^2;
sbit P1_3=P1^3;
sbit P1_4=P1^4;
sbit P1_5=P1^5;
sbit P1_6=P1^6;
sbit P1_7=P1^7;
uchar V,flag,KKK; //V用于调速控制;flag=0正转;flag=1反转; flag=2不转;KKK为P1的状态寄存
uchar loop[2][4]={{0x0c,0x06,0x03,0x09},{0x09,0x03,0x06,0x0c}};         //顺时针转表(1100 0110 0011 1001) 和逆时针转表        (1001 0011 0110 1100)
void loop1(void);
void loop2(void);
void step(void);

main()
{
  uchar i,j;
  TMOD=0x10;
  TL1=0xe0;
  TH1=0xb1;
  IE=0;                 //中断允许寄存器
  while(1)
  {
    if(KKK!=P1)
        {          //当P1的值发生变化,触发采集信号
      loop1();
        }
    if(flag!=2)            
    {
      for(i=0;i<=3;i++)
      {
        P0=loop[flag];
       for(j=0;j<=V;j++)
              {
              step();
                }
            }
    }
  }
}

void step(void)        //产生20MS的单位步时间
{
  TF1=0;
  TR1=1;                  
  while(TF1==0);
  TR1=0;
  TL1=0xe0;
  TH1=0xb1;                          //重装初值
}

void loop1(void)                 //采集顺时针或逆时针信号,P1.6=1,顺时针,P1.7=1,逆时针
{
  KKK=P1;                          //暂存P1的状态      
  if(P1_6==1)
  {
    flag=0;
    loop2();
  }
  else if(P1_7==1)
  {
    flag=1;
    loop2();
  }
  else
  {
    flg=2;
  }
}
void loop2(void)
{
  if(P1_0==1)
  {
          V=3;
  }
  else if(P1_1==1)
  {
         V=5;
  }
  else if(P1_2==1)
  {
         V=8;
  }
  else if(P1_3==1)
  {
          V=12;
  }
  else if(P1_4==1)
  {
   V=16;
  }
  else if(P1_5==1)
  {
    V=20;
  }
}

for循环那块是怎么控制速度的变换的,怎么可以根据我需要的电机转速来 求出具体的速度值(也就是V的大小)









友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。