M471开发板提供了丰富的例程,为实际应用提供了极大的方便,其中的频率与脉冲检测功能就很有使用价值。 1.频率测量: 该功能是使用引脚PC5的EPWM1通道 0来输出检测信号,以引脚PC3的EPWM1通道2来检测信号。故在检测过程中,需按图1所示来短接PC3与PC5。 图1 引脚连接 其检测主程序如下: int32_t main(void)
{
SYS_UnlockReg();
SYS_Init();
SYS_LockReg();
UART0_Init();
printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz(PLL@ %dHz)\n", SystemCoreClock, PllClock);
printf("+------------------------------------------------------------------------+\n");
printf("| EPWM Driver Sample Code |\n");
printf("| |\n");
printf("+------------------------------------------------------------------------+\n");
printf(" This sample code will use EPWM1 channel 2 to capture\n the signal from EPWM1 channel 0.\n");
printf(" I/O configuration:\n");
printf(" EPWM1 channel 2(PC.3) <--> EPWM1 channel 0(PC.5)\n\n");
printf("Use EPWM1 Channel 2(PC.3) to capture the EPWM1 Channel 0(PC.5) Waveform\n");
while(1)
{
printf("\n\nPress any key to start EPWM Capture Test\n");
getchar();
EPWM_ConfigOutputChannel(EPWM1, 0, 250, 30);
EPWM_EnableOutput(EPWM1, EPWM_CH_0_MASK);
EPWM_Start(EPWM1, EPWM_CH_0_MASK);
EPWM_ConfigCaptureChannel(EPWM1, 2, 83, 0);
EPWM_Start(EPWM1, EPWM_CH_2_MASK);
EPWM_EnableCapture(EPWM1, EPWM_CH_2_MASK);
EPWM1->CAPCTL |= EPWM_CAPCTL_FCRLDEN2_Msk;
while((EPWM1->CNT[2]) == 0);
CalPeriodTime(EPWM1, 2);
EPWM_Stop(EPWM1, EPWM_CH_0_MASK);
while((EPWM1->CNT[0] & EPWM_CNT0_CNT_Msk) != 0);
EPWM_ForceStop(EPWM1, EPWM_CH_0_MASK);
EPWM_DisableOutput(EPWM1, EPWM_CH_0_MASK);
EPWM_Stop(EPWM1, EPWM_CH_2_MASK);
while((EPWM1->CNT[2] & EPWM_CNT2_CNT_Msk) != 0);
EPWM_ForceStop(EPWM1, EPWM_CH_2_MASK);
EPWM_DisableCapture(EPWM1, EPWM_CH_2_MASK);
EPWM_ClearCaptureIntFlag(EPWM1, 2, EPWM_CAPTURE_INT_FALLING_LATCH);
}
}
经程序的编译下载,其运行结果如图2所示。 图2 运行结果 其中,高电平的周期是:14399 ~14401,低电平的周期是:33599 ~ 33601, 整体周期是:47999 ~ 48001。 检测结果为: 高电平周期为: 14400,低电平周期为:33601, 整体周期为:48001,上升时间为:31935,下降时间为:17535。 2.脉冲计数: 该功能是使用引脚PB15来输入外部检测信号,并以Timer 0来捕捉计数,故在检测过程中,是按图3所示来连接外部的脉冲发生器。该脉冲发生器具有调节功能,通过电位器的调整即可改变脉冲信号的频率。 图3 线路连接 其检测主程序如下: int main(void)
{
int volatile i;
SYS_Init();
UART0_Init();
printf("\nThis sample code demonstrate timer free counting mode.\n");
printf("Please connect input source with Timer 0 capture pin PB.15, press any key to continue\n");
getchar();
TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1000000);
TIMER_SET_PRESCALE_VALUE(TIMER0, 0);
TIMER_SET_CMP_VALUE(TIMER0, TIMER_CMP_MAX_VALUE);
TIMER_EnableCapture(TIMER0, TIMER_CAPTURE_FREE_COUNTING_MODE, TIMER_CAPTURE_EVENT_RISING);
TIMER_Start(TIMER0);
TIMER_EnableCaptureInt(TIMER0);
NVIC_EnableIRQ(TMR0_IRQn);
while(1);
}
经程序的编译下载,其运行结果如图4至图6所示。 图4 运行提示 图5 驱动检测
图6 调节脉冲信号
|