2019-08-03 17:12发布
你一笑,粉直掉 发表于 2014-2-25 19:09 就是我想用cputimer中断来代替delay延时来控制步进电机转速,但我不知道怎么修改这个程序,改了也有好多错 ...
zhangmangui 发表于 2014-2-25 19:41 你先用delay的方式 让电机转起来 确保修改delay的大小改变转速 然后调一下CPU定时器中断 在中断中去 ...
最多设置5个标签!
#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File
#define PHA_ON GpioDataRegs.GPASET.bit.GPIO8 = 1 //A相输出高电平
#define PHA_OFF GpioDataRegs.GPACLEAR.bit.GPIO8 = 1 //A相输出低电平
#define PHB_ON GpioDataRegs.GPASET.bit.GPIO13 = 1 //B相输出高电平
#define PHB_OFF GpioDataRegs.GPACLEAR.bit.GPIO13 = 1 //B相输出低电平
#define PHC_ON GpioDataRegs.GPASET.bit.GPIO10 = 1 //C相输出高电平
#define PHC_OFF GpioDataRegs.GPACLEAR.bit.GPIO10 = 1 //C相输出低电平
#define PHD_ON GpioDataRegs.GPASET.bit.GPIO14 = 1 //D相输出高电平
#define PHD_OFF GpioDataRegs.GPACLEAR.bit.GPIO14 = 1 //D相输出低电平
#define STEP_TIME 15000
const Uint16 StepMode1[8] = {0x0D0A,0x0D0A,0x0A0B,0x0A0B,0x0B0C,0x0B0C,0x0C0D,0x0C0D}; //单四拍时序A-B-C-D-A
void delay(Uint32 t);
void MotionControl1(void);
void PhaseOff(Uint16 i);
void PhaseOn(Uint16 i);
void Init_StepMotorGpio(void)
{
EALLOW;
//Phase A
GpioCtrlRegs.GPAPUD.bit.GPIO8 = 0; // Enable pullup on GPIO11
GpioDataRegs.GPASET.bit.GPIO8 = 1; // Load output latch
GpioCtrlRegs.GPAMUX1.bit.GPIO8 = 0; // GPIO11 = GPIO
GpioCtrlRegs.GPADIR.bit.GPIO8 = 1; // GPIO11 = output
//Phase B
GpioCtrlRegs.GPAPUD.bit.GPIO13 = 0; // Enable pullup on GPIO11
GpioDataRegs.GPASET.bit.GPIO13 = 1; // Load output latch
GpioCtrlRegs.GPAMUX1.bit.GPIO13 = 0; // GPIO11 = GPIO
GpioCtrlRegs.GPADIR.bit.GPIO13 = 1; // GPIO11 = output
//Phase C
GpioCtrlRegs.GPAPUD.bit.GPIO10 = 0; // Enable pullup on GPIO11
GpioDataRegs.GPASET.bit.GPIO10 = 1; // Load output latch
GpioCtrlRegs.GPAMUX1.bit.GPIO10 = 0; // GPIO11 = GPIO
GpioCtrlRegs.GPADIR.bit.GPIO10 = 1; // GPIO11 = output
//Phase D
GpioCtrlRegs.GPAPUD.bit.GPIO14 = 0; // Enable pullup on GPIO11
GpioDataRegs.GPASET.bit.GPIO14 = 1; // Load output latch
GpioCtrlRegs.GPAMUX1.bit.GPIO14 = 0; // GPIO11 = GPIO
GpioCtrlRegs.GPADIR.bit.GPIO14 = 1; // GPIO11 = output
EDIS;
PHA_OFF;
PHB_OFF;
PHC_OFF;
PHD_OFF;
}
void delay(Uint32 t)
{
Uint32 i = 0;
for (i = 0; i < t; i++);
}
void PhaseOn(Uint16 i)
{
switch(i)
{
case 0xA:
PHA_ON;
break;
case 0xB:
PHB_ON;
break;
case 0xC:
PHC_ON;
break;
case 0xD:
PHD_ON;
break;
case 0x0:
break;
default:
break;
}
}
void PhaseOff(Uint16 i)
{
switch(i)
{
case 0xA:
PHA_OFF;
break;
case 0xB:
PHB_OFF;
break;
case 0xC:
PHC_OFF;
break;
case 0xD:
PHD_OFF;
break;
case 0x0:
break;
default:
break;
}
}
void MotionControl1(void)
{
Uint16 loop = 0;
for(loop = 0;loop<=7;loop++)
{
PhaseOn (StepMode1[loop] & 0x000F); //第一位:控制信号高电平判断
PhaseOn ((StepMode1[loop] & 0x00F0)>>4); //第二位:控制信号高电平判断
PhaseOff((StepMode1[loop] & 0x0F00)>>8); //第三位:控制信号低电平判断
PhaseOff((StepMode1[loop] & 0xF000)>>12); //第四位:控制信号低电平判断
delay(STEP_TIME);
}
}
void main(void)
{
InitSysCtrl();
Init_StepMotorGpio();
DINT;
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
while(1)
{
MotionControl1();
}
}
如果是控制驱动器, 就是调节脉冲输出罢了
你先用delay的方式 让电机转起来 确保修改delay的大小改变转速
然后调一下CPU定时器中断 在中断中去送下一个脉冲就可以啦
谢谢你的回答,但我还不是很理解,我想得到一个确切的转速值,有个公式计算转速:n=f*60/T,不知道如何来正确编写这样符合要求的程序
一周热门 更多>