|
发表于 2012-4-10 11:48:08
|
显示全部楼层
本帖最后由 txzfw 于 2012-4-10 11:55 编辑
转发另一个A3972的底层驱动程序,地址为:http://www.hackchina.com/dlpre.php?id=38701
代码如下见附件A3972_Drive.rar;
我自己参照人家修改后的代码如下:
#ifndef _A3992_XY_DIRVE_
#define _A3992_XY_DIRVE_
#define MOTOR_DATA_LEN 128
void A3992_xy_Init(void);
void A3992_x_mStep(uc dir,uc step_num);
void A3992_y_mStep(uc dir,uc step_num);
#endif
#include"Head.h"
uc SinTable[32]={0x3F,0x3F,0x3F,0x3E,0x3E,0x3D,0x3C,0x3B,
0x3A,0x38,0x37,0x35,0x33,0x31,0x2F,0x2D,
0x2B,0x29,0x26,0x24,0x21,0x1E,0x1B,0x18,
0x16,0x13,0x10,0x0C,0x09,0x06,0x03,0x01};
uc Bridge_Dir[4]={0x00,0x01,0x03,0x02}; //控制电流方向
void Send_Data(uc num,uc value)
{
uc temp=0x00,count=0x01;
count <<= (num - 1);
for(temp=0; temp<num; temp++) //向数据线写入Count位
{
A3992_CLOCK = 0;
_nop_();
A3992_DATA = (bit)(value&count);
_nop_();
A3992_CLOCK =1;
_nop_();
value <<= 1;
}
}
void WriteWord0(uc Set,uc H2_DAC,uc H1_DAC )
{
Send_Data(6,Set);
Send_Data(6,H2_DAC);
Send_Data(7,H1_DAC<<1);
}
void WriteWord1(uc Set,uc FastDecay,uc OffTime,uc BlakTime)
{
Send_Data(7,Set);
Send_Data(4,FastDecay);
Send_Data(5,OffTime);
Send_Data(3,(BlakTime<<1)|0x01);
}
void A3992_xy_Init(void)
{
A3992_STROBEX = 0;
WriteWord1(0x40,15,0x0C,1);
A3992_STROBEX = 1;
_nop_();
A3992_STROBEY = 0;
WriteWord1(0x40,15,0x0C,1);
A3992_STROBEY = 1;
}
void A3992_Send_Word(uc m_step,uc Half_Current)
{
if((m_step<32)||((m_step>63)&&(m_step<96)))
{
if(Half_Current)
{
WriteWord0(Bridge_Dir[m_step/32],SinTable[31-m_step%32],SinTable[m_step%32]);
}
else
{
WriteWord0(Bridge_Dir[m_step/32]|0x20,SinTable[31-m_step%32],SinTable[m_step%32]);
}
}
else
{
if(Half_Current)
{
WriteWord0(Bridge_Dir[m_step/32],SinTable[m_step%32],SinTable[31-m_step%32]);
}
else
{
WriteWord0(Bridge_Dir[m_step/32]|0x20,SinTable[m_step%32],SinTable[31-m_step%32]);
}
}
}
void A3992_x_mStep(uc dir,uc step_num)
{
static uc XData_step=0;
step_num<<=1;
if(dir==NEGATIVE_DIR)
{
XData_step+=step_num;
if(XData_step >= MOTOR_DATA_LEN)
{
XData_step -=MOTOR_DATA_LEN;
}
}
else
{
if(XData_step>=step_num)
{
XData_step -= step_num;
}
else
{
XData_step = XData_step+MOTOR_DATA_LEN-step_num;
}
}
A3992_STROBEX = 0;
A3992_Send_Word(XData_step,0);
A3992_STROBEX = 1;
}
void A3992_y_mStep(uc dir,uc step_num)
{
static uc YData_step=0;
step_num<<=1;
if(dir==NEGATIVE_DIR)
{
YData_step+=step_num;
if(YData_step >= MOTOR_DATA_LEN)
{
YData_step -=MOTOR_DATA_LEN;
}
}
else
{
if(YData_step>=step_num)
{
YData_step -= step_num;
}
else
{
YData_step = YData_step+MOTOR_DATA_LEN-step_num;
}
}
A3992_STROBEY = 0;
A3992_Send_Word(YData_step,1);
A3992_STROBEY = 1;
}
还请各位不惜赐教,指正.
我只是简单的看别人的代码写了驱动。至于细分波形与噪音的关系,速度与衰减模式的关系,衰减的原理,关断时间,空白时间等这些名词弄了很久都没有弄懂!
陈老师说的很对,电机抓起来不难!难得是 加减速与噪音两大问题!陈老师 可否对【细分波形与噪音的关系,速度与衰减模式的关系,衰减的原理,关断时间,空白时间】推荐一些资料? |
|