使用tms320f2812的捕获单元测量外部输入的pwm波的频率的代码
时间: 2023-09-16 14:08:51 浏览: 85
以下是使用TMS320F2812的捕获单元测量外部输入的PWM波的频率的代码:
```c
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"
void InitCapture()
{
EALLOW;
SysCtrlRegs.PCLKCR1.bit.ECAP1ENCLK = 1; // Enable the clock for ECAP1
EDIS;
ECAP1CTL1.bit.CTRRST1 = 1; // Capture counter reset on rising edge
ECAP1CTL1.bit.PRESCALE = 0; // Divide by 1
ECAP1CTL1.bit.CAPLDEN = 1; // Enable capture loading
ECAP1CTL1.bit.CAP1POL = 1; // Rising edge polarity
ECAP1CTL1.bit.CAP2POL = 0; // Falling edge polarity
ECAP1CTL1.bit.CTRFILTRESET = 1; // Capture filter reset
ECAP1CTL1.bit.CAP3POL = 0; // Falling edge polarity
ECAP1CTL1.bit.CAP4POL = 0; // Falling edge polarity
ECAP1CTL1.bit.CAPLD = 1; // Load capture register
ECAP1CTL2.bit.SYNCI_EN = 0; // Disable sync in
ECAP1CTL2.bit.SYNCO_SEL = 0; // Sync out disabled
ECAP1CTL2.bit.TSCTRSTOP = 0; // Counter stopped
ECAP1INT.bit.CEVT1 = 1; // Enable capture interrupt for event 1
ECAP1INT.bit.CEVT2 = 0; // Disable capture interrupt for event 2
ECAP1INT.bit.CEVT3 = 0; // Disable capture interrupt for event 3
ECAP1INT.bit.CEVT4 = 0; // Disable capture interrupt for event 4
ECAP1INT.bit.CTR_EQ_CMP = 0; // Clear counter equal compare interrupt flag
ECAP1CTL2.bit.TSCTRSTOP = 0; // Start counter
}
void main()
{
InitSysCtrl();
InitPieCtrl();
InitPieVectTable();
InitCapture(); // Initialize ECAP1
IER |= M_INT3; // Enable interrupt for ECAP1
PieCtrlRegs.PIEIER3.bit.INTx1 = 1; // Enable interrupt for ECAP1
EINT; // Enable global interrupts
while(1)
{
asm(" NOP");
}
}
interrupt void ECAP1_ISR(void)
{
if(ECAP1INT.bit.CEVT1 == 1) // Check for event 1 interrupt
{
ECAP1INT.bit.CEVT1 = 1; // Clear event 1 interrupt flag
Uint32 period = ECAP1.CAP1; // Read capture register for period value
// Perform frequency calculation using period value
}
PieCtrlRegs.PIEACK.all = PIEACK_GROUP3; // Acknowledge interrupt
}
```
此代码使用ECAP1捕获单元测量外部输入的PWM波的频率。在初始化函数InitCapture中,设置ECAP1的各种寄存器以启用捕获单元并设置其工作模式。在主函数中,启用ECAP1中断,并在ECAP1_ISR中处理捕获事件。在此中断服务程序中,读取ECAP1的捕获寄存器以获取PWM波的周期值,并使用该值计算PWM波的频率。
阅读全文