本帖最后由 halou 于 2015-3-26 09:56 编辑
这个例程中的中断怎么样会实现循环的呢?timer会++,我改哪儿就能让这个程序电机停止呢?
这是例程源代码,方便没有这个例程的大神,从这儿看一下
/********************************************************************
* 文件名: 直流电机控制程序
* 描述: 执行该程序,直流电机旋转
***********************************************************************/
/********************************************************************
程序说明:1.直流电机启动时需要一个大的电流,所以在程序中,启动时给了一个较大的占空比,且维持一段时间
2.直流电机启动后,将占空比调小,保证电机平稳低速运行
3.【电机转速不能太高,否则电磁干扰,仿真器容易掉线】
********************************************************************/
#include "DSP2833x_Device.h" // Headerfile Include File
#include "DSP2833x_Examples.h" // Examples Include File
typedef struct
{
volatile struct EPWM_REGS *EPwmRegHandle;
Uint16 EPwm_CMPA_Direction;
Uint16 EPwm_CMPB_Direction;
Uint16 EPwmTimerIntCount;
Uint16 EPwmMaxCMPA;
Uint16 EPwmMinCMPA;
Uint16 EPwmMaxCMPB;
Uint16 EPwmMinCMPB;
}EPWM_INFO;
// Prototype statements for functions found within this file.
void InitEPwm4Example(void);
interrupt void epwm4_isr(void);
void update_compare(EPWM_INFO*);
// Global variables used in this example
EPWM_INFO epwm4_info;
// Configure the period for each timer
#define EPWM4_TIMER_TBPRD 10000 // Period register
#define EPWM4_MAX_CMPA 9000
#define EPWM4_MIN_CMPA 0
#define EPWM4_MAX_CMPB 9000
#define EPWM4_MIN_CMPB 0
// To keep track of which way the compare value is moving
#define EPWM_CMP_UP 1
#define EPWM_CMP_DOWN 0
void main(void)
{
.
InitSysCtrl();
EALLOW;
GpioCtrlRegs.GPAMUX1.all = 0x0; // GPIO pin
GpioCtrlRegs.GPADIR.all = 0xFF; // Output pin
GpioDataRegs.GPADAT.all =0xFF; // Close LEDs
EDIS;
// For this case just init GPIO pins for ePWM1, ePWM2, EPwm4
// These functions are in the DSP280x_EPwm.c file
InitEPwm4Gpio();
DINT;
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
EALLOW;
PieVectTable.EPWM4_INT = &epwm4_isr;
EDIS;
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP280x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
// For this example, only initialize the ePWM
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
EDIS;
InitEPwm4Example();
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
EDIS;
// Step 5. User specific code, enable interrupts:
// Enable CPU INT3 which is connected to EPWM1-3 INT:
IER |= M_INT3;
// Enable EPWM INTn in the PIE: Group 3 interrupt 1-3
PieCtrlRegs.PIEIER3.bit.INTx4 = 1;
// Enable global Interrupts and higher priority real-time debug events:
EINT;
ERTM;
// Step 6. IDLE loop. Just sit and loop forever (optional):
for(;;)
{
}
}
interrupt void epwm4_isr(void)
{
// Update the CMPA and CMPB values
update_compare(&epwm4_info);
// Clear INT flag for this timer
EPwm4Regs.ETCLR.bit.INT = 1;
// Acknowledge this interrupt to receive more interrupts from group 3
PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
}
void InitEPwm4Example(void)
{
// Setup TBCLK
EPwm4Regs.TBPRD = EPWM4_TIMER_TBPRD; // Set timer period 801 TBCLKs
EPwm4Regs.TBPHS.half.TBPHS = 0x0000; // Phase is 0
EPwm4Regs.TBCTR = 0x0000; // Clear counter
// Set Compare values
EPwm4Regs.CMPA.half.CMPA = EPWM4_MIN_CMPA; // Set compare A value
EPwm4Regs.CMPB = EPWM4_MAX_CMPB; // Set Compare B value
// Setup counter mode
EPwm4Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Count up
EPwm4Regs.TBCTL.bit.PHSEN = TB_DISABLE; // Disable phase loading
EPwm4Regs.TBCTL.bit.HSPCLKDIV = TB_DIV4; // Clock ratio to SYSCLKOUT
EPwm4Regs.TBCTL.bit.CLKDIV = TB_DIV4;
// Setup shadowing
EPwm4Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
EPwm4Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
EPwm4Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; // Load on Zero
EPwm4Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;
// Set actions
EPwm4Regs.AQCTLA.bit.CAU = AQ_SET; // Set PWM1A on event A, up count
EPwm4Regs.AQCTLA.bit.CAD = AQ_CLEAR; // Clear PWM1A on event A, down count
EPwm4Regs.AQCTLB.bit.CBU = AQ_SET; // Set PWM1B on event B, up count
EPwm4Regs.AQCTLB.bit.CBD = AQ_CLEAR; // Clear PWM1B on event B, down count
// Interrupt where we will change the Compare Values
EPwm4Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO; // Select INT on Zero event
EPwm4Regs.ETSEL.bit.INTEN = 1; // Enable INT
EPwm4Regs.ETPS.bit.INTPRD = ET_1ST; // Generate INT on 3rd event
// Information this example uses to keep track
// of the direction the CMPA/CMPB values are
// moving, the min and max allowed values and
// a pointer to the correct ePWM registers
epwm4_info.EPwm_CMPA_Direction = EPWM_CMP_UP; // Start by increasing CMPA &
epwm4_info.EPwm_CMPB_Direction = EPWM_CMP_DOWN; // decreasing CMPB
epwm4_info.EPwmTimerIntCount = 0; // Zero the interrupt counter
epwm4_info.EPwmRegHandle = &EPwm4Regs; // Set the pointer to the ePWM module
epwm4_info.EPwmMaxCMPA = EPWM4_MAX_CMPA; // Setup min/max CMPA/CMPB values
epwm4_info.EPwmMinCMPA = EPWM4_MIN_CMPA;
epwm4_info.EPwmMaxCMPB = EPWM4_MAX_CMPB;
epwm4_info.EPwmMinCMPB = EPWM4_MIN_CMPB;
}
/*****************************直流电机控制子程序*******************************/
Uint16 Direction = 0; //【旋转方向设置】
int32 CmpA = 0; //占空比A
int32 CmpB = 0; //占空比B
int32 Cw = 0; //正转占空比
int32 Ccw= 0; //反转占空比
void MotorControl(void);
void MotorControl(void)
{
static Uint32 timer = 0;
Ccw= 0;
if(timer <= 900)
{
Cw+=15; //占空比逐渐增大,加在电机上的电压增大,电机启动
if(Cw>=8500)
{
Cw = 8500; //占空比8500/10000保持一段时间,保证电机启动(因为直流电机启动需要瞬间大电流)。
}
}
else //
{
Cw = 5000; //当电机启动后,减小占空比,电机以低速运行。
if(timer>=1000)
{
timer = 1000;
}
}
timer++;
if(Direction == 0)
{
CmpA = Cw;
CmpB = Ccw;
}
else
{
CmpA = Ccw;
CmpB = Cw;
}
}
/********************************************************************************************/
void update_compare(EPWM_INFO *epwm_info)
{
MotorControl();
epwm_info->EPwmRegHandle->CMPA.half.CMPA = CmpA;
epwm_info->EPwmRegHandle->CMPB = CmpB;
return;
}
|