51单片机驱动步进电机,采用八拍供电,可分别实现角度控制,方向控制,圈数控制
制作出来的实物图如下:
单片机源码:
- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code zz[8]={0xfc,0xf8,0xf9,0xf1,0xf3,0xf2,0xf6,0xf4};
- uchar code fz[8]={0xf4,0xf6,0xf2,0xf3,0xf1,0xf9,0xf8,0xfc};
-
- void delay(uint z)
- {
- uint x,y;
- for(x = z; x > 0; x--)
- for(y = 120; y > 0 ; y--);
- }
- void motor_zz()
- {
- uchar i;
- for (i=0; i<8; i++)
- {
- P1 = zz[ i ];
- delay(1);
- }
- }
- void motor_fz()
- {
- uchar i;
- for (i=0; i<8; i++)
- {
- P1 = fz[ i ];
- delay(1);
- }
- }
-
- void main()
- {
- uint a,b,N1,N2;
- N1=512;
- N2=512;
- for(a=0;a<N1;a++)
- {
- motor_zz();
- }
- delay(1000);
- for(b=0;b<N2;b++)
- {
- motor_fz();
- }
- delay(1000);
- P1=0xff;
- while(1);
- }
复制代码
|