ePWM Modules (2)
Use of time base module
TBPRD: period register (the set clock period is stored here, it can be written after buffering through the shadow register, or it can be written immediately through the active register)
TBCTR: time base count variable value register (the value currently counted by the time base is stored for comparison with the set period value)
TBPHS: Time Base Phase Register
TBSTS: Time Base Status Register
TBCTL: control register (important)
CTRMOD: counting mode
00: add
01: subtract
10: add and subtract
11: stop counting
PHSEN: enable phase
0: disable loading
1: load phase register value
PRDLD: Period register shadow loading mode
0: Load shadow register value
1: Do not load shadow
SYNCOSEL: Select ePWMxSYNCO signal output source
00: ePWMxSYNCI
01: CTR=ZER0: Time base counter is 0
10: CTR=CMPA: Time base counter is equal to compare register A
11: Disable output
SWFSYNC: software forced synchronization pulse
0: no effect
1: forced once
HSPCLKDIV: frequency division, same as CLKDIV. TBCLK=SYSCLKOUT/(HSPCLKDIV×CLKDIV)
CLKDIV: frequency division, same as HSPCLKDIV with
000: 1 frequency division
001: 2 frequency division
010: 4 frequency division
...
111: 128 frequency division
PHSDIR: In the increase-decrease mode, when the synchronization signal arrives, the counting direction of the increase-decrease state after loading the phase value
0: count down
1: count up
#include "EPWM.h"
#include "leds.h"
//时基模块 TB 单独的应用,使用中断实现呼吸灯
void EPWM1_Init(Uint16 tbprd) //输入参数为PWM的周期
{
//使能 ePWM 外设时钟及失能时基模块时钟
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;//失能TBCLKSYNC
SysCtrlRegs.PCLKCR1.bit.EPWM1ENCLK = 1;//使能ePWM1外设时钟
EDIS;
//中断向量表
EALLOW;
PieVectTable.EPWM1_INT = &epwm1_timer_isr;
EDIS;
//使能时基时钟
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;//使能TBCLKSYNC
EDIS;
//设置同步
EPwm1Regs.TBCTL.bit.SYNCOSEL= 0x0;//TB_SYNC_IN
//允许每个使用被同步
EPwm1Regs.TBCTL.bit.PHSEN = 0x1;//TB_ENABLE
时基相位寄存器赋值0
EPwm1Regs.TBPHS.half.TBPHS = 0;
//设置周期数值
EPwm1Regs.TBPRD = tbprd;
// 递增计数模式
EPwm1Regs.TBCTL.bit.CTRMODE = 0x0;//TB_COUNT_UP
//TBCLK=SYSCLKOUT/(HSPCLKDIV×CLKDIV)
EPwm1Regs.TBCTL.bit.HSPCLKDIV=0x0;//不分频TB_DIV1
EPwm1Regs.TBCTL.bit.CLKDIV=0x0;//不分频TB_DIV1
// 在零事件上选择INT
EPwm1Regs.ETSEL.bit.INTSEL = 0x1;//ET_CTR_ZERO
// 使能 INT
EPwm1Regs.ETSEL.bit.INTEN = 1; //ET_1ST
// 在第一个事件上生成INT
EPwm1Regs.ETPS.bit.INTPRD = 0x1;
// 同步启动所有计时器
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
EDIS;
// 使能连接EPWM1-6 INT的CPU INT3:
IER |= M_INT3;
// 使能PIE中的EPWM INTn:组3中断1-6
PieCtrlRegs.PIEIER3.bit.INTx1 = 1;
// 启用全局中断和更高优先级的实时调试事件:
EINT; // 启用全局中断INTM
ERTM; // 启用全局实时中断DBGM
}
interrupt void epwm1_timer_isr(void)
{
static Uint16 cnt = 0;
cnt++;
if(cnt==5000)
{
cnt=0;
LED3_TOGGLE;
}
// 清除此计时器的INT标志
EPwm1Regs.ETCLR.bit.INT = 1;
// 确认此中断以接收来自组3的更多中断
PieCtrlRegs.PIEACK.bit.ACK3 = 1;
}
#ifndef APP_EPWM_EPWM_H_
#define APP_EPWM_EPWM_H_
#include "DSP2833x_Device.h" // DSP2833x 头文件
#include "DSP2833x_Examples.h" // DSP2833x 例子相关头文件
void EPWM1_Init(Uint16 tbprd);
interrupt void epwm1_timer_isr(void);
#endif /* APP_EPWM_EPWM_H_ */