上图为八相位时序状态图,步进电机的时序状态转换为:1001--1000--1100--0100--0110--0010--0011--0001--1001,为反序循环,并且每四个状态驱动步进电机走"one dot line",即一个点高度的整行,我使用的打印机为富士通的FTP-628MCL101,一行为384个点,所以每四个状态位步进电机走1*384个点。
下面贴上我的步进电机控制部分的代码:
for(j=0;j<steps;j++) //steps为步进电机走的点的数量
{
//一下四个GPIO口为电机驱动芯片LB1836M的输入
cur_pa = ((*(RP)GPIO_PORTG_DATA)&0x800)>>11;
cur_paa = ((*(RP)GPIO_PORTG_DATA)&0x1000)>>12;
cur_pb = ((*(RP)GPIO_PORTF_DATA)&0x004)>>2;
cur_pbb = ((*(RP)GPIO_PORTF_DATA)&0x008)>>3;
//在八个状态位之间循环
if(cur_pa==0x01 && cur_pb==0x00 && cur_paa==0x00 && cur_pbb==0x01)
{
cur_pa = 0x01;
cur_pb = 0x00;
cur_paa = 0x00;
cur_pbb = 0x00;
goto lf_move_step;
}
else if(cur_pa==0x01 && cur_pb==0x00 && cur_paa==0x00 && cur_pbb==0x00)
{
cur_pa = 0x01;
cur_pb = 0x01;
cur_paa = 0x00;
cur_pbb = 0x00;
goto lf_move_step;
}
else if(cur_pa==0x01 && cur_pb==0x01 && cur_paa==0x00 && cur_pbb==0x00)
{
cur_pa = 0x00;
cur_pb = 0x01;
cur_paa = 0x00;
cur_pbb = 0x00;
goto lf_move_step;
}
else if(cur_pa==0x00 && cur_pb==0x01 && cur_paa==0x00 && cur_pbb==0x00)
{
cur_pa = 0x00;
cur_pb = 0x01;
cur_paa = 0x01;
cur_pbb = 0x00;
goto lf_move_step;
}
else if(cur_pa==0x00 && cur_pb==0x01 && cur_paa==0x01 && cur_pbb==0x00)
{
cur_pa = 0x00;
cur_pb = 0x00;
cur_paa = 0x01;
cur_pbb = 0x00;
goto lf_move_step;
}
else if(cur_pa==0x00 && cur_pb==0x00 && cur_paa==0x01 && cur_pbb==0x00)
{
cur_pa = 0x00;
cur_pb = 0x00;
cur_paa = 0x01;
cur_pbb = 0x01;
goto lf_move_step;
}
else if(cur_pa==0x00 && cur_pb==0x00 && cur_paa==0x01 && cur_pbb==0x01)
{
cur_pa = 0x00;
cur_pb = 0x00;
cur_paa = 0x00;
cur_pbb = 0x01;
goto lf_move_step;
}
else if(cur_pa==0x00 && cur_pb==0x00 && cur_paa==0x00 && cur_pbb==0x01)
{
cur_pa = 0x01;
cur_pb = 0x00;
cur_paa = 0x00;
cur_pbb = 0x01;
goto lf_move_step;
}
else
{
cur_pa = 0x01;
cur_pb = 0x00;
cur_paa = 0x00;
cur_pbb = 0x01;
goto lf_move_step;
}
//输出信号控制电机
lf_move_step:
if(cur_pa == 1)
set_port_high(CR_M_PA);
else
set_port_low(CR_M_PA);
if(cur_pb == 1)
set_port_high(CR_M_PB);
else
set_port_low(CR_M_PB);
if(cur_paa == 1)
set_port_high(CR_M_PAA);
else
set_port_low(CR_M_PAA);
if(cur_pbb == 1)
set_port_high(CR_M_PBB);
else
set_port_low(CR_M_PBB);