#include "reg52.h"
void delay(unsigned int t);
//Motor
sbit F1 = P1^0;
sbit F2 = P1^1;
sbit F3 = P1^2;
sbit F4 = P1^3;
unsigned char code FFW[8]={0xfe,0xfc,0xfd,0xf9,0xfb,0xf3,0xf7,0xf6}; //反转
unsigned char code FFZ[8]={0xf6,0xf7,0xf3,0xfb,0xf9,0xfd,0xfc,0xfe}; //正转
unsigned int K;
/**********************************************************************
* *
* 步进电机驱动 *
* *
***********************************************************************/
void motor_ffw()
{
unsigned char i;
unsigned int j;
for (j=0; j<12; j++) //转1*n圈
{
for (i=0; i<8; i++) //一个周期转30度
{
if(K==1) P1 = FFW[i]&0x1f; //取数据
if(K==2) P1 = FFZ[i]&0x1f;
delay(5); //调节转速
}
}
}
/******************************************************
*
* 延时程序
*
********************************************************/
void delay(unsigned int t)
{
unsigned int k;
while(t--)
{
for(k=0; k<80; k++)
{ }
}
}
main()
{
while(1)
{
K=1;
motor_ffw();
K=2;
motor_ffw();
}
}
给你个步进电机的,数码管可以自己加
#include
sbit key=P3^7; //按键控制步进电机的方向
unsigned char speed=10; //步进电机的转速
//八拍方式驱动,顺序为A AB B BC C CD D DA
unsigned char code clockWise[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x0d};
void delay(unsigned char z)
{
unsigned char x,y;
for(x=0;x
}
void delayms(int z)
{
int x,y;
for(x=0;x
}
void main()
{
unsigned char i;
while(1)
{
for(i=0;i<8;i++)
{
if(key==0) //按键未按下,正转
{
delayms(1);
if(key==0)
P0=clockWise[8-i];
delay(speed);
}
/*else //按键按下,反转
{
P0=clockWise[8-i];
delay(speed);
} */
}
}
}