因为我的速度比较低,按频率说的话大概不到3k,所以用输入捕获来测速
[mw_shl_code=c,true]
void TIM5_CH1_Cap_Init(u32 arr,u16 psc)//psc=84-1;arr=0XFFFFFFFF
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
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(GPIOA,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM5);
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM5_ICInitStructure.TIM_ICFilter = 6;
TIM_ICInit(TIM5, &TIM5_ICInitStructure);
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);
TIM_Cmd(TIM5,ENABLE );
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM5_IRQHandler(void)
{
if(TIM_GetITStatus(TIM5, TIM_IT_CC1) != RESET)
{
last_cnt=now_cnt;
now_cnt=TIM_GetCapture1(TIM5);;
if(now_cnt>=last_cnt)
{
w_main=now_cnt-last_cnt;
}
else
{
w_main=0xFFFFFFFF-last_cnt+now_cnt;
}
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1|TIM_IT_Update);
}[/mw_shl_code]
程序大概就是上面这个样子,测试用频率为2khz,所以应该输出500,我在主程序输出w_main的时候,如下:(我是每隔1ms输出一次)
w_main 498.000000
w_main 2.000000
w_main 4.000000
w_main 500.000000
w_main 12.000000
w_main 25.000000
w_main 500.000000
w_main 498.000000
w_main 500.000000
w_main 25.000000
w_main 500.000000
w_main 1447.000000
w_main 480.000000
w_main 500.000000
w_main 500.000000
w_main 496.000000
w_main 491.000000
w_main 370.000000
w_main 15.000000
w_main 493.000000
w_main 500.000000
w_main 496.000000
w_main 500.000000
w_main 454.000000
w_main 7.000000
w_main 44.000000
简直惨不忍睹,第二个就是,我是用另一个板子做的波输入,在我还没有上电的时候就会有一堆输出,拔掉杜邦线就会消失,感觉像是干扰,但是不明白来自哪里,上电之后就像上面那样。跑来求助,希望知道的帮我看一下,谢谢!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
找问题找了很久,然后并没有修改的同样的程序,莫名的可以了,我有点懵。。我再回忆回忆到底哪里出问题了。谢谢原子哥!
一周热门 更多>