两相四线步进电机驱动代码


#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char

sbit a0=P0^ 0;
sbit a1=P0^ 1;
sbit b0=P0^ 2;
sbit b1=P0^ 3;
sbit en0=P1^ 0;   // 使能1
sbit en1=P1^ 1// 使能2

const  uint eighttb[ 8][ 4]={{ 0, 1, 1, 0},{ 0, 1, 0, 0},{ 0, 1, 0, 1},{ 0, 0, 0, 1},{ 1, 0, 0, 1},{ 1, 0, 0, 0},{ 1, 0, 1, 0},{ 0, 0, 1, 0}};
const  uint fourtb[ 4][ 4]={{ 0, 1, 1, 0},{ 0, 1, 0, 1},{ 1, 0, 0, 1},{ 1, 0, 1, 0}};

void Delay( uint z);
void FullStep( uint count, uint dy,uchar anti);
void OneHalfStep( uint count, uint dy,uchar anti);

void main()
{
     uint n= 2000;

     for(n;n> 0;n--)
    {
        en0= 1;
        en1= 1;
        FullStep( 4, 15, 0);
        en0= 0;
        en1= 0;
        Delay( 1000);
    }
     while( 1);
}

void OneHalfStep( uint count, uint dy,uchar anti)      // count为脉冲数,dy为延时时间,anti为0正转非0反转
{
    uchar i,j;
    i = count;
     for(i;i> 0;i--)
    {
         for(j= 0;j< 8;j++)
        {
             if(anti ==  0)
            {
                a0 = eighttb[j][ 0];
                a1 = eighttb[j][ 1];
                b0 = eighttb[j][ 2];
                b1 = eighttb[j][ 3];
            } else{
                a0 = eighttb[ 7-j][ 0];
                a1 = eighttb[ 7-j][ 1];
                b0 = eighttb[ 7-j][ 2];
                b1 = eighttb[ 7-j][ 3];
            }
            Delay(dy);
        }    
    }
    P0= 0x00;
}

void FullStep( uint count, uint dy,uchar anti)    // count为脉冲数,dy为延时时间,anti为0正转非0反转
{
    uchar i,j;
    i = count;
     for(i;i> 0;i--)
    {
         for(j= 0;j< 4;j++)
        {
             if(anti ==  0)
            {
                a0 = fourtb[j][ 0];
                a1 = fourtb[j][ 1];
                b0 = fourtb[j][ 2];
                b1 = fourtb[j][ 3];
            } else{
                a0 = fourtb[ 3-j][ 0];
                a1 = fourtb[ 3-j][ 1];
                b0 = fourtb[ 3-j][ 2];
                b1 = fourtb[ 3-j][ 3];
            }
            Delay(dy);
        }    
    }
    P0= 0x00;
}

void Delay( uint z)  // 延时函数
{
  uint x,y;
  for(x=z;x> 0;x--)
   for(y= 114;y> 0;y--);
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM