28027 Ecap的CAP1、CAP2、CAP3和CAP4具有捕获边沿信号的功能。将CAPx设置为上升沿捕获或下降沿捕获,当外部信号和当前CAPx设置一致时,并将当前TSCTR值赋给CAPx寄存器。根据CAPx之间的差值计算捕获的方波信号周期和占空比。TSCTR的变化模式取决于ECap1Regs.ECCTL1.bit.CTRRSTx的设置,如果为‘0’,则动作匹配时,不复位TSCTR,将一直往上增加,直至溢出;如果为‘1’,则动作匹配时,复位TSCTR。
举例:在计算一个方波的周期时,只需要CAP1和CAP2即可:
CPA1:上升沿,ECap1Regs.ECCTL1.bit.CTRRST1=1(复位CTSTR)
CPA2
:上升沿,设置CEVT2
中断
第一上升沿时,CPA1动作,复位TSCTR。当第二个上降沿时,CPA2动作,并且产生CEVT2事件。同时将TSCTR数加载到CPA2中。这时可以利用CAP2里面的值计算出方波的周期。
如果计算方波的占空比,大家根据计算周期的方法想想就能找到方法。
以28027的ECAP计算方波周期为例。
第一步:初始化ECAP引脚(28027的GPIO5和GPIO19均可配置为ECAP口,本例中将GPIO19配置为ECAP引脚。可参见F2802x_ECap.c中的InitECap1Gpio函数)
void InitECap1Gpio(void)
{
EALLOW;
// GpioCtrlRegs.GPAPUD.bit.GPIO5=0; //Enable pull-up on GPIO5 (CAP1)
GpioCtrlRegs.GPAPUD.bit.GPIO19=0; // Enable pull-up on GPIO19 (CAP1)
// GpioCtrlRegs.GPAQSEL1.bit.GPIO5=0; // Synch to SYSCLKOUT GPIO5 (CAP1)
GpioCtrlRegs.GPAQSEL2.bit.GPIO19=0;//Synch to SYSCLKOUT GPIO19(CAP1)
// GpioCtrlRegs.GPAMUX1.bit.GPIO5=3; // Configure GPIO5 as CAP1
GpioCtrlRegs.GPAMUX2.bit.GPIO19=3; // Configure GPIO19 as CAP1
EDIS;
}
第二步:设置ECap寄存器:
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 = 0; // Make sure the counter is stopped
// Configure peripheral registers
ECap1Regs.ECCTL2.bit.CAP_APWM = 0; //CAP mode
ECap1Regs.ECCTL2.bit.CONT_ONESHT = 0; // continuous mode ECap1Regs.ECCTL2.bit.STOP_WRAP = 1; // Stop at 2 events
ECap1Regs.ECCTL1.bit.PRESCALE = 0; // Divide by 1
ECap1Regs.ECCTL1.bit.CAP1POL = 0; // Rising edge
ECap1Regs.ECCTL1.bit.CAP2POL = 0; // Rising edge
ECap1Regs.ECCTL1.bit.CTRRST1 = 1; // Difference operation
ECap1Regs.ECCTL1.bit.CTRRST2 = 0; // Difference operation
ECap1Regs.ECCTL2.bit.SYNCI_EN = 0; // Enable sync in
ECap1Regs.ECCTL2.bit.SYNCO_SEL = 3; // Disable sync out signal
ECap1Regs.ECCTL1.bit.CAPLDEN = 1; // Enable capture units
ECap1Regs.ECCTL2.bit.TSCTRSTOP = 1; // Start Counter
ECap1Regs.ECEINT.bit.CEVT2 = 1; // 2 events = interrupt
}
第三步:外部PIE和CPU中断使能。
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.ECAP1_INT = &ecap1_isr;
EDIS; // This is needed to disable write to EALLOW protected registers // Enable CPU INT4 which is connected to ECAP1-4 INT:
IER |= M_INT4;
PieCtrlRegs.PIEIER4.bit.INTx1 = 1;
// Enable eCAP INTn in the PIE: Group 3 interrupt 1-6
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
interrupt void ecap1_isr(void)
{
//ECap1Count = ECap1Regs.CAP1;
ECap2Count = ECap1Regs.CAP2;
//ECap1Regs.ECCLR.all = 0xFFFF;
ECap1Regs.ECCLR.bit.CEVT2 = 1;
ECap1Regs.ECCLR.bit.CEVT1 = 1;
ECap1Regs.ECCLR.bit.INT = 1;
PieCtrlRegs.PIEACK.all = PIEACK_GROUP4;
}
注:当使用One-shot mode方式时,若需要不断的捕获方波,需在中断程序中需加入“Cap1Regs.ECCTL2.bit.REARM = 1; ”,否则只能捕获一次。以后即使有方波信号也不会进入捕获中断处理函数。
如上即可利用ECAP的捕获功能来计算方波周期。当然灵活利用ECAP功能可以实现很多,这些需要大家去摸索。
目前“单相交流电压+电流表”已通过捕获功能实现频率测量。为什么在做“单相交流电压+电流表”时会关注,并且计算它的频率呢?大家可以考虑一下。我将在后续“单相交流电压+电流表__采样篇”做介绍。