项目要求对工频电压进行过零检测,而为了防止出现误差,应该检测两次过零点的时间间隔是不是大于一定值,因此需要测量两次捕捉的相对时间。
进行两次检测,第一次检测到信号就复位计数器,第二次检测到的结果就是需要的相对时间,可直接使用。
IER |= M_INT4;
// Initialize eCAP1/2/3
InitECap1Gpio();
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.CONT_ONESHT = 1; // continue
ECap1Regs.ECCTL2.bit.STOP_WRAP = 1; // Stop at 2 events
ECap1Regs.ECCTL1.bit.CAP1POL = 0; // Rising edge
ECap1Regs.ECCTL1.bit.CAP2POL = 0; // Rising edge
ECap1Regs.ECCTL1.bit.CTRRST1 = 1; // Difference operation
ECap1Regs.ECCTL1.bit.CTRRST1 = 0; //第一次捕获,复位计数器,
//第二次得到的值就是需要的时间
ECap1Regs.ECCTL2.bit.SYNCI_EN = 0; // Enable sync in
ECap1Regs.ECCTL2.bit.SYNCO_SEL = 0x10; // Pass through
ECap1Regs.ECCTL1.bit.CAPLDEN = 1; // Enable capture units
ECap1Regs.ECCTL2.bit.REARM = 1;
ECap1Regs.ECCTL2.bit.TSCTRSTOP = 1; // Start Counter
ECap1Regs.ECCTL1.bit.CAPLDEN = 1; // Enable CAP1-CAP4 register loads
ECap1Regs.ECEINT.bit.CEVT2 = 1; // 1 events = interrupt
// Enable eCAP INTn in the PIE: Group 3 interrupt 1
PieCtrlRegs.PIEIER4.bit.INTx1 = 1;
每个CAP模块都有一个PIE中断,而CAP模块中又有7个触发源能够触发此中断,因而在相应的中断函数中,要进行识别,或者限制的处理
每次CAP模块捕获wrap的次数后,对应的引脚则被冻结,停止捕获,而ECap1Regs.ECCTL2.bit.REARM = 1;则能解除冻结,同时返回到cap1
因此在中断函数中应该加入
ECap1Regs.ECCLR.bit.CEVT2 = 1;
ECap1Regs.ECCLR.bit.INT = 1;
ECap1Regs.ECCTL2.bit.REARM = 1;// 用于重新启动CAP |