使用TMS320F2812的捕获单元测量外部输入方波的频率代码
时间: 2023-09-26 10:07:29 浏览: 153
以下是使用TMS320F2812的捕获单元测量外部输入方波的频率的代码:
```c
#include "DSP2812_Device.h" // DSP2812 Headerfile Include File
#include "DSP2812_Examples.h" // DSP2812 Examples Include File
#define CPU_FREQ 150E6 // CPU Frequency in Hz
#define PWM_FREQ 10E3 // PWM Frequency in Hz
#define TBCLK_FREQ CPU_FREQ/2 // TBCLK Frequency in Hz
Uint16 pwmPeriod = (Uint16)(TBCLK_FREQ / PWM_FREQ);
interrupt void cpu_timer0_isr(void);
void main(void)
{
InitSysCtrl();
// Configure CPU Timer 0 to interrupt every second
ConfigCpuTimer(&CpuTimer0, CPU_FREQ, 1000000);
// Initialize capture unit 1
InitCpuTimers();
// Configure GPIO
EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO0 = 1; // Disable pull-up on GPIO0
GpioCtrlRegs.GPAQSEL1.bit.GPIO0 = 0; // Qualify GPIO0 for 3 samples
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1; // Configure GPIO0 as CAP1
EDIS;
// Configure capture unit 1
CpuTimer1Regs.TCR.bit.TSS = 1; // Stop timer
CpuTimer1Regs.TCR.bit.TRB = 1; // Reload timer
CpuTimer1Regs.TCR.bit.SOFT = 0;
CpuTimer1Regs.TCR.bit.FREE = 0;
CpuTimer1Regs.TCR.bit.TIE = 1; // Enable interrupt
CpuTimer1Regs.TCR.bit.TSS = 0; // Start timer
// Enable interrupts
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
while(1)
{
// Wait for interrupt
}
}
interrupt void cpu_timer0_isr(void)
{
// Clear interrupt flag
CpuTimer0.InterruptCount++;
// Calculate frequency
Uint16 capturePeriod = CpuTimer1Regs.CAP1;
float captureFreq = TBCLK_FREQ / (float)capturePeriod;
// Reset capture unit 1
CpuTimer1Regs.TCR.bit.TSS = 1; // Stop timer
CpuTimer1Regs.TCR.bit.TRB = 1; // Reload timer
CpuTimer1Regs.TCR.bit.TSS = 0; // Start timer
// Do something with captureFreq...
}
```
在这个示例中,我们使用CPU Timer 0定期触发中断。在中断处理程序中,我们读取捕获单元1的值,并计算输入方波的频率。然后,我们重置捕获单元1以准备下一个测量。请注意,我们使用CPU Timer 1作为捕获单元。在初始化函数中,我们将其配置为使用GPIO0作为输入端口,并启用中断。
阅读全文