我使用了timer3的通道2来捕获外部的脉冲信号。
main()函数中的程序如下:
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //GPIO??
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2 ;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
/* TIM enable counter */
TIM_Cmd(TIM3, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
while (1);
中断程序如下:
void TIM3_IRQHandler(void)
{
unsigned int Port;
if(TIM_GetITStatus(TIM3, TIM_IT_CC2) == SET)
{
/* Clear TIM3 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
if(CaptureNumber == 0)
{
/* Get the Input Capture value */
IC3ReadValue1 = TIM_GetCapture2(TIM3);
CaptureNumber = 1;
}
else if(CaptureNumber == 1)
{
/* Get the Input Capture value */
IC3ReadValue2 = TIM_GetCapture2(TIM3);
/* Capture computation */
if (IC3ReadValue2 > IC3ReadValue1)
{
Capture = (IC3ReadValue2 - IC3ReadValue1);
}
else
{
Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2);
}
/* Frequency computation */
TIM3Freq = (uint32_t) SystemCoreClock / Capture;
CaptureNumber = 0;
}
}
//翻转PC.10口,作为示波器的调试观测引脚。
//
Port = GPIOC->IDR & GPIO_Pin_10;
if (Port)
{
GPIOC->BRR = GPIO_Pin_10;
} else {
GPIOC->BSRR = GPIO_Pin_10;
}
}
硬件连接:
1.将信号源连接PA.07脚,信号源输出1KHz的方波。
2.示波器一个探头接PA.07,另一个接PC.10,观测捕获比较中断的执行情况,得到波形请看附件。
IMG_20140604_172958.rar
(880.63 KB)
|