DSP

TMS2833X之ePWM模块_2

2019-07-13 16:44发布

上一篇讲述到F2833x系列ePWM模块结构,以及介绍了ePWM模块中的子模块时间基准模块(DB),其主要作用是设置PWM波形的周期,以及输出一系列事件(如CTR=ZERO)等供其他模块使用,下面接着介绍ePWM中的其它子模块。
在这里插入图片描述

1 ePWM 计数比较模块CC

计数器比较模块是以时基计数器的计数值作为输入,与比较寄存器CMPA和CMPB不断进行比较,当时基计数器的值等于CMPA时,就产生比较事件CTR = CMPA;当时基计数器的值等于CMPB时,就产生比较事件CTR = CMPB。计数寄存器模块有以下要点进行说明:
  • 计数比较模块进行恰当配置后,可以控制PWM波形的占空比。
  • 采用影子寄存器来更新比较寄存器可有效防止在PWM周期内出现故障以及毛刺,其具体应用可参考上一篇TB子模块中影子寄存器的介绍。
    计数器比较模块的子结构图如下,逻辑比较简单,不再详细介绍。
    在这里插入图片描述

2 ePWM 动作模块AQ

动作模块在PWM波形形成过程中起到关键作用,它决定了相应事件发生时应该输出什么样的电平,模块执行框图如下图所示。在这里插入图片描述
动作模块操作这里通过配置寄存器代码与波形图结合来说明。
  • 示例1
// Initialization Time // = = = = = = = = = = = = = = = = = = = = = = = = EPwm1Regs.TBPRD = 600; // 计数周期 = 601 TBCLK counts EPwm1Regs.CMPA.half.CMPA = 350; // 比较寄存器A = 350 TBCLK counts EPwm1Regs.CMPB = 200; // 比较寄存器B = 200 TBCLK counts EPwm1Regs.TBPHS = 0; // 相位寄存器置0 EPwm1Regs.TBCTR = 0; // 计数值清0 EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP; // 计数方式采用递增计数 EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE; //禁止相位加载 EPwm1Regs.TBCTL.bit.PRDLD = TB_SHADOW; // 采用影子寄存器 EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_DISABLE; //禁止同步输出 EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1; // 时基寄存器预分频 EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV1; EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW; //CMPA采用影子寄存器加载 EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW; //CMPB采用影子寄存器加载 EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; // CTR = Zero时CMPA从影子寄存器加载 EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO; // CTR = Zero时CMPB从影子寄存器加载 EPwm1Regs.AQCTLA.bit.ZRO = AQ_SET; // CTR = ZERO事件发生时ePWMA输出高电平 EPwm1Regs.AQCTLA.bit.CAU = AQ_CLEAR; // CMPA = 计数值(递增中)事件发生时ePWMA输出低电平 EPwm1Regs.AQCTLB.bit.ZRO = AQ_SET; // CTR = ZERO事件发生时ePWMB输出高电平 EPwm1Regs.AQCTLB.bit.CBU = AQ_CLEAR; // CMPA = 计数值(递增中)事件发生时ePWMB输出低电平 // // Run Time // = = = = = = = = = = = = = = = = = = = = = = = = EPwm1Regs.CMPA.half.CMPA = Duty1A; // 调整ePWM1A的占空比 EPwm1Regs.CMPB = Duty1B; // 调整ePWM1B的占空比 上述配置代码输出波形如下图所示:
在这里插入图片描述
  • 示例2
    下面代码只表示一些核心配置,其他部分省略
EPwm1Regs.TBPRD = 600; // 计数周期 = 601 TBCLK counts EPwm1Regs.CMPA.half.CMPA = 350; // 比较寄存器A = 350 TBCLK counts EPwm1Regs.CMPB = 200; // 比较寄存器B = 200 TBCLK counts EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP; // 计数方式采用递增计数 EPwm1Regs.AQCTLA.bit.PRD = AQ_CLEAR; // CTR = PRD事件发生时ePWMA输出低电平 EPwm1Regs.AQCTLA.bit.CAU = AQ_SET; // CMPA = 计数值(递增中)事件发生时ePWMA输出高电平 EPwm1Regs.AQCTLB.bit.PRD = AQ_CLEAR; // CTR = PRD事件发生时ePWMB输出低电平 EPwm1Regs.AQCTLB.bit.CBU = AQ_SET; // CMPB = 计数值(递增中)事件发生时ePWMB输出高电平 上述配置代码输出波形如下图所示:
在这里插入图片描述
  • 示例3
    下面代码只表示一些核心配置,其他部分省略
EPwm1Regs.TBPRD = 600; // 计数周期 = 601 TBCLK counts EPwm1Regs.CMPA.half.CMPA = 200; // 比较寄存器A = 200 TBCLK counts EPwm1Regs.CMPB = 400; // 比较寄存器B = 400 TBCLK counts EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP; // 计数方式采用递增计数 EPwm1Regs.AQCTLA.bit.CAU = AQ_SET; // CMPA = 计数值(递增中)事件发生时ePWMA输出高电平 EPwm1Regs.AQCTLA.bit.CBU = AQ_CLEAR; // CMPB = 计数值(递增中)事件发生时ePWMA输出低电平 EPwm1Regs.AQCTLB.bit.ZRO= AQ_TOGGLE; // CTR = ZERO事件发生时ePWMB电平反转 上述配置代码输出波形如下图所示:
在这里插入图片描述
  • 示例4
    下面代码只表示一些核心配置,其他部分省略
EPwm1Regs.TBPRD = 600; // 计数周期 = 601 TBCLK counts EPwm1Regs.CMPA.half.CMPA = 400; // 比较寄存器A = 400 TBCLK counts EPwm1Regs.CMPB = 500; // 比较寄存器B = 500 TBCLK counts EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // 计数方式采用递增、减计数 EPwm1Regs.AQCTLA.bit.CAU = AQ_SET; // CMPA = 计数值(递增中)事件发生时ePWMA输出高电平 EPwm1Regs.AQCTLA.bit.CAD = AQ_CLEAR; // CMPA = 计数值(递减中)事件发生时ePWMA输出低电平 EPwm1Regs.AQCTLB.bit.CBU= AQ_SET; // CMPB = 计数值(递增中)事件发生时ePWMA输出高电平 EPwm1Regs.AQCTLB.bit.CBD= AQ_CLEAR; // CMPB = 计数值(递减中)事件发生时ePWMA输出低电平 上述配置代码输出波形如下图所示:
在这里插入图片描述
  • 示例5
    下面代码只表示一些核心配置,其他部分省略
EPwm1Regs.TBPRD = 600; // 计数周期 = 601 TBCLK counts EPwm1Regs.CMPA.half.CMPA = 350; // 比较寄存器A = 350 TBCLK counts EPwm1Regs.CMPB = 400; // 比较寄存器B = 400 TBCLK counts EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // 计数方式采用递增、减计数 EPwm1Regs.AQCTLA.bit.CAU = AQ_SET; // CMPA = 计数值(递增中)事件发生时ePWMA输出高电平 EPwm1Regs.AQCTLA.bit.CAD = AQ_CLEAR; // CMPA = 计数值(递减中)事件发生时ePWMA输出低电平 EPwm1Regs.AQCTLB.bit.CBU= AQ_CLEAR; // CMPB = 计数值(递增中)事件发生时ePWMA输出低电平 EPwm1Regs.AQCTLB.bit.CBD= AQ_SET; // CMPB = 计数值(递减中)事件发生时ePWMA输出高电平 上述配置代码输出波形如下图所示:
在这里插入图片描述
  • 示例6
    下面代码只表示一些核心配置,其他部分省略
EPwm1Regs.TBPRD = 600; // 计数周期 = 601 TBCLK counts EPwm1Regs.CMPA.half.CMPA = 250; // 比较寄存器A = 250 TBCLK counts EPwm1Regs.CMPB = 450; // 比较寄存器B = 450 TBCLK counts EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // 计数方式采用递增、减计数 EPwm1Regs.AQCTLA.bit.CAU = AQ_SET; // CMPA = 计数值(递增中)事件发生时ePWMA输出高电平 EPwm1Regs.AQCTLA.bit.CBD = AQ_CLEAR; // CMPB = 计数值(递减中)事件发生时ePWMA输出低电平 EPwm1Regs.AQCTLB.bit.ZRO= AQ_CLEAR; // CMPB = ZERO事件发生时ePWMB输出低电平 EPwm1Regs.AQCTLB.bit.PRD= AQ_SET; // CMPB = RPD事件发生时ePWMB输出高电平 上述配置代码输出波形如下图所示:
在这里插入图片描述

3.代码实例

下面再通过一个实际代码实例来说明ePWM模块中寄存器的相关设置,改代码实例涉及到TB、CC、AQ、ET等子模块,通过配置寄存器参数输出PWM波形,并触发INT中断,且程序中通过中断触发计数次数(10次)动态调整CMPA、CMPB的值。 //定义ePWM信息记忆结构体 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; // 定义ePWM1~3初始化函数及中断执行函数、用户定制更新CMPA/B函数 void InitEPwm1Example(void); void InitEPwm2Example(void); void InitEPwm3Example(void); interrupt void epwm1_isr(void); interrupt void epwm2_isr(void); interrupt void epwm3_isr(void); void update_compare(EPWM_INFO*); // 示例中用到的全局变量 EPWM_INFO epwm1_info; EPWM_INFO epwm2_info; EPWM_INFO epwm3_info; // 为ePWM1~3定义周期寄存器、比较寄存器最大、最小值 #define EPWM1_TIMER_TBPRD 2000 // Period register #define EPWM1_MAX_CMPA 1950 #define EPWM1_MIN_CMPA 50 #define EPWM1_MAX_CMPB 1950 #define EPWM1_MIN_CMPB 50 #define EPWM2_TIMER_TBPRD 2000 // Period register #define EPWM2_MAX_CMPA 1950 #define EPWM2_MIN_CMPA 50 #define EPWM2_MAX_CMPB 1950 #define EPWM2_MIN_CMPB 50 #define EPWM3_TIMER_TBPRD 2000 // Period register #define EPWM3_MAX_CMPA 950 #define EPWM3_MIN_CMPA 50 #define EPWM3_MAX_CMPB 1950 #define EPWM3_MIN_CMPB 1050 #define EPWM_CMP_UP 1 #define EPWM_CMP_DOWN 0 //主调用函数 void main(void) { //以下为一些初始执行函数,不仔细说明 InitSysCtrl(); InitEPwm1Gpio(); InitEPwm2Gpio(); InitEPwm3Gpio(); DINT; InitPieCtrl(); IER = 0x0000; IFR = 0x0000; InitPieVectTable(); EALLOW; / PieVectTable.EPWM1_INT = &epwm1_isr; PieVectTable.EPWM2_INT = &epwm2_isr; PieVectTable.EPWM3_INT = &epwm3_isr; EDIS; EALLOW; SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0; // 时基计数器停止计数 EDIS; //配置ePWM1~3 InitEPwm1Example(); InitEPwm2Example(); InitEPwm3Example(); EALLOW; SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1; // 时基计数器开始计数 EDIS; // 使能CPU中断使能标志位 IER |= M_INT3; // 使能PIE中断使能标志位 PieCtrlRegs.PIEIER3.bit.INTx1 = 1; PieCtrlRegs.PIEIER3.bit.INTx2 = 1; PieCtrlRegs.PIEIER3.bit.INTx3 = 1; // 使能全局中断标志INTM EINT; ERTM; // 循环体 for(;;) { asm(" NOP"); } } // ePWM1中断执行函数体 interrupt void epwm1_isr(void) { // 更新CMPA和CMPB的值 update_compare(&epwm1_info); EPwm1Regs.ETCLR.bit.INT = 1; PieCtrlRegs.PIEACK.all = PIEACK_GROUP3; } // 初始化ePWM1 void InitEPwm1Example() { EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP; // 计数模式设置为递增计数 EPwm1Regs.TBPRD = EPWM1_TIMER_TBPRD; // 时基计数值为2000 EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE; // 禁止计数计时器装载相位寄存器 EPwm1Regs.TBPHS.half.TBPHS = 0x0000; // 相位设置为0 EPwm1Regs.TBCTR = 0x0000; // 计数器清零 EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV2; // TBCLK = SYSCLKOUT / (HSPCLKDIV * CLKDIV); EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV2; EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW; //CMPA使用影子寄存器 EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW; //CMPB使用影子寄存器 EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; //当产生CTR = 0时间时从影子寄存器加载CMPA的值到活动寄存器 EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO; //当产生CTR = 0时间时从影子寄存器加载CMPB的值到活动寄存器 EPwm1Regs.CMPA.half.CMPA = EPWM1_MIN_CMPA; // 设置CMPA的值 EPwm1Regs.CMPB = EPWM1_MIN_CMPB; // 设置CMPB的值 EPwm1Regs.AQCTLA.bit.ZRO = AQ_SET; // 计数器等于0时ePWMA输出高电平 EPwm1Regs.AQCTLA.bit.CAU = AQ_CLEAR; // 递增计数等于CMPA时,ePWMA输出低电平 EPwm1Regs.AQCTLB.bit.ZRO = AQ_SET; // 计数器等于0时ePWMB输出高电平 EPwm1Regs.AQCTLB.bit.CBU = AQ_CLEAR; // 递增计数等于CMPB时,ePWMB输出低电平0 EPwm1Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO; // CTR = 0事件发生时产生INT中断 EPwm1Regs.ETSEL.bit.INTEN = 1; // INT中断使能 EPwm1Regs.ETPS.bit.INTPRD = ET_3RD; // 每产生3次CTR = 0事件触发一次中断 //记录相关ePWM1的配置 epwm1_info.EPwm_CMPA_Direction = EPWM_CMP_UP; epwm1_info.EPwm_CMPB_Direction = EPWM_CMP_UP; epwm1_info.EPwmTimerIntCount = 0; epwm1_info.EPwmRegHandle = &EPwm1Regs; epwm1_info.EPwmMaxCMPA = EPWM1_MAX_CMPA; epwm1_info.EPwmMinCMPA = EPWM1_MIN_CMPA; epwm1_info.EPwmMaxCMPB = EPWM1_MAX_CMPB; epwm1_info.EPwmMinCMPB = EPWM1_MIN_CMPB; } void update_compare(EPWM_INFO *epwm_info) { // 没发生10次中断,改变CMPA和CMPB的值 if(epwm_info->EPwmTimerIntCount == 10) { epwm_info->EPwmTimerIntCount = 0; if(epwm_info->EPwm_CMPA_Direction == EPWM_CMP_UP) { //ePWMA的值当前处于递增中 if(epwm_info->EPwmRegHandle->CMPA.half.CMPA < epwm_info->EPwmMaxCMPA) { // CMPA的值 小于 设置的CMPA最大值,执行递增操作,否则,将ePWM设置为递减中,并对CMPA的值执行递增操作 epwm_info->EPwmRegHandle->CMPA.half.CMPA++; } else { epwm_info->EPwm_CMPA_Direction = EPWM_CMP_DOWN; epwm_info->EPwmRegHandle->CMPA.half.CMPA--; } } else { //与上面的操作相反 if(epwm_info->EPwmRegHandle->CMPA.half.CMPA == epwm_info->EPwmMinCMPA) { epwm_info->EPwm_CMPA_Direction = EPWM_CMP_UP; epwm_info->EPwmRegHandle->CMPA.half.CMPA++; } else { epwm_info->EPwmRegHandle->CMPA.half.CMPA--; } } //ePWMB的值的处理与ePWMA相等同 if(epwm_info->EPwm_CMPB_Direction == EPWM_CMP_UP) { if(epwm_info->EPwmRegHandle->CMPB < epwm_info->EPwmMaxCMPB) { epwm_info->EPwmRegHandle->CMPB++; } else { epwm_info->EPwm_CMPB_Direction = EPWM_CMP_DOWN; epwm_info->EPwmRegHandle->CMPB--; } } else { if(epwm_info->EPwmRegHandle->CMPB == epwm_info->EPwmMinCMPB) { epwm_info->EPwm_CMPB_Direction = EPWM_CMP_UP; epwm_info->EPwmRegHandle->CMPB++; } else { epwm_info->EPwmRegHandle->CMPB--; } } } else { epwm_info->EPwmTimerIntCount++; } return; }