目的大概就是得到两个上升沿之间的时间。问题是,在一堆正常数据中会出现一个两倍甚至三倍的数据。
[mw_shl_code=c,true]void TIM4_Configuration(void) //0.1s
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM4_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_TIM4);
TIM_TimeBaseStructure.TIM_Prescaler=84 - 1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=0xFFFF;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM4_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInit(TIM4, &TIM4_ICInitStructure);
TIM_ITConfig(TIM4,TIM_IT_CC1,ENABLE);
TIM_Cmd(TIM4,ENABLE );
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =4;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); [mw_shl_code=applescript,true]
void TIM4_IRQHandler(void)//
{
if(TIM_GetITStatus(TIM4, TIM_IT_CC1)!= RESET)
{
int t1=0;
static int t2=0;
t1 = t2;
t2 = TIM4->CCR1;
if(t2 > t1)
{
w_main = 100000 *90 / ((t2 - t1) * 209);
printf("%u
",(t2 - t1));
}
else
{
w_main = 100000 *90 / ((t2 + 0xffff - t1) * 209);
printf("%u
",(t2 + 0xffff - t1));
}
TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);
}
}[/mw_shl_code]
}[/mw_shl_code]
结果就像这样:
10001.000000
20002.000000
10001.000000
30002.000000
10001.000000
10001.000000
10001.000000
10001.000000
10001.000000
10001.000000
10000.000000
20002.000000
10001.000000
10001.000000
10001.000000
10000.000000
10001.000000
10001.000000
10001.000000
10001.000000
10001.000000
10001.000000
10000.000000
10001.000000
10001.000000
求解。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>