#include "DSP28x_Project.h" // Device Headerfile and Examples Include File
#include "DSP2833x_ECap_defines.h"
#define EPWM1_TIMER_TBPRD 3750 // Period register 系统始终为150MHZ,下面的程序进行了4分频,即为37.5MHZ,这样得到的是10KHZ
Uint32 TS1 = 0
Uint32 TS2 = 0
Uint32 TS3 = 0
Uint32 TS4 = 0
Uint32 PEROID = 0
Uint32 DUTY1 = 0
Uint32 DUTY2 = 0
Uint32 initcount = 0
Uint32 duty = 3750 / 2
__interrupt void ecap1_isr(void)
void PWM1_Init()//初始化PWM
{
EALLOW
SysCtrlRegs.PCLKCR1.bit.ECAP1ENCLK = 1
EDIS
// Setup TBCLK
EPwm1Regs.TBPRD = EPWM1_TIMER_TBPRD
EPwm1Regs.TBPHS.half.TBPHS = 0x0000
EPwm1Regs.TBCTR = 0x0000
// Setup counter mode
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE
EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV2
EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV2
EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_CTR_ZERO
// Setup shadowing
EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW
EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW
EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO
EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO
// Set actions
EPwm1Regs.AQCTLA.bit.CAU = AQ_CLEAR
EPwm1Regs.AQCTLA.bit.CAD = AQ_SET
EPwm1Regs.DBCTL.bit.OUT_MODE = DB_FULL_ENABLE
EPwm1Regs.DBCTL.bit.POLSEL = DB_ACTV_HIC
EPwm1Regs.DBFED = 50
EPwm1Regs.DBRED = 50
EPwm1Regs.CMPA.half.CMPA = (EPWM1_TIMER_TBPRD >> 1)
}
void Pwm_Init()
{
EALLOW
GpioCtrlRegs.GPAPUD.bit.GPIO0 = 0
GpioCtrlRegs.GPAPUD.bit.GPIO1 = 0
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1
GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1
EDIS
EALLOW
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0
EDIS
PWM1_Init()
EALLOW
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1
EDIS
}
void InitECapture()
{
ECap1Regs.ECEINT.all = 0x0000
ECap1Regs.ECCLR.all = 0xFFFF
ECap1Regs.ECCTL1.bit.CAPLDEN = 0
ECap1Regs.ECCTL2.bit.TSCTRSTOP = EC_STOP
// Configure peripheral registers
ECap1Regs.ECCTL2.bit.CONT_ONESHT = EC_CONTINUOUS
ECap1Regs.ECCTL2.bit.STOP_WRAP = 3
ECap1Regs.ECCTL1.bit.CAP1POL = EC_RISING
ECap1Regs.ECCTL1.bit.CAP2POL = EC_FALLING
ECap1Regs.ECCTL1.bit.CAP3POL = EC_RISING
ECap1Regs.ECCTL1.bit.CAP4POL = EC_FALLING
ECap1Regs.ECCTL1.bit.CTRRST1 = EC_ABS_MODE
ECap1Regs.ECCTL1.bit.CTRRST2 = EC_ABS_MODE
ECap1Regs.ECCTL1.bit.CTRRST3 = EC_ABS_MODE
ECap1Regs.ECCTL1.bit.CTRRST4 = EC_ABS_MODE
ECap1Regs.ECCTL2.bit.SYNCI_EN = EC_DISABLE
ECap1Regs.ECCTL2.bit.SYNCO_SEL = EC_SYNCO_DIS
ECap1Regs.ECCTL1.bit.CAPLDEN = EC_ENABLE
ECap1Regs.ECCTL1.bit.PRESCALE = EC_DIV1
ECap1Regs.ECCTL2.bit.CAP_APWM = EC_CAP_MODE
ECap1Regs.ECCTL2.bit.TSCTRSTOP = EC_RUN
ECap1Regs.ECEINT.bit.CEVT4 = 1
}
void cap_init()
{
EALLOW
GpioCtrlRegs.GPAPUD.bit.GPIO24 = 0
GpioCtrlRegs.GPAMUX2.bit.GPIO24 = 1
// GpioCtrlRegs.GPAPUD.bit.GPIO25 = 0
// GpioCtrlRegs.GPAMUX1.bit.GPIO25 = 1
EDIS
InitECapture()
}
void All_Init()
{
InitSysCtrl()
DINT
InitPieCtrl()
IER = 0x0000
IFR = 0x0000
InitPieVectTable()
EALLOW
PieVectTable.ECAP1_INT = &ecap1_isr
EDIS
Pwm_Init()
cap_init()
IER |= M_INT4
PieCtrlRegs.PIEIER4.bit.INTx1 = 1
EINT
ERTM
}
void main(void)
{
All_Init()
while(1)
{
EPwm1Regs.CMPA.half.CMPA = duty
}
}
__interrupt void ecap1_isr(void)
{
TS1 = ECap1Regs.CAP1
TS2 = ECap1Regs.CAP2
TS3 = ECap1Regs.CAP3
TS4 = ECap1Regs.CAP4
PEROID = TS3 - TS1
DUTY1 = TS2 - TS1
DUTY2 = TS3 - TS2
initcount++
ECap1Regs.ECCLR.bit.CEVT4 = 1
ECap1Regs.ECCLR.bit.INT = 1
ECap1Regs.ECCTL2.bit.REARM = 1
PieCtrlRegs.PIEACK.all = PIEACK_GROUP4
}
CAP有两个功能,一个是输入捕获,另一个是APWM,即输出PWM波。虽然可以输出PWM,但是我觉得好像没啥用。
在DSP中有一个MOD4寄存器,这个寄存器是不断对数据进行余4,即0-1-2-3-0-1-2-3…….不断的进行循环。比如现在MOD4的值为0,然后基准计数器不断地累加,当捕获到上升沿(或者下降沿,看自己的设定)时,将此时基准计数器的值存入CAP1中,然后MOD4加一,然后当捕获到下一个下降沿时,将此时的基准寄存器的值存入CAP2,然后MOD4加一,依次循环。
最后将period = T3 - T1,duty = T2 - T1