DSP

eCAP

2019-07-13 18:20发布

#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;; // Stop all the TB clocks EDIS; // Setup TBCLK EPwm1Regs.TBPRD = EPWM1_TIMER_TBPRD; // 设置周期 EPwm1Regs.TBPHS.half.TBPHS = 0x0000; // 如果使能的话,当有同步信号时,会将这个值赋值给CTR EPwm1Regs.TBCTR = 0x0000; // 计数器初始值赋值为0 // Setup counter mode EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; //计数模式 EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE; // Disable phase loading EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV2; // 这里是进行2分频 EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV2; //这里也是2分频 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; // Load on Zero EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO; // Set actions EPwm1Regs.AQCTLA.bit.CAU = AQ_CLEAR; // Set PWM1A on event A, up count EPwm1Regs.AQCTLA.bit.CAD = AQ_SET; // Clear PWM1A on event A, down count 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() { /*首先进行的是PWM1引脚的初始化,因为PWM1的引脚为GPIO0和GPIO1,这两个引脚可以是普通的IO口,也可以复用输出PWM,这里选用复用功能*/ EALLOW; GpioCtrlRegs.GPAPUD.bit.GPIO0 = 0; // Enable pull-up on GPIO0 (EPWM1A) GpioCtrlRegs.GPAPUD.bit.GPIO1 = 0; // Enable pull-up on GPIO1 (EPWM1B) GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1; // Configure GPIO0 as EPWM1A复用功能设置,为0代表是普通IO口 GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1; // Configure GPIO1 as EPWM1B EDIS; EALLOW; SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0; // Stop all the TB clocks EDIS; PWM1_Init(); EALLOW; SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1; // Start all the timers synced EDIS; } void InitECapture() { ECap1Regs.ECEINT.all = 0x0000; // Disable all capture interrupts ECap1Regs.ECCLR.all = 0xFFFF; // Clear all CAP interrupt flags ECap1Regs.ECCTL1.bit.CAPLDEN = 0; // Disable CAP1-CAP4 register loads ECap1Regs.ECCTL2.bit.TSCTRSTOP = EC_STOP; // Make sure the counter is stopped // Configure peripheral registers ECap1Regs.ECCTL2.bit.CONT_ONESHT = EC_CONTINUOUS; // One-shot/continuous ECap1Regs.ECCTL2.bit.STOP_WRAP = 3; // Stop at 4 events(每4次进一次中断) ECap1Regs.ECCTL1.bit.CAP1POL = EC_RISING; // Rising edge ECap1Regs.ECCTL1.bit.CAP2POL = EC_FALLING; // Rising edge ECap1Regs.ECCTL1.bit.CAP3POL = EC_RISING; // Rising edge ECap1Regs.ECCTL1.bit.CAP4POL = EC_FALLING; // Rising edge ECap1Regs.ECCTL1.bit.CTRRST1 = EC_ABS_MODE; // Difference operation ECap1Regs.ECCTL1.bit.CTRRST2 = EC_ABS_MODE; // Difference operation ECap1Regs.ECCTL1.bit.CTRRST3 = EC_ABS_MODE; // Difference operation ECap1Regs.ECCTL1.bit.CTRRST4 = EC_ABS_MODE; // Difference operation ECap1Regs.ECCTL2.bit.SYNCI_EN = EC_DISABLE; // Enable sync in ECap1Regs.ECCTL2.bit.SYNCO_SEL = EC_SYNCO_DIS; // Pass through ECap1Regs.ECCTL1.bit.CAPLDEN = EC_ENABLE; // Enable capture units ECap1Regs.ECCTL1.bit.PRESCALE = EC_DIV1; ECap1Regs.ECCTL2.bit.CAP_APWM = EC_CAP_MODE; ECap1Regs.ECCTL2.bit.TSCTRSTOP = EC_RUN; // Start Counter ECap1Regs.ECEINT.bit.CEVT4 = 1; // 4 events = interrupt } void cap_init() { EALLOW; GpioCtrlRegs.GPAPUD.bit.GPIO24 = 0; // Enable pull-up on cap1 (EPWM1A) GpioCtrlRegs.GPAMUX2.bit.GPIO24 = 1; // Configure GPIO0 as cap1复用功能设置,为0代表是普通IO口 // GpioCtrlRegs.GPAPUD.bit.GPIO25 = 0; // Enable pull-up on GPIO1 (EPWM1B) // GpioCtrlRegs.GPAMUX1.bit.GPIO25 = 1; // Configure GPIO1 as EPWM1B 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

热门文章