[51]L298N电机驱动

/*简易版*/
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
/*
1000
0100
0010
0001
*/
uint time=5;
uint i;
sbit a1=P1^0;
sbit a2=P1^1;
sbit b1=P1^2;
sbit b2=P1^3;

void delay(uint z)
{
    uint x,y;
    for(x=z; x>0; x--)
        for(y=110; y>0; y--);
}
void main()
{
    while(1)
    {
     a1=1;
     b1=0;
     a2=0;
     b2=0;
     delay(time);
     a1=0;
     b1=1;
     a2=0;
     b2=0;
     delay(time);
     a1=0;
     b1=0;
     a2=1;
     b2=0;
     delay(time);
     a1=0;
     b1=0;
     a2=0;
     b2=1;
     delay(time);
    }
}



/*复杂版*/
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
/*
1-2相励磁方式:磁场旋转一周需要换相8次,精度更高
1000
1100
0100
0110
0010
0011
0001
1001
*/
uint time=50;
sbit a1=P1^0;
sbit a2=P1^1;
sbit b1=P1^2;
sbit b2=P1^3;
void delay(uint z)
{
    uint x,y;
    for(x=z; x>0; x--)
        for(y=110; y>0; y--);
}
void main()
{
    while(1)
    {
     a1=1;
     b1=0;
     a2=0;
     b2=0;
     delay(time);
     a1=1;
     b1=1;
     a2=0;
     b2=0;
     delay(time);
     a1=0;
     b1=1;
     a2=0;
     b2=0;
     delay(time);
     a1=0;
     b1=1;
     a2=1;
     b2=0;
     delay(time);
     a1=0;
     b1=0;
     a2=1;
     b2=0;
     delay(time);
     a1=0;
     b1=0;
     a2=1;
     b2=1;
     delay(time);
     a1=0;
     b1=0;
     a2=0;
     b2=1;
     delay(time);
     a1=1;
     b1=0;
     a2=0;
     b2=1;
     delay(time);
    }
}

猜你喜欢

转载自blog.csdn.net/echosun1996/article/details/51736990