二、ePWM所需配置流程与内容(每个周期进入一次中断)
Step1:初始化GPIO-ePWM2
InitEPwm2Gpio();
Step2:初始化PIE中断向量表,(使用中断方式接收上位机数据)
EALLOW; //寄存器EALLOW保护
PieVectTable.EPWM2_INT= &PWM2TickIsr;//初始化PIE中断向量表
EDIS; //禁止写如EALLOW保护的寄存器
Step3:初始化ePWM
void EPwmSetup()
{
EPwm2Regs.TBSTS.all=0;
EPwm2Regs.TBPHS.half.TBPHS =0;//设置相位寄存器置零
EPwm2Regs.TBCTR=0;//PWM时间基准(TB)计数器清零
EPwm2Regs.CMPCTL.all=0x50;
// Immediate mode for CMPA and CMPB
EPwm2Regs.CMPA.half.CMPA =SP/2;//比较器A的=SP/2个TBCLK---低电平有效-----调整占空比50%
EPwm2Regs.CMPB=0;//比较器B=SP/4个TBCLK----低电平有效、
EPwm2Regs.AQCTLA.all=0x60;// EPWMxA = 1 when CTR=CMPA and counter
inc
// EPWMxA = 0 when CTR=CMPA and counter
dec
EPwm2Regs.AQCTLB.all=0x600;//EPWMxB = 1 when CTR=CMPB and counter
inc
// EPWMxB= 0 when CTR=CMPB and counter
dec
EPwm2Regs.AQSFRC.all=0;
EPwm2Regs.AQCSFRC.all=0;
EPwm2Regs.TZSEL.all=0;
EPwm2Regs.TZCTL.all=0;
EPwm2Regs.TZEINT.all=0;
EPwm2Regs.TZFLG.all=0;
EPwm2Regs.TZCLR.all=0;
EPwm2Regs.TZFRC.all=0;
EPwm2Regs.DBCTL.all=0x23;
//死区设置 // EPWMxB is inverted
EPwm2Regs.DBRED=0;
EPwm2Regs.DBFED=0;
EPwm2Regs.ETSEL.all=9;
// Interrupt when TBCTR = 0x0000
EPwm2Regs.ETPS.all=1; // Interrupt on first event
EPwm2Regs.ETFLG.all=0;
EPwm2Regs.ETCLR.all=0;
EPwm2Regs.ETFRC.all=0;
EPwm2Regs.PCCTL.all=0;//斩波器控制寄存器
EPwm2Regs.TBCTL.all=0x0010+TBCTLVAL;
// Enable Timer
EPwm2Regs.TBPRD=SP;
//周期=SP个TBCLK
}
interrupt
void PWM2TickIsr(void)
//Interrupts once per ePWM period
{
Pulse_count++;
PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;//
第三组中断响应
EPwm2Regs.ETCLR.bit.INT=1;
//清除ePWM2中断
}
Step4:使能所需的中断
//使能所需的中断
PieCtrlRegs.PIECTRL.bit.ENPIE=1;
//使能PIE模块
//ePWM2
PieCtrlRegs.PIEIER3.bit.INTx2 = 1;
// Enable TINT0in the PIE: Group 3 interrupt 2, ePWM2
IER|= M_INT3;// 使能CPU的第三组中断--ePWM2