//ePWM初始化
void InitEPwm(void)//3750
{ EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0; //ePWM模块的基准时钟停止
EDIS;
EPwm1Regs.TBSTS.all=0; //复位时间基准状态寄存器
EPwm1Regs.TBPHS.half.TBPHS=0; //复位时间基准相位寄存器
EPwm1Regs.TBCTR=0; //复位时间基准计数寄存器
//EPwm1Regs.CMPCTL.all=0x50; // CMPA和CMB的寄存器操作模式选择直接模式,仅有主比较寄存器
EPwm1Regs.CMPCTL.bit.LOADAMODE=CC_CTR_ZERO; // CMPA和CMB的寄存器操作模式选择直接模式,仅有主比较寄存器,周期装载
EPwm1Regs.CMPCTL.bit.LOADBMODE=CC_CTR_ZERO;
EPwm1Regs.CMPCTL.bit.SHDWAMODE=CC_SHADOW; //使能映射
EPwm1Regs.CMPCTL.bit.SHDWBMODE=CC_SHADOW;
EPwm1Regs.CMPA.half.CMPA=SP/2; //初始化计数比较A寄存器EPwm1Regs.CMPA.half.CMPA =0;
EPwm1Regs.CMPB=SP/2; //初始化计数比较B寄存器EPwm1Regs.CMPB=SP/2-300;//SP/2=3750
EPwm1Regs.AQCTLA.bit.ZRO=AQ_SET; // 当CTR=CMPA 且,EPWMxA =0;当CTR=CMPB 且递减计数时,EPWMxB =0
EPwm1Regs.AQCTLA.bit.CAU=AQ_CLEAR;
EPwm1Regs.AQCTLB.bit.ZRO=AQ_CLEAR;
EPwm1Regs.AQCTLB.bit.CBU=AQ_SET;
EPwm1Regs.AQSFRC.all=0;
EPwm1Regs.AQCSFRC.all=0;
EPwm1Regs.DBCTL.all=0x0B;//0xb; // EPWMxB可以反相,死区对于ePWM1A输出的上升沿延迟和ePWM输出的下降沿延迟完全使能
EPwm1Regs.DBRED=300;//SP/5; //设置上升沿延时
EPwm1Regs.DBFED=300;//SP/5; //设置下降沿延时
EPwm1Regs.TZSEL.all=0; //错误控制和状态相关的寄存器
EPwm1Regs.TZCTL.all=0;
EPwm1Regs.TZEINT.all=0;
EPwm1Regs.TZFLG.all=0;
EPwm1Regs.TZCLR.all=0;
EPwm1Regs.TZFRC.all=0;
EPwm1Regs.ETSEL.bit.INTEN=1; //启动周期中断
EPwm1Regs.ETSEL.bit.INTSEL=2; EPwm1Regs.ETPS.bit.INTPRD=1;
EPwm1Regs.ETCLR.bit.INT=1; //清楚中断标志
EPwm1Regs.ETFLG.all=0; //事件触发标志位,只读
//EPwm1Regs.ETCLR.all=0; //清楚事件触发标志位,写0无效,写1清除
EPwm1Regs.ETFRC.all=0; //此寄存器用于测试可不管
EPwm1Regs.PCCTL.all=0; //禁用PWM斩波功能
EPwm1Regs.TBCTL.bit.CTRMODE=TB_COUNT_UP; //0递增计数模式
EPwm1Regs.TBCTL.bit.PHSEN=1;//TB_DISABLE;
EPwm1Regs.TBCTL.bit.PRDLD=TB_SHADOW;
EPwm1Regs.TBCTL.bit.SYNCOSEL=1;//TB_SYNC_DISABLE;
EPwm1Regs.TBCTL.bit.HSPCLKDIV=TB_DIV1;
EPwm1Regs.TBCTL.bit.CLKDIV=TB_DIV1;
EPwm1Regs.TBPRD=SP; //设定时间基准周期寄存器,决定PWM频率
EPwm2Regs.TBSTS.all=0; //复位时间基准状态寄存器
EPwm2Regs.TBPHS.half.TBPHS=0; //复位时间基准相位寄存器 sp
EPwm2Regs.TBCTR=0; //复位时间基准计数寄存器
//EPwm1Regs.CMPCTL.all=0x50; // CMPA和CMB的寄存器操作模式选择直接模式,仅有主比较寄存器
EPwm2Regs.CMPCTL.bit.LOADAMODE=CC_CTR_ZERO; // CMPA和CMB的寄存器操作模式选择直接模式,仅有主比较寄存�周期装载
EPwm2Regs.CMPCTL.bit.LOADBMODE=CC_CTR_ZERO;
EPwm2Regs.CMPCTL.bit.SHDWAMODE=CC_SHADOW;
EPwm2Regs.CMPCTL.bit.SHDWBMODE=CC_SHADOW;
EPwm2Regs.CMPA.half.CMPA =SP/2; //初始化计数比较A寄存器
EPwm2Regs.CMPB=SP/2; //初始化计数比较B寄存器
EPwm2Regs.AQCTLA.bit.ZRO=AQ_SET; // 当CTR=CMPA 且递增计数时,EPWMxA =0;当CTR=CMPB 且递减计数时,EPWMxB =0
EPwm2Regs.AQCTLA.bit.CAU=AQ_CLEAR;
EPwm2Regs.AQCTLB.bit.ZRO=AQ_CLEAR;
EPwm2Regs.AQCTLB.bit.CBU=AQ_SET;
EPwm2Regs.AQSFRC.all=0;
EPwm2Regs.AQCSFRC.all=0;
EPwm2Regs.DBCTL.all=0x0B;//0xb; // EPWMxB可以反相,死区对于ePWM1A输出的上升沿延迟和ePWM输出的下降沿延迟完全使能
EPwm2Regs.DBRED=300;//SP/5;
EPwm2Regs.DBFED=300;//SP/5;
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.ETSEL.bit.INTSEL=2; //启动周期中断
//EPwm2Regs.ETSEL.bit.INTEN=1;
//EPwm2Regs.ETPS.bit.INTPRD=1;
//EPwm2Regs.ETCLR.bit.INT=1; //清楚中断标志
EPwm2Regs.ETFLG.all=0; //事件触发标志位,只读
//EPwm1Regs.ETCLR.all=0; //清楚事件触发标志位,写0无效,写1清除
EPwm2Regs.ETFRC.all=0; //此寄存器糜诓馐钥苫管
EPwm2Regs.PCCTL.all=0; //禁用PWM斩波功能
EPwm2Regs.TBCTL.bit.CTRMODE=TB_COUNT_UP;
EPwm2Regs.TBCTL.bit.PHSEN=1;//TB_DISABLE;
EPwm2Regs.TBCTL.bit.PRDLD=TB_SHADOW;
EPwm2Regs.TBCTL.bit.SYNCOSEL=1;//TB_SYNC_DISABLE;
EPwm2Regs.TBCTL.bit.HSPCLKDIV=TB_DIV1;
EPwm2Regs.TBCTL.bit.CLKDIV=TB_DIV1;
EPwm2Regs.TBPRD=SP; //设定时间基准周期寄存器,决定PWM频率
}
//启动ePWM
void StartePWM(void)
{
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1; //ePWM模块的基准时钟启动
EDIS;
}
//ePWM中断,执行if()就无法返回
interrupt void epwm1_isr(void)
{
// pwm_d=PID_caculate();
EPwm1TimerIntCount++;
if(0==(EPwm1TimerIntCount%500))
{
GpioDataRegs.GPATOGGLE.bit.GPIO27 = 1;
}
if(0==(EPwm1TimerIntCount%1000))
{
GpioDataRegs.GPBTOGGLE.bit.GPIO32 = 1;
EPwm1TimerIntCount=0;
Sent_floatNum(SCIB,V_fb);
}
EPwm2Regs.TBPHS.half.TBPHS = pwm_d;
EPwm1Regs.ETCLR.bit.INT=1;
PieCtrlRegs.PIEACK.all |= PIEACK_GROUP3;
}
//主函数
void main(void)
{
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2833x_SysCtrl.c file.
InitSysCtrl(); //初始化系统控制
asm(" RPT #8 || NOP");
DINT;
InitPieCtrl(); //初始化PIE控制器
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable(); //初始化PIE中断向量表
asm(" RPT #8 || NOP");
for(i=0 ; i < AD_num_VC ; i++)
{
AD_collect_VC[i]=0;
AD_collect_IC[i]=0;
}
InitGpio();
InitEPwm();
InitCpuTimers(); // For this example, only initialize the Cpu Timer0
ConfigCpuTimer(&CpuTimer0, 150, 100000);
sci_init(SCIB);
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.TINT0 = &cpu_timer0_isr;
PieVectTable.EPWM1_INT = &epwm1_isr;
PieVectTable.XINT1 = &ADC7606_ISR; //进行数据采集
PieVectTable.SCIRXINTB = &SCIBRX_ISR;
PieVectTable.SCITXINTB = &SCIBTX_ISR;
EDIS;
PieCtrlRegs.PIECTRL.bit.ENPIE = 1; // Enable the PIE block
PieCtrlRegs.PIEIER1.bit.INTx4 = 1; //AD7606中断
PieCtrlRegs.PIEIER1.bit.INTx7 = 1; //timer0
PieCtrlRegs.PIEIER3.bit.INTx1 = 1; //ePWM1
PieCtrlRegs.PIEIER9.bit.INTx3=1; // PIE Group 9, SCIBRX
PieCtrlRegs.PIEIER9.bit.INTx4=1; // PIE Group 9, SCIBTX
IER |= M_INT1;
IER |= M_INT3;
IER |= M_INT9;
// EALLOW;
// EPwm1Regs.AQSFRC.all=0xc0;
// EPwm1Regs.AQCSFRC.all=0x05;
// EPwm2Regs.AQSFRC.all=0xc0;
// EPwm2Regs.AQCSFRC.all=0x05;
// EDIS;
//StartEPwm(); //启动ePWM
ptrchanel=(Uint32*)chanel; //通道指针
ResetADS7606();
asm (" RPT #10 || NOP");
StartCONVSTAB();
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
StartCpuTimer0();
StartePWM(); //启动ePWM
for(;;)
{
/*
GpioDataRegs.GPATOGGLE.bit.GPIO27 = 1;
GpioDataRegs.GPBTOGGLE.bit.GPIO32 = 1;
DELAY_US(100);
*/
}
}
mangui zhang:
中断中的部分代码 你一部分一部分注释吧
看看那些能够导致进入死循环出不来
//ePWM初始化
void InitEPwm(void)//3750
{ EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0; //ePWM模块的基准时钟停止
EDIS;
EPwm1Regs.TBSTS.all=0; //复位时间基准状态寄存器
EPwm1Regs.TBPHS.half.TBPHS=0; //复位时间基准相位寄存器
EPwm1Regs.TBCTR=0; //复位时间基准计数寄存器
//EPwm1Regs.CMPCTL.all=0x50; // CMPA和CMB的寄存器操作模式选择直接模式,仅有主比较寄存器
EPwm1Regs.CMPCTL.bit.LOADAMODE=CC_CTR_ZERO; // CMPA和CMB的寄存器操作模式选择直接模式,仅有主比较寄存器,周期装载
EPwm1Regs.CMPCTL.bit.LOADBMODE=CC_CTR_ZERO;
EPwm1Regs.CMPCTL.bit.SHDWAMODE=CC_SHADOW; //使能映射
EPwm1Regs.CMPCTL.bit.SHDWBMODE=CC_SHADOW;
EPwm1Regs.CMPA.half.CMPA=SP/2; //初始化计数比较A寄存器EPwm1Regs.CMPA.half.CMPA =0;
EPwm1Regs.CMPB=SP/2; //初始化计数比较B寄存器EPwm1Regs.CMPB=SP/2-300;//SP/2=3750
EPwm1Regs.AQCTLA.bit.ZRO=AQ_SET; // 当CTR=CMPA 且,EPWMxA =0;当CTR=CMPB 且递减计数时,EPWMxB =0
EPwm1Regs.AQCTLA.bit.CAU=AQ_CLEAR;
EPwm1Regs.AQCTLB.bit.ZRO=AQ_CLEAR;
EPwm1Regs.AQCTLB.bit.CBU=AQ_SET;
EPwm1Regs.AQSFRC.all=0;
EPwm1Regs.AQCSFRC.all=0;
EPwm1Regs.DBCTL.all=0x0B;//0xb; // EPWMxB可以反相,死区对于ePWM1A输出的上升沿延迟和ePWM输出的下降沿延迟完全使能
EPwm1Regs.DBRED=300;//SP/5; //设置上升沿延时
EPwm1Regs.DBFED=300;//SP/5; //设置下降沿延时
EPwm1Regs.TZSEL.all=0; //错误控制和状态相关的寄存器
EPwm1Regs.TZCTL.all=0;
EPwm1Regs.TZEINT.all=0;
EPwm1Regs.TZFLG.all=0;
EPwm1Regs.TZCLR.all=0;
EPwm1Regs.TZFRC.all=0;
EPwm1Regs.ETSEL.bit.INTEN=1; //启动周期中断
EPwm1Regs.ETSEL.bit.INTSEL=2; EPwm1Regs.ETPS.bit.INTPRD=1;
EPwm1Regs.ETCLR.bit.INT=1; //清楚中断标志
EPwm1Regs.ETFLG.all=0; //事件触发标志位,只读
//EPwm1Regs.ETCLR.all=0; //清楚事件触发标志位,写0无效,写1清除
EPwm1Regs.ETFRC.all=0; //此寄存器用于测试可不管
EPwm1Regs.PCCTL.all=0; //禁用PWM斩波功能
EPwm1Regs.TBCTL.bit.CTRMODE=TB_COUNT_UP; //0递增计数模式
EPwm1Regs.TBCTL.bit.PHSEN=1;//TB_DISABLE;
EPwm1Regs.TBCTL.bit.PRDLD=TB_SHADOW;
EPwm1Regs.TBCTL.bit.SYNCOSEL=1;//TB_SYNC_DISABLE;
EPwm1Regs.TBCTL.bit.HSPCLKDIV=TB_DIV1;
EPwm1Regs.TBCTL.bit.CLKDIV=TB_DIV1;
EPwm1Regs.TBPRD=SP; //设定时间基准周期寄存器,决定PWM频率
EPwm2Regs.TBSTS.all=0; //复位时间基准状态寄存器
EPwm2Regs.TBPHS.half.TBPHS=0; //复位时间基准相位寄存器 sp
EPwm2Regs.TBCTR=0; //复位时间基准计数寄存器
//EPwm1Regs.CMPCTL.all=0x50; // CMPA和CMB的寄存器操作模式选择直接模式,仅有主比较寄存器
EPwm2Regs.CMPCTL.bit.LOADAMODE=CC_CTR_ZERO; // CMPA和CMB的寄存器操作模式选择直接模式,仅有主比较寄存�周期装载
EPwm2Regs.CMPCTL.bit.LOADBMODE=CC_CTR_ZERO;
EPwm2Regs.CMPCTL.bit.SHDWAMODE=CC_SHADOW;
EPwm2Regs.CMPCTL.bit.SHDWBMODE=CC_SHADOW;
EPwm2Regs.CMPA.half.CMPA =SP/2; //初始化计数比较A寄存器
EPwm2Regs.CMPB=SP/2; //初始化计数比较B寄存器
EPwm2Regs.AQCTLA.bit.ZRO=AQ_SET; // 当CTR=CMPA 且递增计数时,EPWMxA =0;当CTR=CMPB 且递减计数时,EPWMxB =0
EPwm2Regs.AQCTLA.bit.CAU=AQ_CLEAR;
EPwm2Regs.AQCTLB.bit.ZRO=AQ_CLEAR;
EPwm2Regs.AQCTLB.bit.CBU=AQ_SET;
EPwm2Regs.AQSFRC.all=0;
EPwm2Regs.AQCSFRC.all=0;
EPwm2Regs.DBCTL.all=0x0B;//0xb; // EPWMxB可以反相,死区对于ePWM1A输出的上升沿延迟和ePWM输出的下降沿延迟完全使能
EPwm2Regs.DBRED=300;//SP/5;
EPwm2Regs.DBFED=300;//SP/5;
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.ETSEL.bit.INTSEL=2; //启动周期中断
//EPwm2Regs.ETSEL.bit.INTEN=1;
//EPwm2Regs.ETPS.bit.INTPRD=1;
//EPwm2Regs.ETCLR.bit.INT=1; //清楚中断标志
EPwm2Regs.ETFLG.all=0; //事件触发标志位,只读
//EPwm1Regs.ETCLR.all=0; //清楚事件触发标志位,写0无效,写1清除
EPwm2Regs.ETFRC.all=0; //此寄存器糜诓馐钥苫管
EPwm2Regs.PCCTL.all=0; //禁用PWM斩波功能
EPwm2Regs.TBCTL.bit.CTRMODE=TB_COUNT_UP;
EPwm2Regs.TBCTL.bit.PHSEN=1;//TB_DISABLE;
EPwm2Regs.TBCTL.bit.PRDLD=TB_SHADOW;
EPwm2Regs.TBCTL.bit.SYNCOSEL=1;//TB_SYNC_DISABLE;
EPwm2Regs.TBCTL.bit.HSPCLKDIV=TB_DIV1;
EPwm2Regs.TBCTL.bit.CLKDIV=TB_DIV1;
EPwm2Regs.TBPRD=SP; //设定时间基准周期寄存器,决定PWM频率
}
//启动ePWM
void StartePWM(void)
{
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1; //ePWM模块的基准时钟启动
EDIS;
}
//ePWM中断,执行if()就无法返回
interrupt void epwm1_isr(void)
{
// pwm_d=PID_caculate();
EPwm1TimerIntCount++;
if(0==(EPwm1TimerIntCount%500))
{
GpioDataRegs.GPATOGGLE.bit.GPIO27 = 1;
}
if(0==(EPwm1TimerIntCount%1000))
{
GpioDataRegs.GPBTOGGLE.bit.GPIO32 = 1;
EPwm1TimerIntCount=0;
Sent_floatNum(SCIB,V_fb);
}
EPwm2Regs.TBPHS.half.TBPHS = pwm_d;
EPwm1Regs.ETCLR.bit.INT=1;
PieCtrlRegs.PIEACK.all |= PIEACK_GROUP3;
}
//主函数
void main(void)
{
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2833x_SysCtrl.c file.
InitSysCtrl(); //初始化系统控制
asm(" RPT #8 || NOP");
DINT;
InitPieCtrl(); //初始化PIE控制器
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable(); //初始化PIE中断向量表
asm(" RPT #8 || NOP");
for(i=0 ; i < AD_num_VC ; i++)
{
AD_collect_VC[i]=0;
AD_collect_IC[i]=0;
}
InitGpio();
InitEPwm();
InitCpuTimers(); // For this example, only initialize the Cpu Timer0
ConfigCpuTimer(&CpuTimer0, 150, 100000);
sci_init(SCIB);
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.TINT0 = &cpu_timer0_isr;
PieVectTable.EPWM1_INT = &epwm1_isr;
PieVectTable.XINT1 = &ADC7606_ISR; //进行数据采集
PieVectTable.SCIRXINTB = &SCIBRX_ISR;
PieVectTable.SCITXINTB = &SCIBTX_ISR;
EDIS;
PieCtrlRegs.PIECTRL.bit.ENPIE = 1; // Enable the PIE block
PieCtrlRegs.PIEIER1.bit.INTx4 = 1; //AD7606中断
PieCtrlRegs.PIEIER1.bit.INTx7 = 1; //timer0
PieCtrlRegs.PIEIER3.bit.INTx1 = 1; //ePWM1
PieCtrlRegs.PIEIER9.bit.INTx3=1; // PIE Group 9, SCIBRX
PieCtrlRegs.PIEIER9.bit.INTx4=1; // PIE Group 9, SCIBTX
IER |= M_INT1;
IER |= M_INT3;
IER |= M_INT9;
// EALLOW;
// EPwm1Regs.AQSFRC.all=0xc0;
// EPwm1Regs.AQCSFRC.all=0x05;
// EPwm2Regs.AQSFRC.all=0xc0;
// EPwm2Regs.AQCSFRC.all=0x05;
// EDIS;
//StartEPwm(); //启动ePWM
ptrchanel=(Uint32*)chanel; //通道指针
ResetADS7606();
asm (" RPT #10 || NOP");
StartCONVSTAB();
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
StartCpuTimer0();
StartePWM(); //启动ePWM
for(;;)
{
/*
GpioDataRegs.GPATOGGLE.bit.GPIO27 = 1;
GpioDataRegs.GPBTOGGLE.bit.GPIO32 = 1;
DELAY_US(100);
*/
}
}
mao peng:
回复 mangui zhang:
乘除以及循环会占用大量的中断时间,所以main函数基本没有怎么执行