AT89S52单片机的步进电机控制

..
2024-12-18 22:34:42
推荐回答(1个)
回答1:

#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();

}
}