求单片机AT89C52控制步进电机的程序 谢谢! 要C语言的

控制步进电机的正转反转 加减速等 精度需要高
2024-11-23 02:24:33
推荐回答(4个)
回答1:

#include

unsigned char code F_Rotation[4]={0x08,0x10,0x20,0x40};//正转表格
unsigned char code B_Rotation[4]={0x40,0x20,0x10,0x08};//反转表格

void Delay(unsigned int i)//延时
{
while(--i);
}

main()
{
while(1)
{
unsigned char i;
unsigned int j;

for(j=0;j<512;j++)
{
for(i=0;i<4;i++) //4相
{
P1=B_Rotation[i]; //输出对应的相 可以自行换成反转表格
Delay(500); //改变这个参数可以调整电机转速
}
}

for(j=0;j<512;j++)
{
for(i=0;i<4;i++) //4相
{
P1=F_Rotation[i]; //输出对应的相 可以自行换成反转表格
Delay(500); //改变这个参数可以调整电机转速
}
}

}
}

回答2:

不会就是控制它的正反转吧,这样的话功能也太简单了,很多开发板都会有步进电机控制的简单程序示例,自己可以试着修改下,比如计算步进电机的步数,并显示在数码管上,还可以加一些按键来控制步进电机的调速等等...

回答3:

很简单啊

回答4:

/*************************************************************************************
* 标题: 五轴控制步进电机试验 *
* *
* 通过本例程了解步进马达使用及驱动程序编写 *
*; 单双八拍工作方式: *
*; A-AB-B-BC-C-CD-D-DA-A (即一个脉冲,转 3.75 度) *
* J14短路冒需断开 ,通过排线连接步进马达数据接口 *
* 请学员一定要消化掉本例程,领会指针的经典应用, *
* 现象:5个步进马达以不同加速度运行,运行到不同圈数后停止 * *
**************************************************************************************/
#include "reg52.h"

unsigned char TickOut(unsigned int * tick, unsigned int tickCnt);
//unsigned char Acceleration( unsigned int *Base_Tim, unsigned int Delay_Tim,unsigned int * Flag , unsigned char* Speed) ;
void MotorRun(unsigned int *pD);

unsigned int Tick_10ms = 0; //时间片计数器

unsigned char code FFW_L[8]={~0xfe,~0xfc,~0xfd,~0xf9,~0xfb,~0xf3,~0xf7,~0xf6}; //低四位 马达相序变化表
unsigned char code FFW_H[8]={~0xef,~0xcf,~0xdf,~0x9f,~0xbf,~0x3f,~0x7f,~0x6f}; //高四位 马达相序变化表

unsigned int xdata MotorTimer1 = 0 , MotorTimer2 = 0, MotorTimer3 = 0, MotorTimer4 = 0, MotorTimer5 = 0; //马达转速基时
unsigned int xdata MotorDelay1 = 0 , MotorDelay2 = 0, MotorDelay3 = 0, MotorDelay4 = 0, MotorDelay5 = 0; TIM,CT; //马达转速

unsigned int xdata Delay_Tim1 =0, Delay_Tim2 =0, Delay_Tim3 =0, Delay_Tim4 =0, Delay_Tim5 =0; //马达加速度
unsigned int xdata Base_T1 = 0, Base_T2 = 0, Base_T3 = 0, Base_T4 = 0, Base_T5 = 0; //马达加速度基时
unsigned char xdata speed1 = 8, speed2 = 8, speed3 = 8, speed4 = 8, speed5 = 8; //马达转速
unsigned int xdata Acc_Flg1 =0, Acc_Flg2 =0, Acc_Flg3 =0, Acc_Flg4 =0, Acc_Flg5 =0; //加减速标志

/*****************************************************
* 步进马达1 *
******************************************************/

unsigned char SetMotor1(unsigned int step,unsigned int Speed ) //step 马达的步数 Speed马达速度
{
static unsigned int Circle; //马达步数
static unsigned char ret ;
MotorDelay1 = 70-Speed;
if (Circle <= step)
{
if (TickOut(&MotorTimer1 , MotorDelay1))
{ static char i;
ret = (FFW_L[i]&0x0f); //查表取马达相序
if (i++ == 7) i = 0;
++Circle;
}
}

return ret;
}

/*****************************************************
* 步进马达2 *
******************************************************/
unsigned char SetMotor2(unsigned int step,unsigned int Speed ) //step 马达的步数 Speed马达速度
{ static unsigned int Circle ; //马达步数
static unsigned char ret ;
MotorDelay2 = 70-Speed;
if (Circle <= step)
{ if(TickOut(&MotorTimer2 , MotorDelay2))
{ static char i;
ret = (FFW_H[i]&0xF0); //查表取马达相序
if (i++ == 7) i = 0;
++Circle;
}
}

return ret;
}

/*****************************************************
* 步进马达3 *
******************************************************/
unsigned char SetMotor3(unsigned int step,unsigned int Speed) //step 马达的步数 Speed马达速度
{ static unsigned int Circle ; //马达步数
static unsigned char ret ;
MotorDelay3 = 70-Speed;
if(Circle <= step)
{ if (TickOut(&MotorTimer3 , MotorDelay3))
{ static char i;
ret = FFW_L[i]&0x1f; //查表取马达相序
if (i++ == 7) i = 0;
++Circle;
}
}
return ret;
}

/*****************************************************
* 步进马达4 *
******************************************************/
unsigned char SetMotor4(unsigned int step,unsigned int Speed) //step 马达的步数 Speed马达速度
{ static unsigned int Circle ; //马达步数
static unsigned char ret ;
MotorDelay4 = 70-Speed;
if(Circle <= step)
{ if (TickOut(&MotorTimer4 , MotorDelay4))
{ static char i;
ret = FFW_H[i]&0xf0; //查表取马达相序
if (i++ == 7) i = 0;
++Circle;
}
}
return ret;
}

/*****************************************************
* 步进马达5 *
******************************************************/
/*unsigned char SetMotor5(unsigned int step,unsigned int Speed) //step 马达的步数 Speed马达速度
{ static unsigned int Circle ; //马达步数
static unsigned char ret ;
MotorDelay5 = 70-Speed;
if(Circle <= step)
{ if (TickOut(&MotorTimer5 , MotorDelay5))
{ static char i;
ret = FFW_L[i]&0x0f; //查表取马达相序
if (i++ == 7) i = 0;
++Circle;
}

}
return ret;
}*/

void MotorRun(unsigned int * pD)
{
unsigned char ret1 , ret2 , ret3 , ret4 ; //ret5;

//**************************步进马达1处理**************************
Delay_Tim1 = 50;
if(speed1 == 61) Acc_Flg1 = 2;
if(speed1 == 8) Acc_Flg1 = 1 ;
Acc_Flg1 = 1;
// Acceleration(&Base_T1 , Delay_Tim1, &Acc_Flg1 , &speed1);
ret1 = SetMotor1(*(pD+2)*96 ,*(pD+1) ) ; //参数设置步数 8个脉冲30度*100=3000度

//**************************步进马达2处理**************************
Delay_Tim2 = 250;
Acc_Flg2 = 1;
// Acceleration(&Base_T2 , Delay_Tim2, &Acc_Flg2 , &speed2);
ret2 = SetMotor2(*(pD+4)*96 , *(pD+3)) ; //马达2
P1 = ret1 | ret2 ;

//**************************步进马达3处理**************************
Delay_Tim3 = 500;
Acc_Flg3 = 1;
ret3 = SetMotor3(*(pD+6)*1500 ,*(pD+5)) ; //马达3

//**************************步进马达4处理**************************
Delay_Tim4 = 750;
Acc_Flg4 = 1;
ret4 = SetMotor4(*(pD+8)*96 , *(pD+7)) ; //马达4
P0 = ret3 | ret4 ;

//**************************步进马达5处理**************************
// Delay_Tim5 = 1000;
// Acc_Flg5 = 1;
// ret5 = SetMotor5(8*2500 , Acceleration(&Base_T5 , Delay_Tim5, &Acc_Flg5 , &speed5)); //马达5
// P2 = ret5;

}

/**************************************************************************
* *
* 各个马达的基时(相当于多个定时器) *
* 注意:体会指针的用法 ,多工定义任务同时工作,互不干扰 *
**************************************************************************/
unsigned char TickOut(unsigned int * tick, unsigned int tickCnt)
{
if(Tick_10ms - *tick >= tickCnt)
{
*tick = Tick_10ms;
return 1;
}
return 0;
}

/*************************************************************************
* *
* 调整加速度 *
* 注意:体会指针的用法 *
**************************************************************************/
/*unsigned char Acceleration(unsigned int *Base_Tim, unsigned int Delay_Tim, unsigned int * Flag , unsigned char* Speed)
{
if (TickOut(*&Base_Tim , Delay_Tim))
{
if((*Flag == 1) && (*Speed <= 61))
(*Speed)++ ;

if((*Flag == 2) && (*Speed >= 8))
(*Speed)--;
}

return *Speed;
}*/

/************************************************
* *
* 定时中断延时 *
************************************************/
/* void Tzd(void) interrupt 1
{
TH0 = 0xfe; //11.0592
TL0 = 0x33;

Tick_10ms++; //10MS时钟

}*/

/*************************************************************************************
* 标题: 步进电机试验三(加减速运行) *
* *
* 通过本例程了解步进马达使用及驱动程序编写 *
*; 单双八拍工作方式: *
*; A-AB-B-BC-C-CD-D-DA (即一个脉冲,转 3.75 度) *
* *
* 请学员一定要消化掉本例程 *
**************************************************************************************/

#include "reg52.h"

void delay();

//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, rate;

/**********************************************************
* *
* 步进电机驱动 *
***********************************************************/
void motor_ffw()
{
unsigned char i;

for (i=0; i<8; i++) //一个周期转30度
{
P1 = FFW[i]&0x1f; //取数据

delay(); //调节转速
}
}

/********************************************
延时程序
*********************************************/

void delay()
{
unsigned int k,t;
t=rate;
while(t--)
{
for(k=0; k<150; k++)
{ }
}
}

/********************************************************
*
*步进电机运行
*
*********************************************************/
void motor_turn()
{
unsigned char x;
rate=0x0a;
x=0x40;
do
{
motor_ffw(); //加速
rate--;
}while(rate!=0x01);
do
{
motor_ffw(); //匀速
x--;
}while(x!=0x01);

do
{
motor_ffw(); //减速
rate++;
}while(rate!=0x0a);
}

main()
{
while(1)
{

motor_turn();

}
}