/********************************************************************
* 文件名: 步进电机驱动程序
* 描述: 执行该程序,步进电机旋转。例程编写了3种控制方式。
调用不同的控制时序程序,观察控制效果
**********************************************************************/
/********************************************************************
程序说明:
********************************************************************/
#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.GPIO10 = 1 //B相输出高电平
#define PHB_OFF GpioDataRegs.GPACLEAR.bit.GPIO10 = 1 //B相输出低电平
#define PHC_ON GpioDataRegs.GPASET.bit.GPIO13 = 1 //C相输出高电平
#define PHC_OFF GpioDataRegs.GPACLEAR.bit.GPIO13 = 1 //C相输出低电平
#define PHD_ON GpioDataRegs.GPASET.bit.GPIO14 = 1 //D相输出高电平
#define PHD_OFF GpioDataRegs.GPACLEAR.bit.GPIO14 = 1 //D相输出低电平
#define DELAY_TIME 750000 //延时时间
#define STEP_TIME 15000 //步距
/*****************************************************************************************************/
/************************************定义步进电机运行时序**********************************************/
//16进制数的高位代表低电平时序,低位代表高电平时序。
//例如:0x0D0A-------------------------------->0x0000---------------------------------->0x0A0B
// D相低电平,A相高电平------------------>保持电平状态---------------------------->A相低电平,B相高电平
const Uint16 StepMode1[8] = {0x0D0A,0x0D0A,0x0A0B,0x0A0B,0x0B0C,0x0B0C,0x0C0D,0x0C0D}; //单四拍时序A-B-C-D-A
const Uint16 StepMode2[8] = {0xCDAB,0xDABC,0xABCD,0xBCDA,0xCDAB,0xDABC,0xABCD,0xBCDA}; //双四拍时序AB-BC-CD-DA-AB
const Uint16 StepMode3[8] = {0x0D0A,0x0DAB,0x0A0B,0x0ABC,0x0B0C,0x0BCD,0x0C0D,0x0CDA}; //八拍时序A-AB-B-BC-C-CD-D-DA-A
/*********************************************函数声明************************************************/
void Init_LedGpio(void);
void delay(Uint32 t);
void MotionControl1(void);
void MotionControl2(void);
void MotionControl3(void);
void PhaseOff(Uint16 i);
void PhaseOn(Uint16 i);
/*****************************************************************************************************/
/***************************************电机控制IO初始化************************************************/
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++);
}
/*****************************************************************************************************/
/******************************************电机控制IO高电平输出***************************************/
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;
}
}
/*****************************************************************************************************/
/******************************************电机控制IO低电平输出***************************************/
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);
}
}
/*****************************************************************************************************/
/******************************************双四拍控制时序*********************************************/
/*
分析了下,这个函数跑一次,4个线圈是这样的 4相5线步进电机
我的理解是 跑一次电机就该转两圈了吧 可是实际跑64次才转一圈
A 1 0 0 1 1 0 0 1
B 1 1 0 0 1 1 0 0
C 0 1 1 0 0 1 1 0
D 0 0 1 1 0 0 1 1
*/
void MotionControl2(void)
{
Uint16 loop = 0;
for(loop = 0;loop<=7;loop++)
{
PhaseOff((StepMode2[loop] & 0x0F00)>>8); //第三位:控制信号低电平判断
PhaseOff((StepMode2[loop] & 0xF000)>>12); //第四位:控制信号低电平判断
delay(STEP_TIME);
PhaseOn (StepMode2[loop] & 0x000F); //第一位:控制信号高电平判断
PhaseOn ((StepMode2[loop] & 0x00F0)>>4); //第二位:控制信号高电平判断
delay(STEP_TIME);
}
}
/*****************************************************************************************************/
/******************************************八拍控制时序*********************************************/
void MotionControl3(void)
{
Uint16 loop = 0;
for(loop = 0;loop<=7;loop++)
{
PhaseOn (StepMode3[loop] & 0x000F); //第一位:控制信号高电平判断
PhaseOn ((StepMode3[loop] & 0x00F0)>>4); //第二位:控制信号高电平判断
PhaseOff((StepMode3[loop] & 0x0F00)>>8); //第三位:控制信号低电平判断
PhaseOff((StepMode3[loop] & 0xF000)>>12); //第四位:控制信号低电平判断
delay(STEP_TIME);
}
}
/*****************************************************************************************************/
void main(void)
{
Uint16 i=0;
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP280x_SysCtrl.c file.
InitSysCtrl();
// Step 2. Initalize GPIO:
// This example function is found in the DSP280x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio(); // Skipped for this example
// For this example use the following configuration:
Init_StepMotorGpio(); //初始化LED的GPIO
//Buzz_Gpio_Init();
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP280x_PieCtrl.c file.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP280x_DefaultIsr.c.
// This function is found in DSP280x_PieVect.c.
InitPieVectTable();
while(1) //死循环
{
for(i=0;i<64;i++)
MotionControl2();
delay(1500);
}
}
|