#define GPIO_DL_BASE GPIO_PORTH_BASE //低8位数据线
#define GPIO_DH_BASE GPIO_PORTJ_BASE //高8位数据线
#define GPIO_A_BASE GPIO_PORTF_BASE //地址线
#define GPIO_C_BASE GPIO_PORTC_BASE //控制线(CSN,RDN,WRN)
#define GPIO_BUSY_BASE GPIO_PORTC_BASE //忙信号线
#define GPIO_INT_BASE GPIO_PORTG_BASE //314中断信号线
#define DL_PIN GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
#define DH_PIN GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
#define A_PIN GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2
#define C_PIN GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6
#define CSN_PIN GPIO_PIN_4
#define RDN_PIN GPIO_PIN_5
#define WRN_PIN GPIO_PIN_6
#define BUSY_PIN GPIO_PIN_7
#define INT_PIN GPIO_PIN_0
#define WR0 0x00 //命令寄存器
#define WR1 0x01 //模式寄存器 1
#define WR2 0x02 //模式寄存器 2
#define WR3 0x03 //模式寄存器 3
#define WR4 0x04 //输出寄存器
#define WR5 0x05 //插补模式寄存器
#define WR6 0x06 //低位写入数据寄存器
#define WR7 0x07 //高位写入数据寄存器
#define RR0 0x00 //主状态寄存器
#define RR1 0x01 //状态寄存器 1
#define RR2 0x02 //状态寄存器 2
#define RR3 0x03 //状态寄存器 3
#define RR4 0x04 //输入寄存器 1
#define RR5 0x05 //输入寄存器 2
#define RR6 0x06 //低位读出数据寄存器
#define RR7 0x07 //高位读出数据寄存器
#define BP1P 0x02 //BP第 1 轴+方向数据寄存器
#define BP1M 0x03 //BP第 1 轴-方向数据寄存器
#define BP2P 0x04 //BP第 2 轴+方向数据寄存器
#define BP2M 0x05 //BP第 2 轴-方向数据寄存器
#define BP3P 0x06 //BP第 3 轴+方向数据寄存器
#define BP3M 0x07 //BP第 3 轴-方向数据寄存器
#define R 0x00 //数据范围设定(8,000,000 (倍率 1) ~16,000 (倍率 500) )
#define K 0x01 //加/减速度的变化率设定: 1~65,535
#define A 0x02 //加速度设定: 1~8,000
#define D 0x03 //减速度设定: 1~8,000
#define SV 0x04 //初始速度: 1~8,000
#define V 0x05 //驱动速度设定: 1~8,000
#define P 0x06 //输出脉冲数/插补终点设定,输出脉冲数: 0~268,435,455,插补终点: -8,388,608~+8,388,607
#define DP 0x07 //手动减速点设定: 0~268,435,455
#define C 0x08 //圆弧中心点设定: -8,388,608~+8,388,607
#define LP 0x09 //逻辑位置计数器设定: -2,147,483,648~+2,147,483,647
#define EP 0x0A //实位计数器设定: -2,147,483,648~+2,147,483,647
#define CP 0x0B //COMP+寄存器设定: -2,147,483,648~+2,147,483,647
#define CM 0x0C //COMP-寄存器设定: -2,147,483,648~+2,147,483,647
#define AO 0x0D //加速计数器偏移设定: 0~65,535
#define NOP 0x0F //NOP 轴切换用
void BusInit(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOH); //使能IO时钟
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
GPIOPadConfigSet(GPIO_DL_BASE, DL_PIN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD); //驱动电流2MA,推挽方式
GPIODirModeSet(GPIO_DL_BASE, DL_PIN, GPIO_DIR_MODE_OUT); //输出模式
GPIOPadConfigSet(GPIO_DH_BASE, DH_PIN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
GPIODirModeSet(GPIO_DH_BASE, DH_PIN, GPIO_DIR_MODE_OUT);
GPIOPadConfigSet(GPIO_A_BASE, A_PIN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
GPIODirModeSet(GPIO_A_BASE, A_PIN, GPIO_DIR_MODE_OUT);
GPIOPadConfigSet(GPIO_PORTC_BASE, C_PIN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
GPIODirModeSet(GPIO_PORTC_BASE, C_PIN, GPIO_DIR_MODE_OUT);
GPIOPadConfigSet(GPIO_BUSY_BASE, BUSY_PIN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
GPIODirModeSet(GPIO_BUSY_BASE, BUSY_PIN, GPIO_DIR_MODE_IN);
GPIOPadConfigSet(GPIO_INT_BASE, INT_PIN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
GPIODirModeSet(GPIO_INT_BASE, INT_PIN, GPIO_DIR_MODE_IN);
}
void WaitBusy(void)
{
unsigned char stat;
stat = GPIOPinRead(GPIO_BUSY_BASE, BUSY_PIN);
while((stat&0x80)!=0x80);
}
void BusWriteData(unsigned char addr, unsigned int wdata)
{
//数据输出
GPIODirModeSet(GPIO_DL_BASE, DL_PIN, GPIO_DIR_MODE_OUT);
GPIODirModeSet(GPIO_DH_BASE, DH_PIN, GPIO_DIR_MODE_OUT);
//使能片选
GPIOPinWrite(GPIO_C_BASE, CSN_PIN, ~CSN_PIN);
GPIOPinWrite(GPIO_C_BASE, RDN_PIN, RDN_PIN);
//写地址
GPIOPinWrite(GPIO_A_BASE, A_PIN, addr&0x07);
//使能写信号
GPIOPinWrite(GPIO_C_BASE, WRN_PIN, ~WRN_PIN);
//写数据
GPIOPinWrite(GPIO_DL_BASE, DL_PIN, (unsigned char)wdata);
GPIOPinWrite(GPIO_DH_BASE, DH_PIN, (unsigned char)(wdata>>8));
_NOP();
//释放写信号线
GPIOPinWrite(GPIO_C_BASE, WRN_PIN, WRN_PIN);
_NOP();
//WaitBusy();
//释放片选
GPIOPinWrite(GPIO_C_BASE, CSN_PIN, CSN_PIN);
}
unsigned int BusReadData(unsigned char addr)
{
unsigned int rdata;
//数据输入
GPIODirModeSet(GPIO_DL_BASE, DL_PIN, GPIO_DIR_MODE_IN);
GPIODirModeSet(GPIO_DH_BASE, DH_PIN, GPIO_DIR_MODE_IN);
//使能片选
GPIOPinWrite(GPIO_C_BASE, CSN_PIN, ~CSN_PIN);
GPIOPinWrite(GPIO_C_BASE, WRN_PIN, WRN_PIN);
//使能片选,读信号
GPIOPinWrite(GPIO_C_BASE, RDN_PIN, ~RDN_PIN);
//写地址
GPIOPinWrite(GPIO_A_BASE, A_PIN, addr&0x07);
_NOP();
//读数据
rdata = GPIOPinRead(GPIO_DH_BASE, DH_PIN);
rdata = rdata << 8;
rdata = rdata | GPIOPinRead(GPIO_DL_BASE, DL_PIN);
_NOP();
//释放片选,读信号线
GPIOPinWrite(GPIO_C_BASE, RDN_PIN, RDN_PIN);
GPIOPinWrite(GPIO_C_BASE, CSN_PIN, CSN_PIN);
return rdata;
}
//写入寄存器 1 设定
void wreg1(int axis, int wdata)
{
BusWriteData(WR0,(axis<<8) + NOP);
BusWriteData(WR1, wdata);
}
//写入寄存器 2 设定
void wreg2(int axis, int wdata)
{
BusWriteData(WR0,(axis<<8) + NOP);
BusWriteData(WR2, wdata);
}
//写入寄存器 3 设定
void wreg3(int axis, int wdata)
{
BusWriteData(WR0,(axis<<8) + NOP);
BusWriteData(WR3, wdata);
}
//命令写入
void command(int axis, int cmd)
{
BusWriteData(WR0,(axis<<8) + cmd);
}
//范围 R 设定
void range(int axis, long wdata)
{
BusWriteData(WR7,(wdata>>16)&0xffff);
BusWriteData(WR6,wdata & 0xffff);
BusWriteData(WR0,(axis<<8)+ R);
}
//变化率(K)设定
void acac(int axis, int wdata)
{
BusWriteData(WR6,wdata);
BusWriteData(WR0,(axis<<8) + K);
}
void dcac(int axis, int wdata)
{
BusWriteData(WR6,wdata);
BusWriteData(WR0,(axis<<8) + 0x0e);
}
//加/减速度(A)设定
void acc(int axis, int wdata)
{
BusWriteData(WR6,wdata);
BusWriteData(WR0,(axis<<8) + A);
}
//减速度 D 设定
void dec(int axis, int wdata)
{
BusWriteData(WR6, wdata);
BusWriteData(WR0,(axis<<8)+ D);
}
//初始速度 SV 设定
void startv(int axis, int wdata)
{
BusWriteData(WR6,wdata);
BusWriteData(WR0,(axis<<8)+ SV);
}
//驱动速度 V 设定
void speed(int axis, int wdata)
{
BusWriteData(WR6, wdata);
BusWriteData(WR0,(axis<<8)+ V);
}
//输出脉冲数/终点 P 设定
void pulse(int axis, long wdata)
{
BusWriteData(WR7,(wdata>>16)& 0xffff);
BusWriteData(WR6,wdata & 0xffff);
BusWriteData(WR0,(axis<<8) + P);
}
//手动减速点 DP 设定
void decp(int axis, long wdata)
{
BusWriteData(WR7,(wdata>>16)& 0xffff);
BusWriteData(WR6,wdata & 0xffff);
BusWriteData(WR0,(axis<<8) + DP);
}
//圆弧中心点 C 设定
void center(int axis, long wdata)
{
BusWriteData(WR7,(wdata>>16)& 0xffff);
BusWriteData(WR6,wdata & 0xffff);
BusWriteData(WR0,(axis<<8) + C);
}
//逻辑位置计数器(LP)设定
void lp(int axis, long wdata)
{
BusWriteData(WR7,(wdata>>16)& 0xffff);
BusWriteData(WR6,wdata & 0xffff);
BusWriteData(WR0,(axis<<8) + LP);
}
//逻辑位置计数器(EP)设定
void ep(int axis, long wdata)
{
BusWriteData(WR7,(wdata>>16)&0xffff);
BusWriteData(WR6,wdata & 0xffff);
BusWriteData(WR0,(axis<<8) + EP);
}
//COMP + (CP) 设定
void compp(int axis, long wdata)
{
BusWriteData(WR7,(wdata>>16)&0xffff);
BusWriteData(WR6,wdata & 0xffff);
BusWriteData(WR0,(axis<<8) + CP);
}
//COMP - (CM) 设定
void compm(int axis, long wdata)
{
BusWriteData(WR7,(wdata>>16)&0xffff);
BusWriteData(WR6,wdata & 0xffff);
BusWriteData(WR0,(axis<<8) + CM);
}
//加速计数器偏移(AO)设定
void accofst(int axis, long wdata)
{
BusWriteData(WR7,(wdata>>16)& 0xffff);
BusWriteData(WR6,wdata & 0xffff);
BusWriteData(WR0,(axis<<8) + AO);
}
void hsspeed(int axis, int wdata)
{
BusWriteData(WR6,wdata);
BusWriteData(WR0,(axis<<8)+ 0x61);
}
void expmode(int axis, int em6data, int em7data)
{
BusWriteData(WR6,em6data);
BusWriteData(WR7,em7data);
BusWriteData(WR0,(axis<<8) + 0x60);
}
//逻辑位置计数器数值(LP)读出
long readlp(int axis)
{
long a; long d6; long d7;
BusWriteData(WR0,(axis<<8) + 0x10);
d6 = BusReadData(RR6);
d7 = BusReadData(RR7);
a = d6 + (d7<<16);
return(a);
}
//逻辑位置计数器数值(EP)读出
long readep(int axis)
{
long a; long d6; long d7;
BusWriteData(WR0,(axis<<8) + 0x11);
d6 = BusReadData(RR6);
d7 = BusReadData(RR7);
a = d6 + (d7<<16);
return(a);
}
//等驱动结束
void wait(int axis)
{
while(BusReadData(RR0) & axis);
}
//等连续插补下一个数据设定
void next_wait(void)
{
while((BusReadData(RR0) & 0x0200) == 0);
}
//等 BP 插补下一个数据设定
void bp_wait(void)
{
while((BusReadData(RR0) & 0x6000) == 0x6000);
}
//全轴原点检出
void homesrch(void)
{
wreg1(0xf, 0x0008);
speed(0xf, 2000);
if((BusReadData(RR4) & 0x2) == 0x2)
{
command(0x1, 0x23);
}
if((BusReadData(RR4) & 0x200) == 0x200)
{
command(0x2, 0x23);
}
if((BusReadData(RR5) & 0x2) == 0x2)
{
command(0x4, 0x23);
}
if((BusReadData(RR5) & 0x200) == 0x200)
{
command(0x8, 0x23);
}
wait(0xf);
wreg1(0xf, 0x000c);
speed(0xf, 50);
command(0xf, 0x22);
wait(0xf);
wreg1(0xf, 0x0000);
speed(0xf, 4000);
pulse(0xf, 100);
command(0xf, 0x21);
wait(0xf);
lp(0xf, 0);
wreg2(0x7, 0x0003);
compp(0x1, 100000);
compm(0x1, -1000);
compp(0x2, 50000);
compm(0x2, -500);
compp(0x4, 10000);
compm(0x4, -100);
}
int main(void)
{
unsigned int cnt;
SysCtlClockSet(SYSCTL_SYSDIV_2_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_4MHZ);
BusInit();
BusWriteData(WR0, 0x8000); //软件复位
for(cnt=0;cnt<2;cnt++);
command(0x3, 0xf);
BusWriteData(WR1, 0x0000);
BusWriteData(WR2, 0xe000);
BusWriteData(WR3, 0x0000);
expmode(0x3, 0x5d08, 0x497f);
accofst(0x3, 0); //A0 = 0
range(0x3, 800000); //R = 800,000(倍率 = 10)
acac(0x3, 1010); //K = 1,010(变化率 = 619KPPS/SEC2)
dcac(0x3, 1010); //L=1010
acc(0x3, 100); //A = 100(加/减速度=125KPPS/SEC)
dec(0x3, 100); //D = 100(减速度 = 125KPPS/SEC)
startv(0x3, 100); //SV = 100(初始速度 = 1,000PPS)
speed(0x3, 4000); //V = 4,000(驱动速度 = 40,000PPS)
pulse(0x3, 100000); //P = 100,000(输出脉冲数 = 100,000)
lp(0x3, 0); //LP = 0(逻辑位置计数器 = 0)
ep(0x3, 0); //EP=0
homesrch();
acc(0x3, 200);
speed(0x3, 4000);
pulse(0x1, 80000);
pulse(0x2, 40000);
command(0x3, 0x20);
wait(0x3);
wreg3(0x3, 0x0004);
acac(0x3, 1010);
acc(0x3, 200);
speed(0x3, 4000);
pulse(0x1, 50000);
pulse(0x2, 25000);
command(0x3, 0x21);
wait(0x3);
while(1)
{
;
}
}
程序运行到homesrch函数里面的wait(0xf);就死在这里了,不知道是不是我写的代码时序有问题,第一次使用这款芯片,弄了好久不知道问题在哪,请高手解答~~~
此帖出自
小平头技术问答
一周热门 更多>