大家有什么好的外部中断按键去抖的方法吗??因为比赛的需要,要让输出步进可调,所以打算在外部中断里面改输出值,但是按键抖个不停啊,已经软件延时消抖了,还是抖啊。用的是stm32f407。
代码如下,有什么需要改的吗??
- void EXTI2_IRQHandler(void)
- {
- EXTI->IMR&=~(1<<2);
- if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2)==0)
- {
- delay_ms(30);
- if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2)==0)
- {
- vtg-=200;
- DAC_SetVol(vtg);
- }
- }
- EXTI_ClearITPendingBit(EXTI_Line2);
- EXTI->IMR|=1<<2;
- }
复制代码
此帖出自
小平头技术问答
不是吧,帮忙看看呢。
- void Key_Init()
- {
- GPIO_InitTypeDef GPIOC_InitStruct;
- GPIOC_InitStruct.GPIO_Mode=GPIO_Mode_IN;
- GPIOC_InitStruct.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
- GPIOC_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
- GPIOC_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOC,&GPIOC_InitStruct);
- }
复制代码- void Exti_Init()
- {
- NVIC_InitTypeDef NVIC_InitStruct;
- EXTI_InitTypeDef EXTI_InitStruct;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//使能SYSCFG时钟
- SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource0);
- SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource1);
- SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource2);
- SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource3);
-
- EXTI_InitStruct.EXTI_Line=EXTI_Line0|EXTI_Line1|EXTI_Line2|EXTI_Line3;
- EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
- EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling;
- EXTI_InitStruct.EXTI_LineCmd=ENABLE;
- EXTI_Init(&EXTI_InitStruct);
-
- NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;//外部中断0
- NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x03;//抢占优先级3
- NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x02;//子优先级2
- NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
- NVIC_Init(&NVIC_InitStruct);//配置
-
-
- NVIC_InitStruct.NVIC_IRQChannel = EXTI1_IRQn;//外部中断1
- NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x03;//抢占优先级3
- NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x02;//子优先级2
- NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
- NVIC_Init(&NVIC_InitStruct);//配置
-
-
- NVIC_InitStruct.NVIC_IRQChannel = EXTI2_IRQn;//外部中断2
- NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x01;//抢占优先级1
- NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x02;//子优先级2
- NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
- NVIC_Init(&NVIC_InitStruct);//配置
-
- NVIC_InitStruct.NVIC_IRQChannel = EXTI3_IRQn;//外部中断3
- NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x01;//抢占优先级1
- NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x02;//子优先级2
- NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
- NVIC_Init(&NVIC_InitStruct);//配置
-
- }
复制代码估计要多长呢。。。
一周热门 更多>