stm32捕获问题,求解答
时间:10-02
整理:3721RD
点击:
学习捕获章节发现:捕获一个按钮上升沿,每捕获到一个上升沿LED取反,上升沿由按键产生,目前硬件仿真发现,当我按下的时候,确实捕获到上升沿,但当我放开的时候却意外发现又捕获到一次,原因是捕获标志位再次挂起,但不是每次都会,差不多按4次,放开4次中有3次会。求解答,是否捕获原本就必须去抖?还是说我哪里设置错误,以下是我的程序
/*oˉêy??£oTIM5_Init
*?èê? £oTIM53?ê??ˉ
*2?êy £o?T
*·μ?? £o?T
*/
void TIM5_Init(void)
{
//?¨ò??á11ì?
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
//?a???¨ê±ê±?ó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
//72M£¨APB2£?ò?·??μ×÷?aTIMê±?ó
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_div1;
//?òé???êy
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
//??êy′?êy
TIM_TimeBaseInitStruct.TIM_Period = 0xffff;
//TIM·??μ?μ
TIM_TimeBaseInitStruct.TIM_Prescaler = 71;
//???′??êy?÷
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
//????
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStruct);
//?D??ê1?ü
TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE);
//2???à??ó??è??
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//í¨μàTIM1_UP_IRQHandleró??è??????
NVIC_InitStruct.NVIC_IRQChannel = TIM5_IRQn;
//ó??è??ê1?ü
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
//????í¨μà?à????±e
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
//????í¨μà?ìó|??±e
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
//????
NVIC_Init(&NVIC_InitStruct);
//?¨ê±?÷ê1?ü
TIM_Cmd(TIM5,ENABLE);
}
/*oˉêy??£oTIM5_IC_Config
*?èê? £oTIM53?ê??ˉ
*2?êy £o?T
*·μ?? £o?T
*/
void TIM5_IC_Config(void)
{
//?¨ò??á11ì?
TIM_ICInitTypeDef TIM_ICInitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 ;
GPIO_Init(GPIOA,&GPIO_InitStruct);
PAout(0) = 0;
//????í¨μàò?
TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;
//6′???2¨
TIM_ICInitStruct.TIM_ICFilter = 0;
//é?éy??2???
TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
//2?·??μ
TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_div1;
//ó3é?μ?TIé?
TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
//2???3?ê??ˉ
TIM_ICInit(TIM5,&TIM_ICInitStruct);
//?D??ê1?ü
TIM_ITConfig(TIM5,TIM_IT_CC1,ENABLE);
}
void TIM5_IRQHandler(void)
{
if(TIM_GetITStatus(TIM5,TIM_IT_Update) == SET)
{
//??3y±ê????
TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
}
if(TIM_GetITStatus(TIM5,TIM_IT_CC1) == SET)
{
PBout(5) = ~PBout(5);
while(PAin(0));
TIM_ClearITPendingBit(TIM5,TIM_IT_CC1);
}
}
乱码都是注释,忽略不看即可。
/*oˉêy??£oTIM5_Init
*?èê? £oTIM53?ê??ˉ
*2?êy £o?T
*·μ?? £o?T
*/
void TIM5_Init(void)
{
//?¨ò??á11ì?
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
//?a???¨ê±ê±?ó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
//72M£¨APB2£?ò?·??μ×÷?aTIMê±?ó
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_div1;
//?òé???êy
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
//??êy′?êy
TIM_TimeBaseInitStruct.TIM_Period = 0xffff;
//TIM·??μ?μ
TIM_TimeBaseInitStruct.TIM_Prescaler = 71;
//???′??êy?÷
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
//????
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStruct);
//?D??ê1?ü
TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE);
//2???à??ó??è??
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//í¨μàTIM1_UP_IRQHandleró??è??????
NVIC_InitStruct.NVIC_IRQChannel = TIM5_IRQn;
//ó??è??ê1?ü
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
//????í¨μà?à????±e
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
//????í¨μà?ìó|??±e
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
//????
NVIC_Init(&NVIC_InitStruct);
//?¨ê±?÷ê1?ü
TIM_Cmd(TIM5,ENABLE);
}
/*oˉêy??£oTIM5_IC_Config
*?èê? £oTIM53?ê??ˉ
*2?êy £o?T
*·μ?? £o?T
*/
void TIM5_IC_Config(void)
{
//?¨ò??á11ì?
TIM_ICInitTypeDef TIM_ICInitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 ;
GPIO_Init(GPIOA,&GPIO_InitStruct);
PAout(0) = 0;
//????í¨μàò?
TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;
//6′???2¨
TIM_ICInitStruct.TIM_ICFilter = 0;
//é?éy??2???
TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
//2?·??μ
TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_div1;
//ó3é?μ?TIé?
TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
//2???3?ê??ˉ
TIM_ICInit(TIM5,&TIM_ICInitStruct);
//?D??ê1?ü
TIM_ITConfig(TIM5,TIM_IT_CC1,ENABLE);
}
void TIM5_IRQHandler(void)
{
if(TIM_GetITStatus(TIM5,TIM_IT_Update) == SET)
{
//??3y±ê????
TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
}
if(TIM_GetITStatus(TIM5,TIM_IT_CC1) == SET)
{
PBout(5) = ~PBout(5);
while(PAin(0));
TIM_ClearITPendingBit(TIM5,TIM_IT_CC1);
}
}
乱码都是注释,忽略不看即可。
拜托各位帮忙了,晕死我了。
加个去抖试试?