在线时间0 小时
UID163926
ST金币0
蝴蝶豆0
注册时间2008-4-13
新手上路
- 最后登录
- 1970-1-1
|
a0a.1 0b0c
问: 程序如下,可以采集脉冲信号,但是IC2Value = TIM_GetCapture1(TIM5);值不稳定,请教各位论坛技术支持,是不是硬件哪块还需要设置?软件我用去极值滤波算法之后的效果也不是很好,主要是硬件寄存器里面的数本身就不稳定。
int main(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
/* GPIOA clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE)
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the TIM5 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_InitTypeDef GPIO_InitStructure;
/* TIM5 channel 1 pin (PA.00) configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
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_PWMIConfig(TIM5, &TIM_ICInitStructure);
/* Select the TIM5 Input Trigger: TI1FP1 */
TIM_SelectInputTrigger(TIM5, TIM_TS_TI1FP1);
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(TIM5, TIM_SlaveMode_Reset);
/* Enable the Master/Slave Mode */
TIM_SelectMasterSlaveMode(TIM5, TIM_MasterSlaveMode_Enable);
/* TIM enable counter */
TIM_Cmd(TIM5, ENABLE);
/* Enable the CC1 Interrupt Request */
TIM_ITConfig(TIM5, TIM_IT_CC1, ENABLE);
while (1);
}
void TIM5_IRQHandler(void)
{
/* Clear TIM5 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1);
/* Get the Input Capture value */
IC2Value = TIM_GetCapture1(TIM5);
if (IC2Value != 0)
{
/* Duty cycle computation */
DutyCycle = (TIM_GetCapture2(TIM5) * 100) / IC2Value;
/* Frequency computation */
Frequency = SystemCoreClock / IC2Value;
}
else
{
DutyCycle = 0;
Frequency = 0;
}
} |
|