[mw_shl_code=c,true]这是TIM2_CH1的,完全可以用。[/mw_shl_code]
[mw_shl_code=c,true]/**
* @brief TIM2_CAP_Init
* @param arr: This parameter must be a number between 0x0000 and 0xFFFF.
* @param prescaler: This parameter can be a number between 0x0000 and 0xFFFF;
* @retval None
*/
void TIM2_CAP_Init(u16 arr, u16 prescaler)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_ICInitTypeDef TIM_ICInitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
// 开启TIM2和GPIOA的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// PA0初始化
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD; // 设置为下拉输入
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOA, &GPIO_InitStruct);
// TIM2定时器初始化
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseInitStruct.TIM_Period = arr;
TIM_TimeBaseInitStruct.TIM_Prescaler = prescaler;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
// TIM2_CH1输入捕获初始化
TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;
TIM_ICInitStruct.TIM_ICFilter = 0x00; // 不滤波
TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising; // 上升沿捕获
TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1; // 输入分频器不分频
TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI; // 映射到IC1
TIM_ICInit(TIM2, &TIM_ICInitStruct);
// 中断分组初始化
NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStruct);
TIM_ITConfig(TIM2, TIM_IT_Update|TIM_IT_CC1, ENABLE); // 更新中断和CC1IE捕获中断
TIM_Cmd(TIM2, ENABLE);
}
//定时器5中断服务程序
void TIM2_IRQHandler(void)
{
if(TIM_GetFlagStatus(TIM2, TIM_FLAG_CC1) != RESET) // 边沿捕获触发标志触发
{
pluse_cnt ++;
}
TIM_ClearFlag(TIM2, TIM_FLAG_CC1); // 清除状态标志
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); // 清除中断标志位
}
[/mw_shl_code]
这是TIM3_CH4,就不能使用了。
[mw_shl_code=c,true]/**
* @brief TIM3_CAP_Init
* @param arr: This parameter must be a number between 0x0000 and 0xFFFF.
* @param prescaler: This parameter can be a number between 0x0000 and 0xFFFF;
* @retval None
*/
void TIM3_CAP_Init(u16 arr, u16 prescaler)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_ICInitTypeDef TIM_ICInitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
// 开启TIM3和GPIOB的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// PB1初始化
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD; // 设置为下拉输入
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStruct);
// TIM3定时器初始化
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseInitStruct.TIM_Period = arr;
TIM_TimeBaseInitStruct.TIM_Prescaler = prescaler;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
// TIM3_CH4输入捕获初始化
TIM_ICInitStruct.TIM_Channel = TIM_Channel_4;
TIM_ICInitStruct.TIM_ICFilter = 0x00; // 不滤波
TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising; // 上升沿捕获
TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1; // 输入分频器不分频
TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI; // 映射到IC1
TIM_ICInit(TIM3, &TIM_ICInitStruct);
// 中断分组初始化
NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStruct);
TIM_ITConfig(TIM3, TIM_IT_Update|TIM_IT_CC4, ENABLE); // 更新中断和CC1IE捕获中断
TIM_Cmd(TIM3, ENABLE);
}
//定时器3中断服务程序
void TIM3_IRQHandler(void)
{
if(TIM_GetFlagStatus(TIM3, TIM_FLAG_CC4) != RESET) // 边沿捕获触发标志触发
{
pluse_cnt ++;
}
TIM_ClearFlag(TIM3, TIM_FLAG_CC4); // 清除状态标志
TIM_ClearITPendingBit(TIM3, TIM_IT_Update); // 清除中断标志位
}[/mw_shl_code]
仿真查看TIM3的寄存器,有中断发生,但是CCR4通道一直为0,这很不正常啊,不知道怎么回事,请问有大虾指教一下吗,或者有没有遇到和我一样情况的?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
---------------------------------
原子哥 走了好多弯路,问题最终解决了 把GPIO由下拉输入改为上拉或者浮空。。。。。。。。
好无语啊
---------------------------------
原子哥,真的不行了。想了两天,要哭了。。。关于脉冲捕获的。我用PA0--TIM2_CH1可以
但是用PB1--TIM3_CH4就不行了 很纳闷啊 原子哥能不能稍稍发发功麻烦看看啊
一周热门 更多>